How do I create a tree layout using JSON data in d3.v4 - without stratify()

后端 未结 2 1203
野的像风
野的像风 2021-02-07 14:13

I\'m trying to update some d3 code from v3 to version 4. I have a tree diagram using JSON data. The d3.v4 examples show how to convert tabular data (e.g. flare.csv) to hierarchi

相关标签:
2条回答
  • 2021-02-07 14:48

    I had the same problem. Emma's response essentially answered it, but I still had trouble getting it to work properly. I'll elaborate a bit in case anyone may find it helpful. I created a block showing the functional result here, which is based on your example data above, Emma's response, and this block.

    The answer is three parts:

    • You have your JSON object wrapped in Array brackets. Either remove those brackets (as I did in the gist), or put a [0] on the end of your treeData (as Emma did in the link above) to get the Object out of that 1 element Array.
    • Pass your JSON object to d3.hierarchy (as Emma explained).
    • Lastly, the API appears to have changed for naming the nodes, which is also taken care of in my block linked above.

    For convenience, here's the code from the block I made:

    var svg = d3.select("svg"),
        width = +svg.attr("width"),
        height = +svg.attr("height"),
        g = svg.append("g").attr("transform", "translate(60,0)");
    
    var tree = d3.cluster()
        .size([height, width - 160]);
    
    var stratify = d3.stratify()
        .parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); });
    
    d3.json("treeData.json", function(error, treeData) {
      if (error) throw error;
    
      var root = d3.hierarchy(treeData);
      tree(root);
    
      var link = g.selectAll(".link")
          .data(root.descendants().slice(1))
        .enter().append("path")
          .attr("class", "link")
          .attr("d", function(d) {
            return "M" + d.y + "," + d.x
                + "C" + (d.parent.y + 100) + "," + d.x
                + " " + (d.parent.y + 100) + "," + d.parent.x
                + " " + d.parent.y + "," + d.parent.x;
          });
    
      var node = g.selectAll(".node")
          .data(root.descendants())
        .enter().append("g")
          .attr("class", function(d) { return "node" + (d.children ? " node--internal" : " node--leaf"); })
          .attr("transform", function(d) { 
            return "translate(" + d.y + "," + d.x + ")"; 
          })
    
      node.append("circle")
          .attr("r", 2.5);
    
      node.append("text")
          .attr("dy", 3)
          .attr("x", function(d) { return d.children ? -8 : 8; })
          .style("text-anchor", function(d) { return d.children ? "end" : "start"; })
          .text(function(d) { 
            return d.data.name;
          });
    });
    

    and here's treeData.json removed from the Array:

    {
      "name": "Top Level",
      "parent": "null",
      "children": [
        {
          "name": "Level 2: A",
          "parent": "Top Level",
          "children": [
            {
              "name": "Son of A",
              "parent": "Level 2: A"
            },
            {
              "name": "Daughter of A",
              "parent": "Level 2: A"
            }
          ]
        },
        {
          "name": "Level 2: B",
          "parent": "Top Level"
        }
      ]
    }
    
    0 讨论(0)
  • 2021-02-07 14:55

    You need to run d3.hierarchy on your data to get it ready for tree(). Stratify presumably runs hierarchy or is similar in some way. So change the appropriate line to: var root = d3.hierarchy(treeData[0]);

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