D3 V4树节点链接图表

时间:2019-03-01 02:47:35

标签: javascript d3.js

我希望使用D3 v4创建以下图表,任何人都可以通过提供相关的源链接或指针来帮助我。

enter image description here 使用以下命令,创建树节点很容易:

var treeData = {
  "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"
    }
  ]
};
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(".")); });


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) { console.log("1: ", d); return "node" + (d.children ? " node--internal" : " node--leaf"); })
  .attr("transform", function (d) {
    console.log("2: ", d);
    return "translate(" + d.y + "," + d.x + ")";
  })

node.append("rect")
  .attr("width", 100)
  .attr("height", 50)
  .attr("x", -50)
  .attr("y", -25)
  .attr("fill", '#79a4f6');

node.append("text")
  .attr("y", 30)
  .attr("x", 50)
  .style("text-anchor", function (d) { return d.children ? "end" : "start"; })
  .text(function (d) {
    return d.data.name;
  });

我需要以下指针:

  • 创建背景图布局的季度标签和年份标签
  • 将文本居中居中
  • 节点链接需要从参考图所示的矩形侧面开始和结束。

0 个答案:

没有答案
相关问题