我有一个简单的D3图表,但是我遇到了困难,因为节点不会保持在SVG的边界内。我已经给它们设置了高度和宽度,但当节点渲染时,它们超出了所分配的高度和宽度,因此无法看到。
let height = 500;
let width = 960;
this.svg = d3.select("networkView").append("svg")
.attr("width", width)
.attr("height", height);
this.force = d3.layout.force()
.gravity(.05)
.charge(-240)
.linkDistance(50)
.size([width, height]);
let node = this.svg.selectAll(".node")
.data(jsonNodes)
.enter().append("g")
.attr("class", "node")
.call(this.force.drag);
node.append("circle")
.attr("r","10");
node.append("text")
.attr("dx", 0)
.attr("dy", "2.5em")
.text(function(d) { return d.name })
.call(this.force.drag);
this.force
.nodes(jsonNodes)
.start();
this.force.on("tick", function() {
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
});