-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
219 lines (189 loc) · 9.12 KB
/
script.js
File metadata and controls
219 lines (189 loc) · 9.12 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// SETUP
// select the container in which to include the data visualization and begin by appending a title and a description
const container = d3.select("div.container");
container
.append("h1")
.attr("id", "title")
.text("US Educational Attainment");
container
.append("h3")
.attr("id", "description")
.text("Bachelor's degree or higher 2010-2014");
// include a div for the tooltip
// text is included in the two paragraphs appended to the container
const tooltip = container
.append("div")
.attr("id", "tooltip");
tooltip
.append("p")
.attr("class", "area");
tooltip
.append("p")
.attr("class", "education");
// for the SVG, define an object with the margins, used to nest the SVG content safe inside the SVG boundaries
// as there is no need for axis, the margin is used to safely draw the legend and the overall visualization
const margin = {
top: 20,
right: 20,
bottom: 20,
left: 20
}
// define and append an SVG element
const width = 800 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
const svgContainer = container
.append("svg")
.attr("viewBox", `0 0 ${width + margin.left + margin.right} ${height + margin.top + margin.bottom}`);
// define the group element nested inside the SVG, in which to actually plot the map
const svgCanvas = svgContainer
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
// create an object for the values of the legend
// defining the percentages and the matching color for the fill of the rectangle elements
const legendValues = {
percentage: [3, 12, 21, 30, 39, 48, 57, 66],
color: ["#E5F5E0", "#C7E9C0", "#A1D99B", "#74C476", "#41AB5D", "#238B45", "#006D2C", "#00441B"],
height: 15,
width: 30
}
// create and append a legend at the top of the SVG
const legend = svgCanvas
.append("g")
.attr("id", "legend")
// translate the legend as to have the last rectangle on the very edge of the container
.attr("transform", `translate(${width - legendValues.percentage.length * legendValues.width}, 0)`);
// in the group referencing the legend, append one rectangle each for the defined values
legend
.selectAll("rect")
.data(legendValues.percentage)
.enter()
.append("rect")
.attr("width", legendValues.width)
.attr("height", legendValues.height)
// include the rectangle elements on the basis of their width, one after the other
.attr("x", (d, i) => i*legendValues.width)
// include the rectangle elements at the top of the canvas
.attr("y", 0)
.attr("fill", (d, i) => legendValues.color[i]);
// beside the rectangles, include text elements as labels for the rectangle elements themselves
legend
.selectAll("text")
.data(legendValues.percentage)
.enter()
.append("text")
.attr("x", (d,i) => i*legendValues.width)
// position the labels below the rectangle elements
.attr("y", legendValues.height*2)
.style("font-size", "0.6rem")
.text((d) => `${d}%`);
// create a quantize scale, a scale which allows to take as input a continuous interval and returns as output one discrete value
// the discrete value in question is attributed on the basis of the input, which is sectioned into intervals
// five values -> five intervals in which the input is divided
// the range in question: the colors chosen for the legend
const colorScale = d3
.scaleQuantize()
.range(legendValues.color);
/* store in two constant
- the URL responsible for the SVG values making up the shapes of the counties
- the URL responsible for educational data of each each county
the logic is as follows:
1. fetch the data from the URL regarding eduational data
1. consider the JSON format out of the response
1. pass the JSON to a function which includes the data in the matching data point which describes the SVG values
1. call a function to draw the counties on the basis of these values
1 as these values now obtain the educational data as well, include the pertinent information in each path element
*/
const URL_DATA = "https://raw.githubusercontent.com/no-stack-dub-sack/testable-projects-fcc/master/src/data/choropleth_map/for_user_education.json";
const URL_SVG = "https://raw.githubusercontent.com/no-stack-dub-sack/testable-projects-fcc/master/src/data/choropleth_map/counties.json";
// first off, retrieve educational data and pass the json output in a function responsible for the merge
fetch(URL_DATA)
.then((response) => response.json())
.then((json) => mergeData(json));
// create a function which takes as input the educational data and merges its values in the SVG values
function mergeData(data) {
// fetch the SVG values and include the JSON format from the response
fetch(URL_SVG)
.then((response) => response.json())
.then((json) => {
// loop through the array of educational data
for(let i = 0; i < data.length; i++) {
// for each educational data point, consider the .fips property
// this provides the link with the SVG values, in the matching property of .id
let fips = data[i].fips;
// loop through the array of geometries for the counties (geometries then used when drawing the map)
let geometries = json.objects.counties.geometries;
for(let j = 0; j < geometries.length; j++) {
// consider the id of each array item
let id = geometries[j].id;
// if the fips value matches the id counterpart
if(fips === id) {
// update the object with the SVG values with the properties of the matching object in the educational array
geometries[j] = Object.assign({}, geometries[j], data[i]);
// stop looping as to find the next match
break;
}
}
}
// return the entire JSON format, now updated with the matching educational values
return json;
})
// call a function to draw the map, on the basis of the updated JSON format
.then((json) => drawMap(json));
}
// with the JSON format including SVG values _and_ educational data draw the counties and include matching data
function drawMap(data) {
// with the obtained data, include the domain of the scale
// this is an interval ranging between 0 and 100%
colorScale.domain([0, d3.max(data.objects.counties.geometries, (d) => d.bachelorsOrHigher)]);
// for the map itself, the data provided by the URL returns an obejct of type 'Topology'
// console.log(data);
// as d3.geoPath() works with GeoJSON, it is first necessary to convert the object into a type of understandable format
// topojson.feature is a function from the topojson library which converts a topology to a feature collection
// it accepts two arguments, the object itself and the subset to be "feature-ized"
let feature = topojson.feature(data, data.objects.counties);
// console.log(feature);
// include the function which creates the SVG values from the coordinates included in the JSON file
const path = d3
.geoPath();
// the path function accepts as argument the feature collection and returns the SVG syntax required to draw the shape
// console.log(path(feature));
// append a path element for each feature
// the d attribute is dicated by the geoPath function, which gets applied to each data point
// the educational data is then retrieved, for each data point on the basis of its index, in the properties included with the previous merge
svgCanvas
.selectAll("path")
.data(feature.features)
.enter()
.append("path")
// on mouseenter, display the tooltip by altering its opacity
// include the prescribed attributes and position the element near the cursor
.on("mouseenter", (d,i) => {
tooltip
.style("opacity", 1)
.attr("data-fips", data.objects.counties.geometries[i].fips)
.attr("data-education", data.objects.counties.geometries[i].bachelorsOrHigher)
// position the tooltip close to the cursor, using d3.event.layerX and d3.event.layerY
.style("left", `${d3.event.layerX + 5}px`)
// as the y scale is offset by the margin value, include the margin value to have the tooltip close to the actual hovered cell
.style("top", `${d3.event.layerY + 5}px`);
// include the text in the two paragraph elements
tooltip
.select("p.area")
.text(() => `${data.objects.counties.geometries[i].area_name}, ${data.objects.counties.geometries[i].state}`);
tooltip
.select("p.education")
.text(() => `${data.objects.counties.geometries[i].bachelorsOrHigher}%`);
})
// on mouseout, hide the tooltip
.on("mouseout", () => tooltip.style("opacity", 0))
.attr("d", path)
.attr("transform", `scale(0.82, 0.62)`)
.attr("class", "county")
// include the attributes prescribed by the user stories
.attr("data-fips", (d, i) => data.objects.counties.geometries[i].fips)
.attr("data-state", (d, i) => data.objects.counties.geometries[i].state)
.attr("data-area", (d, i) => data.objects.counties.geometries[i].area_name)
.attr("data-education", (d, i) => data.objects.counties.geometries[i].bachelorsOrHigher)
// include a fill property dependant on the bachelorsOrHigher property and the color scale
.attr("fill", (d, i) => colorScale(data.objects.counties.geometries[i].bachelorsOrHigher));
}