D3 布局树未呈现

D3 layout tree is not rendering

本文关键字:布局 D3      更新时间:2023-09-26

我不知道为什么 D3 布局树没有呈现。我想及时在页面正文中呈现 D3 布局。我尝试同时呈现"body"或 Div,但没有呈现。请建议?在我这边需要做什么。

下面是编辑后的代码

    var treeData = {
            name: "/",
            contents: [
        {
            name: "Applications",
            contents: [
                { name: "Mail.app" },
                { name: "iPhoto.app" },
                { name: "Keynote.app" },
                { name: "iTunes.app" },
                { name: "XCode.app" },
                { name: "Numbers.app" },
                { name: "Pages.app" }
            ]
        },
        {
            name: "System",
            contents: []
        },
        {
            name: "Library",
            contents: [
                {
                    name: "Application Support",
                    contents: [
                        { name: "Adobe" },
                        { name: "Apple" },
                        { name: "Google" },
                        { name: "Microsoft" }
                    ]
                },
                {
                    name: "Languages",
                    contents: [
                        { name: "Ruby" },
                        { name: "Python" },
                        { name: "Javascript" },
                        { name: "C#" }
                    ]
                },
                {
                    name: "Developer",
                    contents: [
                        { name: "4.2" },
                        { name: "4.3" },
                        { name: "5.0" },
                        { name: "Documentation" }
                    ]
                }
            ]
        },
        {
            name: "opt",
            contents: []
        },
        {
            name: "Users",
            contents: [
                { name: "pavanpodila" },
                { name: "admin" },
                { name: "test-user" }
            ]
        }
    ]
    };
    var height = 300;
    var width = 300;
    var diameter = 960;
    var tree = d3.layout.tree()
    .sort(null)
    .size([height, width - 120])
    .children(function (d) {
        return (!d.contents || d.contents.length === 0) ? null : d.contents;
    });
    var nodes = tree.nodes(treeData);
    var links = tree.links(nodes);
    var svg = d3.select("#pie_chart_1").append("svg")
    .attr("width", diameter)
    .attr("height", diameter - 150)
  .append("g")
    .attr("transform", "translate(" + diameter / 2 + ",50)");
    var link = d3.svg.diagonal()
     .projection(function (d) {
         return [d.y, d.x];
     });
     layoutRoot.selectAll(".link")
     .data(links)
     .enter()
     .append("path")
     .attr("class", "link")
     .attr("d", link);



    var link = svg.selectAll(".link")
  .data(links)
.enter().append("path")
  .attr("class", "link")
  .attr("d", link);
    var nodeGroup = layoutRoot.selectAll(".node")
     .data(nodes)
     .enter()
     .append("g")
     .attr("class", "node")
     .attr("transform", function (d) {
         return "translate(" + d.y + "," + d.x + ")";
     });
     nodeGroup.append("circle")
     .attr("class", "node-dot")
     .attr("r", options.nodeRadius);
     nodeGroup.append("text")
     .attr("text-anchor", function (d) {
         return d.children ? "end" : "start";
     })
     .attr("dx", function (d) {
         var gap = 2 * options.nodeRadius;
         return d.children ? -gap : gap;
     })
     .attr("dy", 3)
     .text(function (d) {
         return d.name;
     });
    }

您当前的(给定)代码有几个问题:

  • 必须删除最后一个}
  • layoutRoot未定义,可能应该svg
  • options是未定义的(但这可能只是代码中缺少的部分)。

这是一个工作小提琴

实际上,您应该能够通过使用控制台并解决显示的问题来解决所有这些问题。