Javascript D3.js:代码不工作
我是一名中间D3程序员。我偶然发现了一个我不知道的问题。代码对我来说似乎很好。我已经做了两天了,仍然没有找到解决方案 代码: HTML JS:Javascript D3.js:代码不工作,javascript,d3.js,circle-pack,Javascript,D3.js,Circle Pack,我是一名中间D3程序员。我偶然发现了一个我不知道的问题。代码对我来说似乎很好。我已经做了两天了,仍然没有找到解决方案 代码: HTML JS: 我已经发布了HTML/CSS,以防万一。flare.json文件可以在这里找到我认为问题是因为您试图加载flare.json,它可能会导致CORS问题,因此一种方法是您可以将其合并到js中 另一点是,您不需要导入circle-pack.js <script type="text/javascript" src="js/d3/circle-pack.
我已经发布了HTML/CSS,以防万一。
flare.json
文件可以在这里找到我认为问题是因为您试图加载flare.json,它可能会导致CORS问题,因此一种方法是您可以将其合并到js中
另一点是,您不需要导入circle-pack.js
<script type="text/javascript" src="js/d3/circle-pack.js"></script>
.node{
光标:指针;
}
.节点:悬停{
行程:#000;
笔划宽度:1.5px;
}
.node—叶{
填充物:白色;
}
.标签{
字体:11px“Helvetica Neue”,Helvetica,Arial,无衬线;
文本锚定:中间;
文本阴影:01px0#fff,1px0#fff,-1px0#fff,0-1px0#fff;
}
.标签,
.node—根,
.node—叶{
指针事件:无;
}
圆形填料
你的问题是什么……它是断裂还是没有画出来?它不是画出来的。我只看到指定背景颜色的银行页面。在浏览器控制台上,您会看到错误。您可以在问题部分也发布该错误。因为我已经复制了你的代码,它工作得很好…我认为这是一个CORS问题(对于flare.json),但只是从你的浏览器控制台确认这一点。嗯,我没有想到CORS。我在Firefox上运行它,到目前为止它还没有给我一个CORS问题。如果我在服务器上运行它,它会发生吗?不,它不会发生在服务器上。它会发生在您在没有服务器的情况下在本地运行并尝试读取本地文件时
.node {
cursor: pointer;
}
.node:hover {
stroke: #000;
stroke-width: 1.5px;
}
.node--leaf {
fill: white;
}
.label {
font: 11px "Helvetica Neue", Helvetica, Arial, sans-serif;
text-anchor: middle;
text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff, 0 -1px 0 #fff;
}
.label,
.node--root,
.node--leaf {
pointer-events: none;
}
var margin = 20,
diameter = 960;
var color = d3.scale.linear()
.domain([-1, 5])
.range(["hsl(152,80%,80%)", "hsl(228,30%,40%)"])
.interpolate(d3.interpolateHcl);
var pack = d3.layout.pack()
.padding(2)
.size([diameter - margin, diameter - margin])
.value(function(d) { return d.size; })
var svg = d3.select("body").append("svg")
.attr("width", diameter)
.attr("height", diameter)
.append("g")
.attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");
d3.json("flare.json", function(error, root) {
if (error) throw error;
var focus = root,
nodes = pack.nodes(root),
view;
var circle = svg.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("class", function(d) { return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"; })
.style("fill", function(d) { return d.children ? color(d.depth) : null; })
.on("click", function(d) { if (focus !== d) zoom(d), d3.event.stopPropagation(); });
var text = svg.selectAll("text")
.data(nodes)
.enter().append("text")
.attr("class", "label")
.style("fill-opacity", function(d) { return d.parent === root ? 1 : 0; })
.style("display", function(d) { return d.parent === root ? null : "none"; })
.text(function(d) { return d.name; });
var node = svg.selectAll("circle,text");
d3.select("body")
.style("background", color(-1))
.on("click", function() { zoom(root); });
zoomTo([root.x, root.y, root.r * 2 + margin]);
function zoom(d) {
var focus0 = focus; focus = d;
var transition = d3.transition()
.duration(d3.event.altKey ? 7500 : 750)
.tween("zoom", function(d) {
var i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2 + margin]);
return function(t) { zoomTo(i(t)); };
});
transition.selectAll("text")
.filter(function(d) { return d.parent === focus || this.style.display === "inline"; })
.style("fill-opacity", function(d) { return d.parent === focus ? 1 : 0; })
.each("start", function(d) { if (d.parent === focus) this.style.display = "inline"; })
.each("end", function(d) { if (d.parent !== focus) this.style.display = "none"; });
}
function zoomTo(v) {
var k = diameter / v[2]; view = v;
node.attr("transform", function(d) { return "translate(" + (d.x - v[0]) * k + "," + (d.y - v[1]) * k + ")"; });
circle.attr("r", function(d) { return d.r * k; });
}
});
d3.select(self.frameElement).style("height", diameter + "px");
<script type="text/javascript" src="js/d3/circle-pack.js"></script>