How do i add two different shapes to D3 forced directed graph based on shape field value?

最后都变了- 提交于 2019-11-30 14:47:19

You can do this, as shown in e.g. this example, by using a symbol generator and path elements instead of SVG elements for specific shapes. The code to add shapes becomes

var node = svg.selectAll(".node")
  .data(data.nodes)
  .enter().append("path")
  .attr("class", "node")
  .attr("d", d3.svg.symbol()
    .type(function(d) { return d3.svg.symbolTypes[d.s]; }))
  .style("fill", function(d) { return color(d.group); })
  .call(force.drag);

Then you also need to change your tick handler to change the transform attribute of the path elements:

node.attr("transform", function(d) {
    return "translate(" + d.x + "," + d.y + ")";
});

Complete jsfiddle here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!