|
2 | 2 | <html> |
3 | 3 | <head> |
4 | 4 | <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> |
| 5 | + <title>Non-Contiguous Cartogram</title> |
5 | 6 | <script type="text/javascript" src="../../d3.js"></script> |
6 | 7 | <script type="text/javascript" src="../../d3.geo.js"></script> |
7 | 8 | <script type="text/javascript" src="../../d3.geom.js"></script> |
8 | | - <style type="text/css"> |
9 | | - |
10 | | -svg { |
11 | | - width: 960px; |
12 | | - height: 500px; |
13 | | -} |
14 | | - |
15 | | -.black path { |
16 | | - fill: none; |
17 | | - stroke: #ccc; |
18 | | - stroke-width: 3px; |
19 | | -} |
20 | | - |
21 | | -.white path { |
22 | | - fill: #fff; |
23 | | - stroke: #fff; |
24 | | -} |
25 | | - |
26 | | -.grey path { |
27 | | - fill: #ccc; |
28 | | - stroke: #666; |
29 | | -} |
30 | | - |
31 | | - </style> |
| 9 | + <link type="text/css" rel="stylesheet" href="cartogram.css"/> |
32 | 10 | </head> |
33 | 11 | <body> |
34 | | - <script type="text/javascript"> |
35 | | - |
36 | | -var data; // loaded asynchronously |
37 | | - |
38 | | -var svg = d3.select("body") |
39 | | - .append("svg:svg"); |
40 | | - |
41 | | -d3.json("../../data/us-states.json", function(json) { |
42 | | - var path = d3.geo.path(); |
43 | | - |
44 | | - // Synthesize a random data variable to visualize… |
45 | | - // Note: This has a baked-in sqrt transform! |
46 | | - json.features.forEach(function(f) { |
47 | | - f.properties.value = Math.sqrt(.2 + .8 * Math.random()); |
48 | | - }); |
49 | | - |
50 | | - // A thick black stroke for the exterior. |
51 | | - svg.append("svg:g") |
52 | | - .attr("class", "black") |
53 | | - .selectAll("path") |
54 | | - .data(json.features) |
55 | | - .enter().append("svg:path") |
56 | | - .attr("d", path); |
57 | | - |
58 | | - // A white overlay to hide interior black strokes. |
59 | | - svg.append("svg:g") |
60 | | - .attr("class", "white") |
61 | | - .selectAll("path") |
62 | | - .data(json.features) |
63 | | - .enter().append("svg:path") |
64 | | - .attr("d", path); |
65 | | - |
66 | | - // The polygons, scaled! |
67 | | - svg.append("svg:g") |
68 | | - .attr("class", "grey") |
69 | | - .selectAll("path") |
70 | | - .data(json.features) |
71 | | - .enter().append("svg:path") |
72 | | - .attr("transform", function(d) { |
73 | | - var centroid = path.centroid(d), |
74 | | - x = centroid[0], |
75 | | - y = centroid[1]; |
76 | | - return "translate(" + x + "," + y + ")" |
77 | | - + "scale(" + d.properties.value + ")" |
78 | | - + "translate(" + -x + "," + -y + ")"; |
79 | | - }) |
80 | | - .attr("stroke-width", function(d) { return 1 / d.properties.value; }) |
81 | | - .attr("d", path); |
82 | | - |
83 | | -}); |
84 | | - |
85 | | - </script> |
| 12 | + <div id="chart"></div> |
| 13 | + <script type="text/javascript" src="cartogram.js"></script> |
86 | 14 | </body> |
87 | 15 | </html> |
0 commit comments