我试图让我的d3构建在其内部约束div而没有成功。这是我到目前为止所拥有的 -
var width = 960,
height = 570,
root;
var force = d3.layout.force()
.linkDistance(120)
.charge(-200)
.gravity(.05)
.size([width, height])
.on("tick", tick);
var svg = d3.select("#powerchart").append("svg:svg")
.attr("width", width)
.attr("height", height);
var link = svg.selectAll(".link"),
node = svg.selectAll(".node");
d3.json('/assets/images/graph.json', function(error, json) {
root = json;
update();
});
function update() {
var nodes = flatten(root),
links = d3.layout.tree().links(nodes);
// Restart the force layout.
force
.nodes(nodes)
.links(links)
.start();
// Update links.
link = link.data(links, function(d) { return d.target.id; });
link.exit().remove();
link.enter().insert("line", ".node")
.attr("class", "link");
// Update nodes.
node = node.data(nodes, function(d) { return d.id; });
node.exit().remove();
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.on("click", click)
.call(force.drag);
nodeEnter.append("circle")
.attr("r", 1)
.transition()
.delay(function(d, i) { return i*60; })// <-- delay as a function of i
.attr("r", function(d) { return Math.sqrt(d.size) / 2 || 25; });
nodeEnter.append("text")
.transition()
.delay(function(d, i) { return i*60; })// <-- delay as a function of i
.attr("dy", ".35em")
.text(function(d) { return d.name; });
node.select("circle")
.style("fill", color);
}
function tick() {
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("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
}
function color(d) {
return d._children ? "#ffffff" // collapsed package
: d.children ? "#ffffff" // expanded package
: "#c9c9c9"; // leaf node
}
// Toggle children on click.
function click(d) {
if (d3.event.defaultPrevented) return; // ignore drag
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update();
updateSide(d);
}
// Returns a list of all nodes under the root.
function flatten(root) {
var nodes = [], i = 0;
function recurse(node) {
if (node.children) node.children.forEach(recurse);
if (!node.id) node.id = ++i;
nodes.push(node);
}
recurse(root);
return nodes;
}
function updateSide(node){
$("#nodeHead").empty();
$("#nodeEmailFirst").empty();
$("#nodeEmailLast").empty();
$("#nodeHead").append(node.name);
$("#nodeEmailFirst").append(node.firstEmail + " years ago");
$("#nodeEmailLast").append(node.lastEmail + " years ago");
}
我想知道我会将这些约束到父div?