admin管理员组文章数量:1331388
D3's operation on selection are versatile (likeattr
) that it takes a value, or a function with the data passed on. I wanted to set few style parameters based on the current selection.
From the doc: From what I understand, below scenarios will work
sel.style("stroke", "#000")
sel.style({"stroke": "#000", "stroke-width": "0.5"})
sel.style("stroke", function(d){return "#000"})
but, what I need is it to take an object returned by a fn.
var fn = function(d){
return {"stroke": "#000", "stroke-width": "0.5"}
}
sel.style( fn)
Above example, I don't do anything with d
, but ,in my case, I would make decision based on d
passed.
I am sure, I may be able to do something like,
sel.style(`fill`, fillFn)
sel.style(`stroke`, strokeFn)
But, I am trying to make my code generic, so I may not know what style I need to set ahead of time.
Edit:
I also tried, selection.property
,
sel.property("style", fn);
but got error
TypeError: Object [object Array] has no method 'property'
but, on the same selection, if I do
sel.style({"stroke": "#000", "stroke-width": "0.5"})
it works fine. What could be wrong?
D3's operation on selection are versatile (likeattr
) that it takes a value, or a function with the data passed on. I wanted to set few style parameters based on the current selection.
From the doc: From what I understand, below scenarios will work
sel.style("stroke", "#000")
sel.style({"stroke": "#000", "stroke-width": "0.5"})
sel.style("stroke", function(d){return "#000"})
but, what I need is it to take an object returned by a fn.
var fn = function(d){
return {"stroke": "#000", "stroke-width": "0.5"}
}
sel.style( fn)
Above example, I don't do anything with d
, but ,in my case, I would make decision based on d
passed.
I am sure, I may be able to do something like,
sel.style(`fill`, fillFn)
sel.style(`stroke`, strokeFn)
But, I am trying to make my code generic, so I may not know what style I need to set ahead of time.
Edit:
I also tried, selection.property
,
sel.property("style", fn);
but got error
TypeError: Object [object Array] has no method 'property'
but, on the same selection, if I do
sel.style({"stroke": "#000", "stroke-width": "0.5"})
it works fine. What could be wrong?
Share Improve this question edited Oct 16, 2013 at 18:28 bsr asked Oct 16, 2013 at 18:21 bsrbsr 58.7k88 gold badges217 silver badges321 bronze badges2 Answers
Reset to default 5In this case, I would use the .each()
function (or maybe .call()
) to run a function on each (or all at once) selected elements. In this function, you can, based on data or something else, add styles, attributes etc.
The basic code would look like this.
sel.each(someFunc);
function someFunc(d) {
if(d.something) { d3.select(this).style("foo", "bar"); }
if(d.somethingElse) { d3.select(this).attr("bar", "foo"); }
}
My answer relates to css almost but I think it's a good idea to solve the problem.
d3.selection.prototype.css = function (...arr) {
if (arr.length === 2) {
this.style(arr[0], arr[1]);
} else if (arr.length === 1 && typeof arr[0] === 'object') {
let properties = arr[0];
for (let prop in properties) {
this.style(prop, properties[prop]);
}
}
return this;
};
Useage:
d3.select('body').css('text-decoration', 'underline').css({
'color': '#f00',
'font-size': '20px'
});
p/s: I'm just learning d3 about 2 hours ago. Good luck :)
本文标签: javascriptD3js setting multiple styles dynamicallyStack Overflow
版权声明:本文标题:javascript - D3.js setting multiple styles dynamically - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742282885a2446419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论