-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
370 lines (307 loc) · 11.7 KB
/
app.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Preliminary Variable Setup
let svgWidth = 960;
let svgHeight = 500;
let margin = {
top: 20,
right: 40,
bottom: 80,
left: 100
};
let width = svgWidth - margin.left - margin.right;
let height = svgHeight - margin.top - margin.bottom;
// Creates an SVG, appends an SVG group that will hold the interactive chart,
// and shifts the latter by left and top margins.
let svg = d3
.select(".chart")
.append("svg")
.attr("width", svgWidth)
.attr("height", svgHeight);
// Appends an SVG group
let chartGroup = svg.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
// Initial Parameters
var selectedXAxis = "poverty";
var selectedYAxis = "healthcare";
// The xScale function updates x-scale variables upon clicking the axis label
function xScale(journalismData, selectedXAxis) {
var xLinearScale = d3.scaleLinear()
.domain([d3.min(journalismData, d => d[selectedXAxis]) * 0.8,
d3.max(journalismData, d => d[selectedXAxis]) * 1.2
])
.range([0, width]);
return xLinearScale;
}
// The renderXAxis function in turn updates the xAxis variables upon clicking the axis label
function renderXAxis(newXScale, xAxis) {
var bottomAxis = d3.axisBottom(newXScale);
xAxis.transition()
.duration(1000)
.call(bottomAxis);
return xAxis;
}
// The yScale function updates y-scale variables upon clicking the axis label
function yScale(journalismData, selectedYAxis) {
var yLinearScale = d3.scaleLinear()
.domain([d3.min(journalismData, d => d[selectedYAxis]) * 0.8,
d3.max(journalismData, d => d[selectedYAxis]) * 1.2
])
.range([height, 0]);
return yLinearScale;
}
// The renderYAxis function in turn updates the yAxis variables upon clicking the axis label
function renderYAxis(newYScale, yAxis) {
var leftAxis = d3.axisLeft(newYScale);
yAxis.transition()
.duration(1000)
.call(leftAxis);
return yAxis;
}
// Updates Circles Group with a transition to new x value for circles
function renderXCircles(circlesGroup, newXScale, selectedXAxis) {
circlesGroup.transition()
.duration(1000)
.attr("cx", d => newXScale(d[selectedXAxis]))
.attr("dx", d => newXScale(d[selectedXAxis]));
return circlesGroup;
}
function renderYCircles(circlesGroup, newYScale, selectedYAxis) {
circlesGroup.transition()
.duration(1000)
.attr("cy", d => newYScale(d[selectedYAxis]))
.attr("dy", d => newYScale(d[selectedYAxis]));
return circlesGroup;
}
// Updates Circles Group with new tooltips
function updateToolTip(selectedXAxis, selectedYAxis, circlesGroup) {
var xLabel;
if (selectedXAxis === "poverty") {
xLabel = "In Poverty (%):";
}
else if (selectedXAxis === "age") {
xLabel = "Age (Median):";
}
else {
xLabel = "Household Income (Median):";
}
var yLabel;
if (selectedYAxis === "healthcare") {
yLabel = "Lacks Healthcare (%)";
}
else if (selectedYAxis === "smokes") {
yLabel = "Smokes (%):";
}
else {
yLabel = "Obese (%):";
}
var toolTip = d3.tip()
.attr("class", "tooltip")
.offset([80, -60])
.html(function(d) {
return (`${d.state}<br>${xLabel} ${d[selectedXAxis]}<br>${yLabel} ${d[selectedYAxis]}`);
});
circlesGroup.call(toolTip);
circlesGroup.on("mouseover", function(data) {
toolTip.show(data);
})
// On Mouseout Event
.on("mouseout", function(data, index) {
toolTip.hide(data);
});
return circlesGroup;
}
// Retrieves data from CSV file and executes all below
d3.csv("assets/census_Journalism_Data.csv").then(function(journalismData, err) {
if (err) throw err;
// Parses data
journalismData.forEach(function(data) {
data.poverty = +data.poverty;
data.age = +data.age;
data.income = +data.income;
data.healthcare = +data.healthcare;
data.smokes = +data.smokes;
data.obesity = +data.obesity;
});
var xLinearScale = xScale(journalismData, selectedXAxis);
var yLinearScale = yScale(journalismData, selectedYAxis);
// Creates Initial Axis Functions
var bottomAxis = d3.axisBottom(xLinearScale);
var leftAxis = d3.axisLeft(yLinearScale);
// Appends X Axis
var xAxis = chartGroup.append("g")
.classed("x-axis", true)
.attr("transform", `translate(0, ${height})`)
.call(bottomAxis);
// Appends Y Axis
var yAxis = chartGroup.append("g")
.classed("y-axis", true)
.call(leftAxis);
// Appends Initial Circles
var circlesGroup = chartGroup.selectAll("circle")
.data(journalismData)
.enter()
.append("circle")
.attr("cx", d => xLinearScale(d[selectedXAxis]))
.attr("cy", d => yLinearScale(d[selectedYAxis]))
.attr("r", 20)
.attr("fill", "pink")
.attr("opacity", ".5")
circlesGroup.append("text")
.text(d => d.abbr)
.attr("font-size", 14)
.attr("dx", d => xLinearScale(d[selectedXAxis]) - 10)
.attr("dy", d => yLinearScale(d[selectedYAxis]) + 5);
console.log(circlesGroup)
// Creates groups for two x-axis labels
var labelsGroup = chartGroup.append("g")
.attr("transform", `translate(${width / 2}, ${height + 20})`);
var povertyLabel = labelsGroup.append("text")
.attr("x", 0)
.attr("y", 20)
.attr("value", "poverty") // Value being grabbed for event listener
.classed("active", true)
.classed("x-label", true)
.text("In Poverty (%)");
var ageLabel = labelsGroup.append("text")
.attr("x", 0)
.attr("y", 40)
.attr("value", "age") // Value being grabbed for event listener
.classed("inactive", true)
.classed("x-label", true)
.text("Age (Median)");
var incomeLabel = labelsGroup.append("text")
.attr("x", 0)
.attr("y", 60)
.attr("value", "income") // Value being grabbed for event listener
.classed("inactive", true)
.classed("x-label", true)
.text("Household Income (Median)");
var healthcareLabel = chartGroup.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - (margin.left * (2 / 3)))
.attr("x", 0 - (height / 2))
.attr("dy", "1em")
.attr("value", "healthcare") // Value being grabbed for event listener
.classed("active", true)
.classed("y-label", true)
.text("Lacks Healthcare (%)");
var smokesLabel = chartGroup.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - (margin.left / 2))
.attr("x", 0 - (height / 2))
.attr("dy", "1em")
.attr("value", "smokes") // Value being grabbed for event listener
.classed("inactive", true)
.classed("y-label", true)
.text("Smokes (%)");
var obesityLabel = chartGroup.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - (margin.left / 3))
.attr("x", 0 - (height / 2))
.attr("dy", "1em")
.attr("value", "obesity") // Value being grabbed for event listener
.classed("inactive", true)
.classed("y-label", true)
.text("Obese (%)");
var circlesGroup = updateToolTip(selectedXAxis, selectedYAxis, circlesGroup);
d3.selectAll("text.x-label")
.on("click", function() {
// Gets value of selection
let xValue = d3.select(this).attr("value");
if (xValue !== selectedXAxis) {
// Replaces selectedXAxis with Value
selectedXAxis = xValue;
// Updates X Scale with new data
var xLinearScale = xScale(journalismData, selectedXAxis);
// Updates X Axis with transition
xAxis = renderXAxis(xLinearScale, xAxis);
// Updates Circles with new x and y values
circlesGroup = renderXCircles(circlesGroup, xLinearScale, selectedXAxis);
// Updates Tooltips with new information
// circlesGroup = updateToolTip(selectedXAxis, circlesGroup);
if (selectedXAxis === "poverty") {
povertyLabel
.classed("active", true)
.classed("inactive", false);
ageLabel
.classed("active", false)
.classed("inactive", true);
incomeLabel
.classed("active", false)
.classed("inactive", true);
}
else if (selectedXAxis === "age") {
povertyLabel
.classed("active", false)
.classed("inactive", true);
ageLabel
.classed("active", true)
.classed("inactive", false);
incomeLabel
.classed("active", false)
.classed("inactive", true);
}
else {
povertyLabel
.classed("active", false)
.classed("inactive", true);
ageLabel
.classed("active", false)
.classed("inactive", true);
incomeLabel
.classed("active", true)
.classed("inactive", false);
}
}
});
d3.selectAll("text.y-label")
.on("click", function() {
// Gets value of selection
let yValue = d3.select(this).attr("value");
if (yValue !== selectedYAxis) {
selectedYAxis = yValue;
// Updates Y Scale with new data
var yLinearScale = yScale(journalismData, selectedYAxis);
// Updates Y Axis with transition
yAxis = renderYAxis(yLinearScale, yAxis);
// Updates Circles with new x and y values
circlesGroup = renderYCircles(circlesGroup, yLinearScale, selectedYAxis);
// Updates Tooltips with new information
// circlesGroup = updateToolTip(selectedXAxis, circlesGroup);
if (selectedYAxis === "healthcare") {
healthcareLabel
.classed("active", true)
.classed("inactive", false);
smokesLabel
.classed("active", false)
.classed("inactive", true);
obesityLabel
.classed("active", false)
.classed("inactive", true);
}
else if (selectedYAxis === "smokes") {
healthcareLabel
.classed("active", false)
.classed("inactive", true);
smokesLabel
.classed("active", true)
.classed("inactive", false);
obesityLabel
.classed("active", false)
.classed("inactive", true);
}
else {
healthcareLabel
.classed("active", false)
.classed("inactive", true);
smokesLabel
.classed("active", false)
.classed("inactive", true);
obesityLabel
.classed("active", true)
.classed("inactive", false);
}
}
});
}).catch(function(error) {
console.log(error);
});