我不知道为什么D3布局树没有渲染。我想在页面body中时间渲染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;
});
}
答案 0 :(得分:0)
您当前(给定)代码有几个问题:
}
。layoutRoot
未定义,可能应为svg
options
未定义(但可能只是代码中缺少的部分)。实际上,您应该能够通过使用控制台并解决所显示的问题来解决所有这些问题。