forked from devsar/d3talk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcartogram.html
100 lines (86 loc) · 2.53 KB
/
cartogram.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" src="d3/d3.js"></script>
<script type="text/javascript" src="d3/d3.geo.js"></script>
<script type="text/javascript" src="d3/d3.geom.js"></script>
<link type="text/css" rel="stylesheet" href="style.css"/>
<style type="text/css">
.black path {
fill: none;
stroke: #ccc;
stroke-width: 3px;
}
.white path {
fill: #fff;
stroke: #fff;
}
.grey path {
fill: #bbb;
stroke: #666;
}
</style>
</head>
<body>
<div id="body">
<div id="footer">
U.S. Adult Obesity, 2008
<div class="hint">source: CDC</div>
</div>
</div>
<script type="text/javascript">
// Ratio of Obese (BMI >= 30) in U.S. Adults, CDC 2008
var data = [
, .187, .198, , .133, .175, .151, , .1, .125, .171, , .172, .133, , .108,
.142, .167, .201, .175, .159, .169, .177, .141, .163, .117, .182, .153, .195,
.189, .134, .163, .133, .151, .145, .13, .139, .169, .164, .175, .135, .152,
.169, , .132, .167, .139, .184, .159, .14, .146, .157, , .139, .183, .16, .143
];
var formatPercent = d3.format(".1%");
var svg = d3.select("#body").append("svg:svg")
.attr("width", 1280)
.attr("height", 800);
var path = d3.geo.path()
.projection(d3.geo.albersUsa()
.scale(1400)
.translate([680, 360]));
d3.json("us-states.json", function(json) {
// A thick black stroke for the exterior.
svg.append("svg:g")
.attr("class", "black")
.selectAll("path")
.data(json.features)
.enter().append("svg:path")
.attr("d", path);
// A white overlay to hide interior black strokes.
svg.append("svg:g")
.attr("class", "white")
.selectAll("path")
.data(json.features)
.enter().append("svg:path")
.attr("d", path);
// The polygons, scaled!
svg.append("svg:g")
.attr("class", "grey")
.selectAll("path")
.data(json.features)
.enter().append("svg:path")
.attr("transform", function(d) {
var centroid = path.centroid(d),
x = centroid[0],
y = centroid[1];
return "translate(" + x + "," + y + ")"
+ "scale(" + Math.sqrt(data[+d.id] * 5 || 0) + ")"
+ "translate(" + -x + "," + -y + ")";
})
.style("stroke-width", function(d) {
return 1 / Math.sqrt(data[+d.id] * 5);
})
.attr("d", path)
.append("svg:title")
.text(function(d) { return d.properties.name + ": " + formatPercent(data[+d.id]); });
});
</script>
</body>
</html>