source: Dev/trunk/d3/examples/force/force-interactive.html @ 76

Last change on this file since 76 was 76, checked in by fpvanagthoven, 14 years ago

d3

File size: 3.2 KB
Line 
1<!DOCTYPE html>
2<html>
3  <head>
4    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
5    <title>Force-Directed Graph</title>
6    <script type="text/javascript" src="../../d3.js"></script>
7    <script type="text/javascript" src="../../d3.geom.js"></script>
8    <script type="text/javascript" src="../../d3.layout.js"></script>
9    <style type="text/css">
10
11circle.node {
12  cursor: pointer;
13  stroke: #3182bd;
14  stroke-width: 1.5px;
15}
16
17line.link {
18  fill: none;
19  stroke: #9ecae1;
20  stroke-width: 1.5px;
21}
22
23    </style>
24  </head>
25  <body>
26    <div id="chart"></div>
27    <script type="text/javascript">
28
29var w = 960,
30    h = 500,
31    root;
32
33var force = d3.layout.force()
34    .size([w, h]);
35
36var vis = d3.select("#chart").append("svg:svg")
37    .attr("width", w)
38    .attr("height", h);
39
40d3.json("../data/flare.json", function(json) {
41  root = json;
42  update();
43});
44
45function update() {
46  var nodes = flatten(root),
47      links = d3.layout.tree().links(nodes);
48
49  // Restart the force layout.
50  force
51      .nodes(nodes)
52      .links(links)
53      .start();
54
55  // Update the links

56  var link = vis.selectAll("line.link")
57      .data(links, function(d) { return d.target.id; });
58
59  // Enter any new links.
60  link.enter().insert("svg:line", ".node")
61      .attr("class", "link")
62      .attr("x1", function(d) { return d.source.x; })
63      .attr("y1", function(d) { return d.source.y; })
64      .attr("x2", function(d) { return d.target.x; })
65      .attr("y2", function(d) { return d.target.y; });
66
67  // Exit any old links.
68  link.exit().remove();
69
70  // Update the nodes

71  var node = vis.selectAll("circle.node")
72      .data(nodes, function(d) { return d.id; })
73      .style("fill", color);
74
75  // Enter any new nodes.
76  node.enter().append("svg:circle")
77      .attr("class", "node")
78      .attr("cx", function(d) { return d.x; })
79      .attr("cy", function(d) { return d.y; })
80      .attr("r", function(d) { return Math.sqrt(d.size) / 10 || 4.5; })
81      .style("fill", color)
82      .on("click", click)
83      .call(force.drag);
84
85  // Exit any old nodes.
86  node.exit().remove();
87
88  // Re-select for update.
89  link = vis.selectAll("line.link");
90  node = vis.selectAll("circle.node");
91
92  force.on("tick", function() {
93    link.attr("x1", function(d) { return d.source.x; })
94        .attr("y1", function(d) { return d.source.y; })
95        .attr("x2", function(d) { return d.target.x; })
96        .attr("y2", function(d) { return d.target.y; });
97
98    node.attr("cx", function(d) { return d.x; })
99        .attr("cy", function(d) { return d.y; });
100  });
101}
102
103// Color leaf nodes orange, and packages white or blue.
104function color(d) {
105  return d._children ? "#3182bd" : d.children ? "#c6dbef" : "#fd8d3c";
106}
107
108// Toggle children on click.
109function click(d) {
110  if (d.children) {
111    d._children = d.children;
112    d.children = null;
113  } else {
114    d.children = d._children;
115    d._children = null;
116  }
117  update();
118}
119
120// Returns a list of all nodes under the root.
121function flatten(root) {
122  var nodes = [], i = 0;
123
124  function recurse(node) {
125    if (node.children) node.children.forEach(recurse);
126    if (!node.id) node.id = ++i;
127    nodes.push(node);
128  }
129
130  recurse(root);
131  return nodes;
132}
133
134    </script>
135  </body>
136</html>
Note: See TracBrowser for help on using the repository browser.