-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratio.html
229 lines (213 loc) · 7.12 KB
/
ratio.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Narrative Visualization Project</title>
<script src="https://d3js.org/d3.v6.min.js"></script>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
#visualization {
width: 80%;
margin: auto;
display: flex;
flex-direction: column;
align-items: center;
padding-top: 30px;
}
.chart-container {
display: flex;
gap: 10px;
width: 100%;
margin-bottom: 25px;
}
.chart {
width: 50%;
}
.axis text {
font-size: 10px;
}
.tooltip {
position: absolute;
background: rgba(0, 0, 0, 0.8);
color: #f1f1f1;
padding: 8px;
border-radius: 8px;
pointer-events: none;
opacity: 0;
transition: opacity 0.4s ease;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
}
.annotation {
margin-top: 15px;
font-size: 16px;
text-align: center;
width: 85%;
line-height: 1.5;
color: #333;
}
.title {
font-size: 26px;
font-weight: 700;
margin-bottom: 25px;
color: #0056b3;
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
}
.button {
margin-top: 25px;
padding: 12px 24px;
font-size: 18px;
cursor: pointer;
border: none;
background-color: #28a745;
color: #ffffff;
border-radius: 6px;
transition: background-color 0.3s ease;
}
.button:hover {
background-color: #218838;
}
.button-container {
display: flex;
justify-content: space-around;
width: 100%;
margin-top: 20px;
}
.filter-container {
margin-bottom: 25px;
font-size: 16px;
}
.filter-container label {
margin-right: 12px;
font-weight: 500;
color: #333;
}
</style>
</head>
<body>
<div id="visualization">
<div class="title">Ratio of City MPG to Highway MPG of Motor Vehicles</div>
<div class="filter-container">
<label for="filterElectric">Show only Gasoline and Diesel vehicles:</label>
<input type="checkbox" id="filterElectric">
</div>
<div class="chart-container">
<svg id="chart1" class="chart"></svg>
<svg id="chart2" class="chart"></svg>
</div>
<div id="annotation" class="annotation">
This confirms the trend noticed in the first two charts. City driving is better for electric cars, while highway driving is better gas and diesel cars. Budget, location, and commute among many other factors all contribute to what car is a better choice for you.
</div>
</div>
<div id="tooltip" class="tooltip"></div>
<script>
var t = 30;
var r = 60;
var b = 80;
var l = 100;
var width = 600 - l - r;
var height = 400 - t - b;
var color = d3.scaleOrdinal(d3.schemeCategory10);
d3.csv("https://flunky.github.io/cars2017.csv").then(function(data) {
data.forEach(function(d) {
d.AverageCityMPG = +d.AverageCityMPG;
d.AverageHighwayMPG = +d.AverageHighwayMPG;
d.Ratio = d.AverageCityMPG / d.AverageHighwayMPG;
d.EngineCylinders = +d.EngineCylinders;
});
updateCharts(data);
d3.select("#filterElectric").on("change", function() {
updateCharts(data);
});
});
function updateCharts(data) {
var isElectricOnly = d3.select("#filterElectric").property("checked");
var filteredData = isElectricOnly
? data.filter(function(d) { return d.Fuel === "Gasoline" || d.Fuel === "Diesel"; })
: data;
d3.select("#chart1").select("g").remove();
d3.select("#chart2").select("g").remove();
createBarChart(filteredData, "Ratio", false, "Top 5 Ratio of City MPG to Highway MPG", d3.select("#chart1"));
createBarChart(filteredData, "Ratio", true, "Bottom 5 Ratio of City MPG to Highway MPG", d3.select("#chart2"));
}
function createBarChart(data, attribute, ascending, title, svgSelection) {
data.sort(function(a, b) {
return ascending ? a[attribute] - b[attribute] : b[attribute] - a[attribute];
});
var filteredData = data.slice(0, 5);
var x = d3.scaleBand()
.domain(filteredData.map(function(d) { return d.Make + " " + d.EngineCylinders; }))
.range([0, width])
.padding(0.1);
var y = d3.scaleLinear()
.domain([0, d3.max(filteredData, function(d) { return d[attribute]; })])
.range([height, 0]);
var svg = svgSelection
.attr("width", width + l + r)
.attr("height", height + t + b)
.append("g")
.attr("transform", "translate(" + l + "," + t + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.selectAll("text")
.attr("transform", "rotate(-40)")
.style("text-anchor", "end");
svg.append("g")
.attr("class", "y axis")
.call(d3.axisLeft(y));
var bars = svg.selectAll(".bar")
.data(filteredData);
bars.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.Make + " " + d.EngineCylinders); })
.attr("width", x.bandwidth())
.attr("y", height)
.attr("height", 0)
.attr("fill", function(d, i) { return color(i); })
.on("mouseover", function(event, d) {
var tooltip = d3.select("#tooltip");
tooltip.style("left", event.pageX + "px")
.style("top", event.pageY - 40 + "px")
.style("opacity", 1)
.html("<strong>Make:</strong> " + d.Make + "<br>" +
"<strong>Fuel:</strong> " + d.Fuel + "<br>" +
"<strong>Engine Cylinders:</strong> " + d.EngineCylinders + "<br>" +
"<strong>" + attribute + ":</strong> " + d[attribute]);
})
.on("mouseout", function() {
d3.select("#tooltip").style("opacity", 0);
})
.merge(bars)
.transition()
.duration(750)
.attr("x", function(d) { return x(d.Make + " " + d.EngineCylinders); })
.attr("width", x.bandwidth())
.attr("y", function(d) { return y(d[attribute]); })
.attr("height", function(d) { return height - y(d[attribute]); });
bars.exit()
.transition()
.duration(750)
.attr("height", 0)
.attr("y", height)
.remove();
svg.append("text")
.attr("x", width / 2)
.attr("y", 0 - (t / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.text(title);
}
</script>
<div class="button-container">
<a href="index.html"><button class="button">Back to Introduction</button></a>
<a href="city.html"><button class="button">City MPG Comparison</button></a>
<a href="highway.html"><button class="button">Highway MPG Comparison</button></a>
</div>
</body>
</html>