forked from cs480x-21c/final
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
235 lines (197 loc) · 7.8 KB
/
index.html
File metadata and controls
235 lines (197 loc) · 7.8 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<script src="https://d3js.org/d3.v6.min.js"></script>
<svg id="frame"></svg>
<style>
</style>
<body>
<select id="selectButton"></select>
</body>
<script>
var listCsv = ["./data/TotalDiningEmployees.csv", "./data/UnemploymentData.csv", "./data/USAWEeklyAverageHours.csv"];
var listPromises = []
for (var i = 0; i < listCsv.length; i++)
{
const promise = new Promise((resolve, reject) => {
resolve(d3.csv(listCsv[i]));
});
listPromises.push(promise);
}
var width = 540;
var height = 300;
var minIncome = 1;
var margin = {top: 10, right: 30, bottom: 30, left: 60};
width = width - margin.left - margin.right,
height = height - margin.top - margin.bottom;
//setup body area and axis
var scatterSvg = d3.select("body").append("svg")
.attr("id", "scatterPlot")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
var xscaleScatter = d3.scaleLinear()
.domain([1, 12])
.range([0, width]);
var xAxis = scatterSvg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(xscaleScatter));
var yscaleScatterTotalEmployees = d3.scaleLinear()
.domain([6000,13000])
.range([height, 0]);
var yscaleScatterUnemployment = d3.scaleLinear()
.domain([1, 35])
.range([height, 0]);
var yscaleScatterHoursWorked = d3.scaleLinear()
.domain([20, 27])
.range([height, 0]);
var yAxis = scatterSvg.append("g")
.call(d3.axisLeft(yscaleScatterTotalEmployees));
var totalEmployeesData;
var totalEmployeesLine;
var unemploymentData;
var unemploymentLine;
var avgWeeklyHours;
var avgWeeklyHoursLine;
Promise.all(listPromises).then((values) => {
totalEmployeesData = separateData(values[0]);
totalEmployeesLine = createLineData(values[0]);
unemploymentData = separateData(values[1]);
unemploymentLine = createLineData(values[1]);
avgWeeklyHours = separateData(values[2]);
avgWeeklyHoursLine = createLineData(values[2]);
//onsole.log(totalEmployeesData);
//console.log(totalEmployeesLine);
//console.log(unemploymentData);
//console.log(avgWeeklyHours);
//console.log(totalEmployeesData.columns);
//idea: line chart that is highlightable on mouseover,
//overlaid years selectable by years
//also points that bring up the value and maybe a tooltip with a comment?
//example 1: Total Employees data
//start with the data points, should be all overlaid
var allGroup = ["Total Employees", "Unemployment", "Average Weekly Hours"];
// add the options to the button
d3.select("#selectButton")
.selectAll('myOptions')
.data(allGroup)
.enter()
.append('option')
.text(function (d) { return d; }) // text showed in the menu
.attr("value", function (d) { return d; });
updatePoints(totalEmployeesData, totalEmployeesLine, xscaleScatter, yscaleScatterTotalEmployees);
});
function updatePoints(data, line, xscaleScatter, yscaleScatter) {
//data points
scatterSvg.selectAll("circle").remove();
scatterSvg
.selectAll("dot")
.data(data)
.enter()
.append("circle")
.attr("id", function(d,i) { return "scatter"+ d[4]})
.attr('cx', function(d, i){
//depends on the month
return xscaleScatter(d[1]);
})
.attr('cy', function(d){
//depends on the actual val
return yscaleScatter(d[2]);
})
.attr("r", 3)
.style("fill", "#FFFFFF")
.style("opacity", function(d){return 1;})
.attr("stroke", "black")
.attr("stroke-width", function(d){
return 1;
})
.on('mouseover', function(mouseEvent, val) {
//display datapoint info somewhere
d3.select(this).attr('stroke-width', 2).attr('stroke', 'black');
})
.on('mouseout', function(mouseEvent, val) {
//consider undoing the above
d3.select(this).attr('stroke-width', 1).attr('stroke', 'black');
});
scatterSvg.selectAll("path").remove();
//credit https://www.d3-graph-gallery.com/graph/line_basic.html
for (var i = 0; i < line.length; i++) {
var lineGenerator = d3.line();
var lineData = []
var currRow = line[i][1];
var currYear = line[i][0];
for (var j = 0; j < line[i][1].length; j++) {
lineData.push([xscaleScatter(currRow[j][0]), yscaleScatter(currRow[j][1])]);
}
var pathData = lineGenerator(lineData);
scatterSvg.append("path")
//.transition()
//.duration(1000)
.attr("d", pathData)
.attr("stroke", "black")
.attr("stroke", "gray")
.attr("stroke-width", 3)
.style("opacity", function(d){return 0.7;})
.attr("fill", "none")
.on('mouseover', function(mouseEvent, val) {
//display datapoint info somewhere
d3.select(this).attr('stroke-width', 7).attr('stroke', 'steelblue').style("opacity", function(d){return 1;});
})
.on('mouseout', function(mouseEvent, val) {
//consider undoing the above
d3.select(this).attr('stroke-width', 3).attr('stroke', 'gray').style("opacity", function(d){return 0.7;});
});
}
}
d3.select("#selectButton").on("change", function(d) {
// recover the option that has been chosen
var selectedOption = d3.select(this).property("value")
// run the updateChart function with this selected option
if (selectedOption == "Total Employees"){ yAxis.call(d3.axisLeft(yscaleScatterTotalEmployees)); updatePoints(totalEmployeesData, totalEmployeesLine, xscaleScatter, yscaleScatterTotalEmployees); }
else if (selectedOption == "Unemployment") {yAxis.call(d3.axisLeft(yscaleScatterUnemployment)); updatePoints(unemploymentData, unemploymentLine, xscaleScatter, yscaleScatterUnemployment);}
else { yAxis.call(d3.axisLeft(yscaleScatterHoursWorked)); updatePoints(avgWeeklyHours, avgWeeklyHoursLine, xscaleScatter, yscaleScatterHoursWorked);}
}); // corresponding value returned by the button
function separateData(dataIn) {
//function separates the data in format:
//Each row is a year
//each col is a month
//each cell is a val
//and changes it into:
//everything is one row
//each index is [year, month, val]
var listOfEverything = [];
for (var i = 0; i < dataIn.length; i ++) {
var year = dataIn[i]["Year"];
count = 1;
for (var key in dataIn[i]) {
if (parseInt(dataIn[i][key]) != NaN && dataIn[i][key] != "" && key != "Year") {
listOfEverything.push([year, count, parseInt(dataIn[i][key])]);
count += 1;
}
}
}
return listOfEverything;
}
function createLineData(dataIn) {
//function separates the data in format:
//Each row is a year
//each col is a month
//each cell is a val
//and changes it into:
//each year is a row
//each index is [month, val]
var listOfEverything = [];
for (var i = 0; i < dataIn.length; i ++) {
var year = dataIn[i]["Year"];
var listYear = [];
count = 1;
for (var key in dataIn[i]) {
if (parseInt(dataIn[i][key]) != NaN && dataIn[i][key] != "" && key != "Year") {
listYear.push([count, parseInt(dataIn[i][key])]);
count += 1;
}
}
listOfEverything.push([year, listYear]);
}
return listOfEverything;
}
</script>