-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
171 lines (155 loc) · 4.19 KB
/
script.js
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
/* global d3, topojson */
/* eslint-disable max-len */
// eslint-disable-next-line no-unused-vars
const projectName = "choropleth";
// coded by @yellowflash2041 (github)
// Define body
var body = d3.select("body");
var svg = d3.select("svg");
// Define the div for the tooltip
var tooltip = body
.append("div")
.attr("class", "tooltip")
.attr("id", "tooltip")
.style("opacity", 0);
var path = d3.geoPath();
var x = d3.scaleLinear().domain([2.6, 75.1]).rangeRound([600, 860]);
var color = d3
.scaleThreshold()
.domain(d3.range(2.6, 75.1, (75.1 - 2.6) / 8))
.range(d3.schemeGreens[9]);
var g = svg
.append("g")
.attr("class", "key")
.attr("id", "legend")
.attr("transform", "translate(0,40)");
g.selectAll("rect")
.data(
color.range().map(function (d) {
d = color.invertExtent(d);
if (d[0] === null) {
d[0] = x.domain()[0];
}
if (d[1] === null) {
d[1] = x.domain()[1];
}
return d;
})
)
.enter()
.append("rect")
.attr("height", 8)
.attr("x", function (d) {
return x(d[0]);
})
.attr("width", function (d) {
return d[0] && d[1] ? x(d[1]) - x(d[0]) : x(null);
})
.attr("fill", function (d) {
return color(d[0]);
});
g.append("text")
.attr("class", "caption")
.attr("x", x.range()[0])
.attr("y", -6)
.attr("fill", "#000")
.attr("text-anchor", "start")
.attr("font-weight", "bold");
g.call(
d3
.axisBottom(x)
.tickSize(13)
.tickFormat(function (x) {
return Math.round(x) + "%";
})
.tickValues(color.domain())
)
.select(".domain")
.remove();
const EDUCATION_FILE =
"https://raw.githubusercontent.com/no-stack-dub-sack/testable-projects-fcc/master/src/data/choropleth_map/for_user_education.json";
const COUNTY_FILE =
"https://raw.githubusercontent.com/no-stack-dub-sack/testable-projects-fcc/master/src/data/choropleth_map/counties.json";
Promise.all([d3.json(COUNTY_FILE), d3.json(EDUCATION_FILE)])
.then((data) => ready(data[0], data[1]))
.catch((err) => console.log(err));
function ready(us, education) {
svg
.append("g")
.attr("class", "counties")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.enter()
.append("path")
.attr("class", "county")
.attr("data-fips", function (d) {
return d.id;
})
.attr("data-education", function (d) {
var result = education.filter(function (obj) {
return obj.fips === d.id;
});
if (result[0]) {
return result[0].bachelorsOrHigher;
}
// could not find a matching fips id in the data
console.log("could find data for: ", d.id);
return 0;
})
.attr("fill", function (d) {
var result = education.filter(function (obj) {
return obj.fips === d.id;
});
if (result[0]) {
return color(result[0].bachelorsOrHigher);
}
// could not find a matching fips id in the data
return color(0);
})
.attr("d", path)
.on("mouseover", function (event, d) {
tooltip.style("opacity", 0.9);
tooltip
.html(function () {
var result = education.filter(function (obj) {
return obj.fips === d.id;
});
if (result[0]) {
return (
result[0]["area_name"] +
", " +
result[0]["state"] +
": " +
result[0].bachelorsOrHigher +
"%"
);
}
// could not find a matching fips id in the data
return 0;
})
.attr("data-education", function () {
var result = education.filter(function (obj) {
return obj.fips === d.id;
});
if (result[0]) {
return result[0].bachelorsOrHigher;
}
// could not find a matching fips id in the data
return 0;
})
.style("left", event.pageX + 10 + "px")
.style("top", event.pageY - 28 + "px");
})
.on("mouseout", function () {
tooltip.style("opacity", 0);
});
svg
.append("path")
.datum(
topojson.mesh(us, us.objects.states, function (a, b) {
return a !== b;
})
)
.attr("class", "states")
.attr("d", path);
}