admin管理员组文章数量:1334681
I am working on a D3 Scatter Plot having data from different regions - here regions are Continent names like Europe, Asia etc. I want to associate the region with a class name so that I can perform mon activities for one region.
Below is my code to add multiple classes together to an element:
var dot = svg.append("g")
.attr("class", "dots")
.selectAll(".dot")
.data(interpolateData(1900))
.enter().append("circle")
.attr("class", "dot " + function(d) { return d.Region; });
When I write the following code to check what classes are associated with the current object.
alert(this.getAttribute('class'));
I got this output:
So I can see that it is adding "dot" class successfully but not able to add Region dynamically.
TIA
I am working on a D3 Scatter Plot having data from different regions - here regions are Continent names like Europe, Asia etc. I want to associate the region with a class name so that I can perform mon activities for one region.
Below is my code to add multiple classes together to an element:
var dot = svg.append("g")
.attr("class", "dots")
.selectAll(".dot")
.data(interpolateData(1900))
.enter().append("circle")
.attr("class", "dot " + function(d) { return d.Region; });
When I write the following code to check what classes are associated with the current object.
alert(this.getAttribute('class'));
I got this output:
So I can see that it is adding "dot" class successfully but not able to add Region dynamically.
TIA
Share Improve this question asked Nov 9, 2016 at 10:04 disp_namedisp_name 1,4882 gold badges24 silver badges49 bronze badges2 Answers
Reset to default 5You have move your function, passing it as the second argument of attr
:
.attr("class", function(d){
return "dot " + d.Region;
});
Once I encountered the same problem. I expected d3.js attr() to accept collection of values to represent multiple classes and ended up searching for the solution on this issue.
Multiple classes can be passed as a result of method call:
.attr('class', (d) => `label item${d.itemNr}`);
where label
is the first class and item0
is the second one after this code is executed. You can add many more.
This is the result you get:
<text class="label item0" x="543" y="51">Item 0</text>
Maybe the design originates from data driven approach - you have selection of data and want to pass function of data or its result for an attribute's value. At the end, that made sense for me.
本文标签: javascriptAdding multiple class name to an element D3Stack Overflow
版权声明:本文标题:javascript - Adding multiple class name to an element D3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742374707a2462929.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论