-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonoma2012.html
executable file
·181 lines (150 loc) · 4.86 KB
/
sonoma2012.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<meta charset="utf-8">
<style>
/* CSS goes here. */
.counties {
fill: none;
stroke: #000000;
stroke-width: 2.5px;
}
body {
font-family: Arial, sans-serif;
}
.legend {
font-size: 12px;
}
div.tooltip {
position: absolute;
text-align: center;
width: 150px;
height: 25px;
padding: 5px;
font-size: 12px;
background: #e5f5f9;
border: 2px;
border-radius: 8px;
pointer-events: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
<script>
// set width and height
var width = 600,
height = 600;
// create projection
var projection = d3.geo.albers()
.translate([width / 2, height / 2])
.parallels([54, 40.5])
.rotate([123, -0.8])
.center([0, 37.7750])
.scale(20500);
// create path
var path = d3.geo.path()
.projection(projection);
// create svg
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
// Setting color domains(intervals of values) for our map
var color_domain = [-50, 0.89374793287022, 3.4456745357533, 6.69421221864952, 10.8480010064288, 15.7158374293162, 22.54363601157, 33.2792996917682, 70.8635345847554];
var color = d3.scale.threshold()
.domain(color_domain)
.range(["#ffffff","#ffffe5", "#f7fcb9", "#d9f0a3", "#addd8e", "#78c679", "#41ab5d","#238443","#006837","#004529"]);
//Adding legend for our Choropleth
var ext_color_domain = [-100, 0, 2, 4, 8, 12, 17, 25, 40, 80];
var legend_labels = ["missing value","< 0.8", "0.8 - 3.4", "3.4 - 6.6", "6.6 - 10.8", "10.8 - 15.7", "15.7 - 22.5","22.5 - 33.2","33.2 - 70.8", "> 70.8"];
var legend = svg.selectAll("g.legend")
.data(ext_color_domain)
.enter().append("g")
.attr("class", "legend");
var ls_w = 20, ls_h = 20;
legend.append("rect")
.attr("x", 20)
.attr("y", function(d, i){ return height - (i*ls_h) - 2*ls_h;})
.attr("width", ls_w)
.attr("height", ls_h)
.style("fill", function(d, i) { return color(d); })
.style("opacity", 0.8);
legend.append("text")
.attr("x", 50)
.attr("y", function(d, i){ return height - (i*ls_h) - ls_h - 4;})
.text(function(d, i){ return legend_labels[i]; });
legend.append("text")
.attr("class", "caption")
.attr("x", 20)
.attr("y", height - (10*ls_h) - 2*ls_h)
.text("Watt/population per zipcode");
// download resources async
queue()
.defer(d3.json, "output/ca.topo.json")
.defer(d3.csv, "output/trend_2012_on_population.csv")
.await(ready);
// Start of Choropleth drawing
function ready(error, map, data) {
if (error) throw error;
var ratioByZip = {};
data.forEach(function(d) {
ratioByZip[d.ZIPCODE] = +d.RATIO;
});
// drawing choropleth
svg.append("g")
.attr("class", "zipcodes")
.selectAll("path")
.data(topojson.feature(map, map.objects.tl_2010_06_zcta510).features)
.enter().append("path")
.attr("d", path)
.style("fill", function(d) {
value = ratioByZip[d.id];
// it is a zipcode present in the shp file, but we don't have any value
if(typeof value === 'undefined'){
value = 0;
};
return color(value);
}).style("opacity", 0.8)
//Adding mouseevents
.on("mouseover", function(d) {
value = ratioByZip[d.id];
// it is a zipcode present in the shp file, but we don't have any value
if(typeof value === 'undefined'){
value = 0;
};
d3.select(this).transition().duration(300).style("opacity", 1);
div.transition().duration(300).style("opacity", 1)
div.html("zip "+d.id+ ":<br/> <strong>" + formatNumber(value)+"</strong> (Watt/pop)")
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY -50) + "px");
})
.on("mouseout", function() {
d3.select(this)
.transition().duration(300)
.style("opacity", 0.8);
div.transition().duration(300)
.style("opacity", 0);
})
// county borders
svg.append("path")
.datum(topojson.mesh(map, map.objects.counties, function(a, b) {
if(a.properties.COUNTY == "Sonoma County") return true;
return a !== b;
}))
.attr("class", "counties")
.attr("d", path);
};
// stuff for tooltip
var formatNumber = d3.format(".3n");
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-357760-27', 'auto');
ga('send', 'pageview');
</script>