UNPKG

d3

Version:

A small, free JavaScript library for manipulating documents based on data.

140 lines (118 loc) 3.5 kB
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Node-Link Tree</title> <script type="text/javascript" src="../../d3.js"></script> <script type="text/javascript" src="../../d3.layout.js"></script> <style type="text/css"> circle.node { cursor: pointer; fill: #fff; stroke: steelblue; stroke-width: 1.5px; } path.link { fill: none; stroke: #ccc; stroke-width: 1.5px; } </style> </head> <body> <div id="chart"></div> <script type="text/javascript"> var w = 960, h = 2000, i = 0, duration = 500, root; var tree = d3.layout.tree() .size([h, w - 160]); var diagonal = d3.svg.diagonal() .projection(function(d) { return [d.y, d.x]; }); var vis = d3.select("#chart").append("svg:svg") .attr("width", w) .attr("height", h) .append("svg:g") .attr("transform", "translate(40,0)"); d3.json("../data/flare.json", function(json) { json.x0 = 800; json.y0 = 0; update(root = json); }); function update(source) { // Compute the new tree layout. var nodes = tree.nodes(root).reverse(); // Update the nodes… var node = vis.selectAll("circle.node") .data(nodes, function(d) { return d.id || (d.id = ++i); }); // Enter any new nodes at the parent's previous position. node.enter().append("svg:circle") .attr("class", "node") .attr("cx", function(d) { return source.y0; }) .attr("cy", function(d) { return source.x0; }) .attr("r", 4.5) .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; }) .on("click", click) .transition() .duration(duration) .attr("cx", function(d) { return d.y; }) .attr("cy", function(d) { return d.x; }); // Transition nodes to their new position. node.transition() .duration(duration) .attr("cx", function(d) { return d.y; }) .attr("cy", function(d) { return d.x; }) .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; }); // Transition exiting nodes to the parent's new position. node.exit().transition() .duration(duration) .attr("cx", function(d) { return source.y; }) .attr("cy", function(d) { return source.x; }) .remove(); // Update the links… var link = vis.selectAll("path.link") .data(tree.links(nodes), function(d) { return d.target.id; }); // Enter any new links at the parent's previous position. link.enter().insert("svg:path", "circle") .attr("class", "link") .attr("d", function(d) { var o = {x: source.x0, y: source.y0}; return diagonal({source: o, target: o}); }) .transition() .duration(duration) .attr("d", diagonal); // 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; }); } // Toggle children on click. function click(d) { if (d.children) { d._children = d.children; d.children = null; } else { d.children = d._children; d._children = null; } update(d); } </script> </body> </html>