我正在设置一个带有标签和缩放功能的力导向D3图。一切正常,直到我尝试缩放它会引发以下错误:
d3.v4.js:7637 Error: Invalid value for <svg> attribute transform="translate(undefined) scale(undefined)"(anonymous function) @ d3.v4.js:7637selection_each @ d3.v4.js:7616selection_attr @ d3.v4.js:7673(anonymous function) @ (index):36dispatch.apply @ d3.v4.js:4536customEvent @ d3.v4.js:7280Gesture.emit @ d3.v4.js:12207Gesture.zoom @ d3.v4.js:12195wheeled @ d3.v4.js:12239(anonymous function) @ d3.v4.js:7204
贝娄是d3代码,我们非常感谢任何建议:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<script src="https://d3js.org/d3.v4.js"></script>
</head>
<style>
.node text {
pointer-events: none;
font: 10px sans-serif;
}
.links line {
stroke-opacity: 0.9;
}
.nodes circle {
stroke: #fff;
stroke-width: 5.5px;
}
</style>
<body>
<svg width="1000" height="800"></svg>
<script>
var svg = d3.select("svg").call(d3.zoom().on("zoom", function () {
svg.attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")")
})),
width = +svg.attr("width"),
height = +svg.attr("height");
var color = d3.scaleOrdinal(d3.schemeCategory20);
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
d3.json("static/graphFile.json", function(error, graph) {
if (error) throw error;
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(graph.links)
.enter().append("line")
.attr("stroke", function(d) { return color(d.value); })
.attr("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.id });
node.append("circle").attr("r", 10)
.attr("fill", function(d) { return color(d.group); })
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
function ticked() {
link
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
node
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
}
});
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
</script>
</body>
</html>
答案 0 :(得分:1)
当您在D3图表上设置边界时,显然无法缩放。我通过删除那些边界解决了这个问题。 (致记自:Jeremy Fields)
var svg = d3.select("svg").call(d3.zoom().on("zoom", function () {svg.attr("transform", d3.event.transform)})).append("g"),
width = +svg.attr("width"),
height = +svg.attr("height");