admin管理员组文章数量:1307752
Using the force layout in d3, I can happily add nodes as, say, an SVG group of a circle and some text:
var node = vis.selectAll(".node")
.data(nodes)
.enter().append("svg:g")
.attr("class", "node");
node.append("svg:circle")
.attr("r", 5);
node.append("svg:text")
.text(function(d) { return d.nodetext });
However, I would like to optionally add another element to the group, dependent on the node data: so if the data for this node contains an "image" attribute, I'd like to add a new child-element (an svg:image). It's easy to add the svg:image to all nodes (I just do it as I've done the circle and text above). It's also easy to dynamically change an attribute of an element you've already created (by having a function as the attribute's value, as per text
above). I do not know how to add an svg:image
child element only if the data contains an image
attribute. What's the best way of achieving this?
Using the force layout in d3, I can happily add nodes as, say, an SVG group of a circle and some text:
var node = vis.selectAll(".node")
.data(nodes)
.enter().append("svg:g")
.attr("class", "node");
node.append("svg:circle")
.attr("r", 5);
node.append("svg:text")
.text(function(d) { return d.nodetext });
However, I would like to optionally add another element to the group, dependent on the node data: so if the data for this node contains an "image" attribute, I'd like to add a new child-element (an svg:image). It's easy to add the svg:image to all nodes (I just do it as I've done the circle and text above). It's also easy to dynamically change an attribute of an element you've already created (by having a function as the attribute's value, as per text
above). I do not know how to add an svg:image
child element only if the data contains an image
attribute. What's the best way of achieving this?
1 Answer
Reset to default 11Try selection.filter:
node.filter(function(d) { return d.image; }).append("image")
.attr("xlink:href", function(d) { return d.image; })
.attr("width", 16)
.attr("height", 16);
本文标签: javascriptd3 Optionally add child elements based on node content in force layoutStack Overflow
版权声明:本文标题:javascript - d3: Optionally add child elements based on node content in force layout - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741800263a2398179.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论