-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3_map.html
102 lines (82 loc) · 1.89 KB
/
d3_map.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
101
102
<!DOCTYPE html>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v2.min.js?2.9.6"></script>
<style>
.background {
fill: none;
pointer-events: all;
}
#districts {
fill: #aaa;
stroke: #fff;
stroke-width: 0.5px;
}
#districts .active {
fill: steelblue;
strok
}
#states {
stroke: #000;
stroke-width: 1.5px;
}
</style>
<body>
<script>
var width = 960,
height = 500,
centered;
var projection = d3.geo.albersUsa()
.scale(width)
.translate([0, 0]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", click);
var g = svg.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
.append("g")
.attr("id", "districts");
d3.json("./data/us_districts.json", function(districts) {
g.selectAll("path")
.data(districts.features)
.enter().append("path")
.attr("id", function(d){console.log(d); return d.properties.Dist2;})
.attr("d", path)
.on("click", click);
d3.json("./data/us_states.json", function(states) {
g.append("g")
.attr("id", "states")
.selectAll("path")
.data(states.features)
.enter().append("path")
.attr("d",path)
.attr("fill","none")
});
});
function click(d) {
var x = 0,
y = 0,
k = 1;
if (d && centered !== d) {
var centroid = path.centroid(d);
x = -centroid[0];
y = -centroid[1];
k = 4;
centered = d;
} else {
centered = null;
}
g.selectAll("path")
.classed("active", centered && function(d) { return d === centered; });
g.transition()
.duration(1000)
.attr("transform", "scale(" + k + ")translate(" + x + "," + y + ")")
.style("stroke-width", 1.5 / k + "px");
}
</script>