-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
236 lines (212 loc) · 9.72 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cars</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<style>
.button-container {
text-align: center;
margin-bottom: 20px;
}
.button-container button {
background-color: #007bff;
border: none;
color: white;
padding: 10px 20px;
margin: 0 10px;
text-align: center;
font-size: 16px;
cursor: pointer;
border-radius: 5px;
transition: background-color 0.2s;
}
.button-container button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="button-container">
<button onclick="update_scene('Fuel', 'Fuel')">1. Fuel Types</button>
<button onclick="update_scene('EngineCylinders', 'Engine Cylinders')">2. Engine Cylinders</button>
<button onclick="update_scene('Country', 'Country')">3. Countries</button>
</div>
<div id="chart"></div>
<script>
d3.csv('cars2017.csv').then(function(data) {
data.forEach(element => {
element.EngineCylinders = +element.EngineCylinders;
});
const svg = d3.select("#chart")
.append("svg")
.attr("width", 1800)
.attr("height", 700)
.append("g")
.attr("transform", "translate(500, 50)");
const x_axis = d3.scaleLog()
.domain([10, 200])
.range([0, 600]);
const y_axis = d3.scaleLog()
.domain([8, 160])
.range([500, 0]);
const dots = svg.selectAll("dot")
.data(data)
.enter()
.append("circle")
.attr("cx", d => x_axis(d.AverageHighwayMPG))
.attr("cy", d => y_axis(d.AverageCityMPG))
.attr("r", 6)
.attr("opacity", "0")
.attr("stroke", "none");
const tooltip = d3.select("body")
.append("div")
.style("opacity", 0)
.style("position", "absolute")
.style("background", "rgba(0,0,0,0.5)")
.style("border-radius", "8px")
.style("padding", "4px");
const title = svg.append("text")
.attr("x", 300)
.attr("y", 0)
.attr("font-size", "25px")
.attr("font-weight", "bold")
.attr("text-anchor", "middle")
.text("");
const color = d3.scaleOrdinal(d3.schemeCategory10);
const legend = svg.append("g")
.attr("font-size", "19px")
.attr("cursor", "pointer")
.attr("transform", "translate(650, 0)");
let selected_categories = [];
let current_dimension = "";
svg.append("g")
.style("font", "19px times")
.attr("transform", "translate(0, 500)")
.call(d3.axisBottom(x_axis).ticks(5));
svg.append("g")
.style("font", "19px times")
.call(d3.axisLeft(y_axis).ticks(5));
svg.append("text")
.attr("x", "500")
.attr("y", "550")
.attr("font-size", "20px")
.text("Average Highway MPG");
svg.append("text")
.attr("x", -120)
.attr("y", -50)
.attr("transform", "rotate(-90)")
.attr("font-size", "20px")
.text("Average City MPG");
dots.on("mouseover", (event, d) => {
tooltip.transition()
.duration(200)
.style("opacity", .9);
if(current_dimension == "Fuel"){
tooltip.html("Make: " + d.Make +
"<br>Num Cylinders: " + d.EngineCylinders +
"<br>Highway MPG: " + d.AverageHighwayMPG +
"<br>City MPG: " + d.AverageCityMPG +
"<br>Country: " + d.Country);
}
else if(current_dimension == "EngineCylinders"){
tooltip.html("Make: " + d.Make +
"<br>Fuel: " + d.Fuel +
"<br>Highway MPG: " + d.AverageHighwayMPG +
"<br>City MPG: " + d.AverageCityMPG +
"<br>Country: " + d.Country);
}
else{
tooltip.html("Make: " + d.Make +
"<br>Fuel: " + d.Fuel +
"<br>Num Cylinders: " + d.EngineCylinders +
"<br>Highway MPG: " + d.AverageHighwayMPG +
"<br>City MPG: " + d.AverageCityMPG);
};
tooltip.style("left", (event.pageX + 5) + "px")
.style("top", (event.pageY + 5) + "px");
})
.on("mouseout", () => {
tooltip.transition()
.duration(200)
.style("opacity", 0);
});
function update_legend(categories) {
legend.selectAll("*").remove();
categories.forEach((c, i) => {
legend.append("rect")
.attr("x", 0)
.attr("y", 30 * i)
.attr("width", 20)
.attr("height", 20)
.attr("fill", color(c))
.attr("cursor", "pointer")
.on("click", function() {
if(selected_categories.includes(c)){
selected_categories.splice(selected_categories.indexOf(c), 1);
d3.select(this).attr("stroke", "none");
}
else{
selected_categories.push(c);
d3.select(this).attr("stroke", "black").attr("stroke-width", "4px");
};
if (selected_categories.length == 0) {
dots.transition()
.duration(1)
.style("stroke", "none")
.style("opacity", "0.9");
}
else{
dots.style("opacity", d => (selected_categories.includes(d[current_dimension]) ? "0.9" : "0.3"))
.style("stroke", d => (selected_categories.includes(d[current_dimension]) ? "black" : "none"))
.style("stroke-width", "4px");
};
});
legend.append("text")
.attr("x", 30)
.attr("y", 30 * i + 15)
.attr("font-size", "19px")
.text(c)
.attr("cursor", "pointer")
.on("click", function() {
if(selected_categories.includes(c)){
selected_categories.splice(selected_categories.indexOf(c), 1);
legend.selectAll("rect").filter((d, j) => j === i).attr("stroke", "none");
}
else{
selected_categories.push(c);
legend.selectAll("rect").filter((d, j) => j === i).attr("stroke", "black").attr("stroke-width", "4px");
};
if (selected_categories.length == 0) {
dots.transition()
.duration(1)
.style("stroke", "none")
.style("opacity", "0.9");
}
else{
dots.style("opacity", d => (selected_categories.includes(d[current_dimension]) ? "0.9" : "0.3"))
.style("stroke", d => (selected_categories.includes(d[current_dimension]) ? "black" : "none"))
.style("stroke-width", "4px");
};
});
});
};
window.update_scene = function(dimension, curr_title){
title.text(curr_title);
selected_categories = [];
current_dimension = dimension;
dots.transition()
.duration(100)
.style("stroke", "none")
.style("opacity", "0.9")
.style("fill", d => color(d[current_dimension]));
let all_categories = [...new Set(data.map(d => d[current_dimension]))];
all_categories = all_categories.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
update_legend(all_categories);
};
update_scene("Fuel", "Fuel");
});
</script>
</body>
</html>