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

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

d3

File size: 2.6 KB
Line 
1<!DOCTYPE html>
2<html>
3  <head>
4    <title>Force-Directed Map</title>
5    <script type="text/javascript" src="../../d3.js"></script>
6    <script type="text/javascript" src="../../d3.geo.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
11path {
12  fill: #ddd;
13  fill-opacity: .8;
14  stroke: #fff;
15  stroke-width: 1.5px;
16}
17
18line {
19  stroke: #999;
20}
21
22    </style>
23  </head>
24  <body>
25    <script type="text/javascript">
26
27var w = 960,
28    h = 500;
29
30var path = d3.geo.path(),
31    force = d3.layout.force().size([w, h]);
32
33var svg = d3.select("body").append("svg:svg")
34    .attr("width", w)
35    .attr("height", h);
36
37d3.json("../data/us-states.json", function(states) {
38  var nodes = [],
39      links = [];
40
41  states.features.forEach(function(d, i) {
42    if (d.id == "02" || d.id == "15" || d.id == "72") return;
43    var centroid = path.centroid(d);
44    centroid.x = centroid[0];
45    centroid.y = centroid[1];
46    centroid.feature = d;
47    nodes.push(centroid);
48  });
49
50  d3.geom.delaunay(nodes).forEach(function(d) {
51    links.push(edge(d[0], d[1]));
52    links.push(edge(d[1], d[2]));
53    links.push(edge(d[2], d[0]));
54  });
55
56  force
57      .gravity(0)
58      .charge(0)
59      .nodes(nodes)
60      .links(links)
61      .linkDistance(function(d) { return d.distance; })
62      .start();
63
64  var link = svg.selectAll("line")
65      .data(links)
66    .enter().append("svg:line")
67      .attr("x1", function(d) { return d.source.x; })
68      .attr("y1", function(d) { return d.source.y; })
69      .attr("x2", function(d) { return d.target.x; })
70      .attr("y2", function(d) { return d.target.y; });
71
72  var node = svg.selectAll("g")
73      .data(nodes)
74    .enter().append("svg:g")
75      .attr("transform", function(d) { return "translate(" + -d.x + "," + -d.y + ")"; })
76      .call(force.drag)
77    .append("svg:path")
78      .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
79      .attr("d", function(d) { return path(d.feature); });
80
81  force.on("tick", function(e) {
82    link.attr("x1", function(d) { return d.source.x; })
83        .attr("y1", function(d) { return d.source.y; })
84        .attr("x2", function(d) { return d.target.x; })
85        .attr("y2", function(d) { return d.target.y; });
86
87    node.attr("transform", function(d) {
88      return "translate(" + d.x + "," + d.y + ")";
89    });
90  });
91});
92
93function edge(a, b) {
94  var dx = a[0] - b[0], dy = a[1] - b[1];
95  return {
96    source: a,
97    target: b,
98    distance: Math.sqrt(dx * dx + dy * dy)
99  };
100}
101
102    </script>
103  </body>
104</html>
Note: See TracBrowser for help on using the repository browser.