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

后端 未结 1 1662
有刺的猬
有刺的猬 2021-01-03 02:25

I am newbie to D3. I am using force directed graph. I want to add two different type of shapes at node\'s places.

My json is following:

{
  \"nodes\"         


        
相关标签:
1条回答
  • 2021-01-03 02:58

    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.

    0 讨论(0)
提交回复
热议问题