forked from john-guerra/flask_d3_example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
105 lines (85 loc) · 2.98 KB
/
index.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
<!DOCTYPE html>
<style>
.states :hover {
fill: red;
}
.states {
stroke: #aaa;
stroke-width: 0.5px;
stroke-linejoin: round;
stroke-linecap: round;
}
.state-borders {
fill: none;
stroke: #aaa;
stroke-width: 0.5px;
stroke-linejoin: round;
stroke-linecap: round;
pointer-events: none;
}
.map-facet {
float:left;
}
</style>
<body>
<h1>US Map by revenue</h1>
<input id="yearSlider" type="range" min="1942" max="2008" value="2008" class="slider" id="myRange"><label id="yearSliderLabel" for="yearSlider">2008</label>
<div id="maps"></div>
<div><a href="https://github.com/john-guerra/flask_d3_example">See the code here</a></div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<script src="static/js/USCloroplethByState.js"></script>
<script>
/* global d3, USCloroplethByState */
// Taken from wikipedia https://en.wikipedia.org/wiki/List_of_U.S._state_abbreviations
var stateCodes = {
"AL":"01", "AK":"02", "AZ":"04", "AR":"05", "CA":"06", "CO":"08", "CT":"09", "DE":"10", "DC":"11", "FL":"12", "GA":"13", "HI":"15", "ID":"16", "IL":"17", "IN":"18", "IA":"19", "KS":"20", "KY":"21", "LA":"22", "ME":"23", "MD":"24", "MA":"25", "MI":"26", "MN":"27", "MS":"28", "MO":"29", "MT":"30", "NE":"31", "NV":"32", "NH":"33", "NJ":"34", "NM":"35", "NY":"36", "NC":"37", "ND":"38", "OH":"39", "OK":"40", "OR":"41", "PA":"42", "RI":"44", "SC":"45", "SD":"46", "TN":"47", "TX":"48", "UT":"49", "VT":"50", "VA":"51", "WA":"53", "WV":"54", "WI":"55", "WY":"56"
};
var attrib = "Total Revenue";
var chart = USCloroplethByState()
.width(600)
.height(450)
.id(function (d) { return d.id; })
.color(function (d) { return d.value; });
// var nest = d3.nest()
// .key(function (d) { return d["Year4"]; });
d3.select("#yearSlider")
.on("input", function () {
d3.select("#yearSliderLabel").text(d3.event.target.value);
update(d3.event.target.value);
})
.on("change", function () {
d3.select("#yearSliderLabel").text(d3.event.target.value);
update(d3.event.target.value);
});
update(2008);
function update(year) {
d3.json("getData/" + year,
function (err, data) {
if (err) throw err;
data.forEach(
function (d) {
// Convert the state name to ansi code;
var state = stateCodes[d["Name"].slice(0,2)];
if (state===undefined) {
return;
}
d.id = state;
d.value = 1.0*+d[attrib]/+d["Population (000)"];
});
// Create an unified color scale and assign it to the chart
var domain = d3.extent(data.map(function (d) { return d.value; }));
chart.colorScale(d3.scaleThreshold()
// A stepped list with 9 elements for the domain
.domain(d3.range(domain[0], domain[1], (domain[1]-domain[0])/9))
.range(d3.schemeBlues[9])
);
d3.select("#maps")
.datum(data)
.call(chart);
}
);
}
</script>
</body>