我正在开发一个涉及D3库的JavaScript项目,并尝试使用此代码https://gist.github.com/mbostock/4063269制作气泡图。在我的文件中,我创建了一个JSON对象,它是我想要放入函数的JSON对象
d3.json("testfin.json", function(error, root) {
if (error) throw error;
var node = svg.selectAll(".node")
.data(bubble.nodes(classes(root))
.filter(function(d) { return !d.children; }))
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
node.append("title")
.text(function(d) { return d.className + ": " + format(d.value); });
node.append("circle")
.attr("r", function(d) { return d.r; })
.style("fill", function(d) { return color(d.packageName); });
node.append("text")
.attr("dy", ".3em")
.style("text-anchor", "middle")
.text(function(d) { return d.className.substring(0, d.r / 3); });
});
我想以某种方式把我的JSON对象放在“testfin.json”去制作气泡图。我怎样才能在JavaScript中执行此操作?
JSON对象已经具有该函数的正确格式。
你能帮助我用我的JSON对象创建一个文件,然后从文件中读取或绕过testfin.json并让函数接受JSON对象吗?
答案 0 :(得分:0)
您需要在主机上访问JSON文件。如果它位于主机的根目录,那么d3.json("testfin.json")
应该可以工作,或者正确的路径将取决于您的文件夹结构。我建议您下载要点https://gist.github.com/mbostock/4063269/download并查看它在您所引用的示例中的工作原理。