javascript - D3V4 - 无法让 d3.tree 在 CodeSnippet 中工作

标签 javascript d3.js

该代码在我的浏览器中运行良好,取自 https://bl.ocks.org/d3noob/b024fcce8b4b9264011a1c3e7c7d70dc

但是,当我尝试在 StackOverflow 中创建 CodeSnippet 时,出现以下错误(如下)。我使用的是 D3.4.9.1,如何修复代码才能使其作为 CodeSnippet 运行而不会出现错误?一旦我可以让代码片段工作,我就可以发布我的实际问题;)

    var treeData =
    {
      "value": "+",
      "children": [
        {
          "value": "/",
          "children": [
            { "value": "4" },
            { 
              "value": "7",
            }
          ]              
        },
        { 
          "value": "/",
          "children": [
            { "value": "2" },
            { 
              "value": "7",
            }
          ]   
        }
      ]
    };
    // set the dimensions and margins of the diagram
    var margin = {top: 40, right: 40, bottom: 40, left: 40},
    width = 600 - margin.left - margin.right,
    height = 600 - margin.top - margin.bottom;
    // declares a tree layout and assigns the size
    var treemap = d3.tree()
    .size([width, height]);
    //  assigns the data to a hierarchy using parent-child relationships
    var nodes = d3.hierarchy(treeData);
    // maps the node data to the tree layout
    nodes = treemap(nodes);
    // append the svg obgect to the body of the page
    // appends a 'group' element to 'svg'
    // moves the 'group' element to the top left margin
    var svg = d3.select(".xTree").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom),
    g = svg.append("g")
    .attr("transform",
    "translate(" + margin.left + "," + margin.top + ")");
    // adds the links between the nodes
    var link = g.selectAll(".bbTreeLink")
    .data( nodes.descendants().slice(1))
    .enter().append("path")
    .attr("class", "bbTreeLink")
    .attr("d", function(d) {
      return "M" + d.x + "," + d.y
      + "C" + d.x + "," + (d.y + d.parent.y) / 2
      + " " + d.parent.x + "," +  (d.y + d.parent.y) / 2
      + " " + d.parent.x + "," + d.parent.y;
    });
    // adds each node as a group
    var node = g.selectAll(".bbTreeNode")
    .data(nodes.descendants())
    .enter().append("g")
    .attr("class", function(d) {
      return "bbTreeNode" +
      (d.children ? " bbTreeNode--internal" : " bbTreeNode--leaf"); })
    .attr("transform", function(d) {
      return "translate(" + d.x + "," + d.y + ")"; });
      // adds the circle to the node
      node.append("circle")
      .attr("r", 26);
      // adds the text to the node
      node.append("text")
      .attr("dy", ".35em")
      .attr('class', 'bbTreeText')
      //- .attr("y", function(d) { return d.children ? -20 : 20; })
      .style("text-anchor", "middle")      
      .text(function(d) { return d.data.value; });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.9/d3.min.js"></script>

{
  "message": "Uncaught TypeError: d3.tree is not a function",
  "filename": "https://stacksnippets.net/js",
  "lineno": 43,
  "colno": 22
}

code snippet screen

最佳答案

看来您无意中使用了 D3 版本 3.4.9在你的<script> 。使用 v4 代替:

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.12.2/d3.min.js"></script>

示例:

    
.node circle {
  fill: #fff;
  stroke: steelblue;
  stroke-width: 3px;
}

.node text { font: 12px sans-serif; }

.node--internal text {
  text-shadow: 0 1px 0 #fff, 0 -1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff;
}

.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 2px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.12.2/d3.min.js"></script>

<script>

var treeData =
  {
    "name": "Top Level",
    "children": [
      { 
		"name": "Level 2: A",
        "children": [
          { "name": "Son of A" },
          { "name": "Daughter of A" }
        ]
      },
      { "name": "Level 2: B" }
    ]
  };

// set the dimensions and margins of the diagram
var margin = {top: 40, right: 90, bottom: 50, left: 90},
    width = 660 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

// declares a tree layout and assigns the size
var treemap = d3.tree()
    .size([width, height]);

//  assigns the data to a hierarchy using parent-child relationships
var nodes = d3.hierarchy(treeData);

// maps the node data to the tree layout
nodes = treemap(nodes);

// append the svg obgect to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("body").append("svg")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom),
    g = svg.append("g")
      .attr("transform",
            "translate(" + margin.left + "," + margin.top + ")");

// adds the links between the nodes
var link = g.selectAll(".link")
    .data( nodes.descendants().slice(1))
  .enter().append("path")
    .attr("class", "link")
    .attr("d", function(d) {
       return "M" + d.x + "," + d.y
         + "C" + d.x + "," + (d.y + d.parent.y) / 2
         + " " + d.parent.x + "," +  (d.y + d.parent.y) / 2
         + " " + d.parent.x + "," + d.parent.y;
       });

// adds each node as a group
var node = g.selectAll(".node")
    .data(nodes.descendants())
  .enter().append("g")
    .attr("class", function(d) { 
      return "node" + 
        (d.children ? " node--internal" : " node--leaf"); })
    .attr("transform", function(d) { 
      return "translate(" + d.x + "," + d.y + ")"; });

// adds the circle to the node
node.append("circle")
  .attr("r", 10);

// adds the text to the node
node.append("text")
  .attr("dy", ".35em")
  .attr("y", function(d) { return d.children ? -20 : 20; })
  .style("text-anchor", "middle")
  .text(function(d) { return d.data.name; });
    
</script>

关于javascript - D3V4 - 无法让 d3.tree 在 CodeSnippet 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48393625/

相关文章:

javascript - 如何在 WordPress 中异步入队 jQuery?

javascript - IE 不显示在 Javascript 中完成的更新的 IMG SRC 更改

javascript - 隐藏选中不同比例的不同div

javascript - 在 for 循环中调用 Function.prototype.call.apply

javascript - 如何将非常大的 CSV 数据集加载到 d3

javascript - 如何在 AngularJS 中停止 $observe

javascript - 条形图的子集序数域

javascript - d3 js 工具提示不会显示

javascript - 如何更新具有相同父值的同一级别中的所有现有元素?

javascript - 传单版本 1.0.2 中未触发事件 View 重置