-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWeek3Map
176 lines (158 loc) · 4.24 KB
/
Week3Map
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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
stroke-linejoin: round;
}
.points{
fill:black;
}
.land {
fill: #ddd;
}
.states {
stroke: #222222;
}
.legendEntry {
font-size: 12px;
}
.counties {
fill: none;
stroke: black;
stroke-width: 0.5px;
}
</style>
<body>
<h1>Average Annual Income by State 2014</h1>
<p> Data comes from the <a href=" http://kff.org/other/state-indicator/median-annual-income/", target="_blank">Kaiser Family Foundation</a></p>
<script src="http://d3js.org/d3.v3.js"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 1000,
height = 500;
//Data on 2014 median annaul income taken from the Kaiser Family Foundation, http://kff.org/other/state-indicator/median-annual-income/
var data=[{"16":53438, //ID
"53":59068, //WA
"27":67244, //MN
"38":60730, //ND
"23":51710, //ME
"26":52005,//Michigan
"55":58080,//WI
"30":51102,//MT
"41":58875, //OR
"46":53053, //SD
"33":73397, //NH
"50":50708, //VT
"36":54310, //NY
"56":55690,//WO
"19":57810,//IA
"31":56870,//NE
"25":63151,//MA
"17":54916,//IL
"42":55173,//PA
"9":70161,//CT
"44":58633,//RI
"6":60487,//CA
"49":63383,//UT
"32":49875,//NV
"39":49644,//OH
"18":48060,//IN
"34":65243,//NJ
"8":60940, //CO
"54":39552,//WV
"29":56630,//MO
"20":53444,//KS
"10":57522,//DE
"24":76165,//MD
"21":42786,//KY
"51":66155,//VA
"11":68277,//DC?
"4":49254,//AZ
"40":47199,//OK
"35":46686,//NM
"47":43716,//TN
"37":46784,//NC
"48":53875,//TX
"5":44922, //AR
"45":44929,//SC
"1":42278,//AL
"13":49555,//GA
"28":35521,//MS
"22":42406,//LA also?
"12":46140,//FL
"15":71223, //HI
"2":67629//,// AK
//"72":19518//PR THIS DATA FROM U.S. CENSUS http://www.census.gov/newsroom/press-releases/2014/cb14-17.html Removed because
//outliar status skewed results
}];//?guam? ?samoa? //removed because skewed results and also too hard to see. also no 2014 data could be found
var projection = d3.geo.albers();
var path = d3.geo.path()
.projection(projection)
.pointRadius(1);
var color_scale = d3.scale.quantize()
.domain(d3.values(data[0]).sort(d3.ascending))
.range(["#bae4bc","#7bccc4","#43a2ca","#0868ac"]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
queue()
.defer(d3.json, "us.json")
.await(ready);
function ready(error, us) {
svg.append("path")
.datum(topojson.feature(us, us.objects.land))
.attr("class", "land")
.attr("d", path);
svg.append("g")
.attr("class", "states")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter()
.append("path")
.attr("d", path)
.style("fill", function(d){return color_scale(data[0][d.id]);});
svg.append("path")
.attr("class", "counties")
//.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.enter()
.append("path")
.attr("d", path)
.style("fill", function(d){return color_scale(data[0][d.id]);});
//svg.append("path")
// .data(topojson.feature(us, us.objects.counties).features)
// .enter()
//.attr("class", "counties")
//.attr("d", function(d) { return "M" + d.map(function(d) { return d.join("L"); }).join("ZM") + "Z"; });
var legend = svg.selectAll('g.legendEntry')
.data(color_scale.range().reverse())
.enter()
.append('g').attr('class', 'legendEntry');
legend
.append('rect')
.attr("x", width - 985)
.attr("y", function(d, i) {
return i * 20;
})
.attr("width", 10)
.attr("height", 10)
.style("stroke", "black")
.style("stroke-width", 1)
.style("fill", function(d){return d;});
myformat = d3.format(',')
legend
.append('text')
.attr("x", width - 965) //leave 5 pixel space after the <rect>
.attr("y", function(d, i) {
return i * 20;
})
.attr("dy", "0.8em") //place text one line *below* the x,y point
.text(function(d,i) {
var extent = color_scale.invertExtent(d);
return "$" + myformat(Math.round(extent[0])) + " - $" + myformat(Math.round(extent[1]));
});
}
</script>
</body>
</html>