我稍微研究了一下你的代码,发现了几个潜在的问题:
根据您运行此程序的位置/方式,加载flare.json 文件可能会导致 CORS 错误(例如,如果您尝试链接到 bostock 的 github 上的文件)。您可以将 json 对象直接嵌入到“flare”变量中,然后只需将 d3.json() 方法中的代码移动到脚本底部,并将根变量分配给flare。
var flare = ...paste contents of flare.json here... ;
您的 javascript 方法是按照在定义方法之前引用方法的顺序定义的(您应该将缩放和单击方法移到更新方法之前):
collapse(d){...}
zoom(){...}
click(){...}
update(){...}
您的缩放方法引用了“h”、“w”和“m”,它们在任何地方都没有定义。我认为它们应该是宽度、高度和边距:
var zoom = function() {
var scale = d3.event.scale,
translation = d3.event.translate,
tbound = -height * scale,
bbound = height * scale,
lbound = (-width + margin.right) * scale,
rbound = (width - margin.left) * scale;
最后,要修复缩放问题,您应该像这样修复缩放方法:
var zoom = function() {
var scale = d3.event.scale,
translation = d3.event.translate,
tbound = -height * scale,
bbound = height * scale,
lbound = (-width + margin.right) * scale,
rbound = (width - margin.left) * scale;
// limit translation to thresholds
translation = [
Math.max(Math.min(translation[0], rbound), lbound),
Math.max(Math.min(translation[1], bbound), tbound)
];
svg.attr("transform", "translate(" + translation + ")" + " scale(" + scale + ")");
}
请注意,我将“select(”.drawArea”)”替换为“svg”。因为您想要转换顶部的 g 元素(即“svg”变量中的内容)。
仅供参考 - 我无法真正在 plnkr 或 jsFiddle 中实现此功能,因为由于 plnkr 和 jsFiddle 中处理变量/函数作用域的方式,存在奇怪的 d3 错误(请参阅here)。这可能是其他人可以解决的问题。但这是完整的代码(减去嵌入的flare.json变量)。我在自己的 Tomcat 容器中运行了这段代码,可以双击树并将其放大。您可能想要尝试缩放和平移,以便树不会绘制在可见窗口的边界之外(现在)如果双击它会缩放,但会居中,因此树的左侧不在浏览器窗口的一侧)。
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg { pointer-events: all; }
.node { cursor: pointer; }
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 1.5px;
}
tree { pointer-events: all; }
.node text { font: 12px sans-serif; }
.link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 120, bottom: 20, left: 120},
width = 1500 - margin.right - margin.left,
height = 800 - margin.top - margin.bottom;
var i = 0,
duration = 750,
root;
var tree = d3.layout.tree()
.size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
d3.select(self.frameElement).style("height", "800px");
var collapse = function(d) {
if (d.children) {
d._children = d.children;
d._children.forEach(collapse);
d.children = null;
}
}
var zoom = function() {
var scale = d3.event.scale,
translation = d3.event.translate,
tbound = -height * scale,
bbound = height * scale,
lbound = (-width + margin.right) * scale,
rbound = (width - margin.left) * scale;
// limit translation to thresholds
translation = [
Math.max(Math.min(translation[0], rbound), lbound),
Math.max(Math.min(translation[1], bbound), tbound)
];
svg.attr("transform", "translate(" + translation + ")" + " scale(" + scale + ")");
}
// Toggle children on click.
var click = function(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
};
var update = function(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function(d) { d.y = d.depth * 180; });
// Update the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
.on("click", click);
nodeEnter.append("circle")
.attr("r", 1e-6)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
nodeEnter.append("text")
.attr("x", function(d) { return d.children || d._children ? -10 : 10; })
.attr("dy", ".35em")
.attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
.text(function(d) { return d.name; })
.style("fill-opacity", 1e-6);
// Transition nodes to their new position.
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
nodeUpdate.select("circle")
.attr("r", 4.5)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
nodeUpdate.select("text")
.style("fill-opacity", 1);
// Transition exiting nodes to the parent's new position.
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
.remove();
nodeExit.select("circle")
.attr("r", 1e-6);
nodeExit.select("text")
.style("fill-opacity", 1e-6);
// Update the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; });
// Enter any new links at the parent's previous position.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function(d) {
var o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
});
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal);
// Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function(d) {
var o = {x: source.x, y: source.y};
return diagonal({source: o, target: o});
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
d3.select("svg")
.call(d3.behavior.zoom()
.scaleExtent([0.5, 5])
.on("zoom", zoom));
};
d3.json("flare.json", function(json){
root = json;
root.x0 = height / 2;
root.y0 = 0;
root.children.forEach(collapse);
update(root);
});
</script>