-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
273 lines (232 loc) · 10.6 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
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
<!DOCTYPE html>
<html lang="en">
<title>Visualization Programming with D3.JS</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://www.w3schools.com/lib/w3-theme-black.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<!-- Load color palettes -->
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<style>
html,body,h1,h2,h3,h4,h5,h6 {font-family: "Roboto", sans-serif;}
.w3-sidebar {
z-index: 3;
width: 250px;
top: 43px;
bottom: 0;
height: inherit;
}
</style>
<body>
<!-- Navbar -->
<div class="w3-top">
<div class="w3-bar w3-theme w3-top w3-left-align w3-large">
<a class="w3-bar-item w3-button w3-right w3-hide-large w3-hover-white w3-large w3-theme-l1" href="javascript:void(0)" onclick="w3_open()"><i class="fa fa-bars"></i></a>
<!-- <a href="#" class="w3-bar-item w3-button w3-theme-l1">Logo</a> -->
<a href="#" class="w3-bar-item w3-button w3-hide-small w3-hover-white">Heatmap</a>
<a href="bubble_chart.html" class="w3-bar-item w3-button w3-hide-small w3-hover-white">Bubble Chart</a>
<a href="scatter_plot.html" class="w3-bar-item w3-button w3-hide-small w3-hover-white">Scatter Plot</a>
<a href="density_plot.html" class="w3-bar-item w3-button w3-hide-small w3-hover-white">Density Plot</a>
<a href="histogram.html" class="w3-bar-item w3-button w3-hide-small w3-hide-medium w3-hover-white">Histogram</a>
</div>
</div>
<!-- Sidebar -->
<nav class="w3-sidebar w3-bar-block w3-collapse w3-large w3-theme-l5 w3-animate-left" id="mySidebar">
<a href="javascript:void(0)" onclick="w3_close()" class="w3-right w3-xlarge w3-padding-large w3-hover-black w3-hide-large" title="Close Menu">
<i class="fa fa-remove"></i>
</a>
<h4 class="w3-bar-item"><b>Visualizations</b></h4>
<p><b>Dataset:</b> a1-cars.csv</p>
<p><b>Interactions</b></p>
<P>This visualization offers interaction to the user by displaying tooltip information
when a user hovers the mouse over a cell
</p>
<p>The tooltip information consists of details such as the name of the car,
its Manufacturer, and its fuel efficiency in Miles Per Gallon (MPG)
</p>
</nav>
<!-- Overlay effect when opening sidebar on small screens -->
<div class="w3-overlay w3-hide-large" onclick="w3_close()" style="cursor:pointer" title="close side menu" id="myOverlay"></div>
<!-- Main content: shift it to the right by 300 pixels when the sidebar is visible -->
<div class="w3-main" style="margin-left:300px">
<div class="w3-row w3-padding-64">
<div class="w3-twothird w3-container">
<h1 class="w3-text-teal">Data Visualization using D3.js: Heatmap</h1>
<!-- Create a div where the graph will take place -->
<div id="my_dataviz">
<script>
// set the dimensions and margins of the graph
var margin = {top: 80, right: 25, bottom: 30, left: 40},
width = 600 - margin.left - margin.right,
height = 420 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
//Read the data
d3.csv("https://raw.githubusercontent.com/vjajala/dataViz/main/a1-cars.csv", function(data) {
// Labels of row and columns -- Origin and Model_Year
var myOrigin = d3.map(data, function(d){return d.Origin;}).keys()
var myModel_Years = d3.map(data, function(d){return d.Model_Year;}).keys()
// Build X scales and axis:
var x = d3.scaleBand()
.range([ 0, width ])
.domain(myOrigin)
.padding(0.05);
svg.append("g")
.style("font-size", 15)
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).tickSize(0))
.select(".domain").remove()
// Build Y scales and axis:
var y = d3.scaleBand()
.range([ height, 0 ])
.domain(myModel_Years)
.padding(0.05);
svg.append("g")
.style("font-size", 15)
.call(d3.axisLeft(y).tickSize(0))
.select(".domain").remove()
// Build color scale
var myColor = d3.scaleSequential()
.interpolator(d3.interpolateInferno)
.domain([1,50])
// create a tooltip
var tooltip = d3.select("#my_dataviz")
.append("div")
.style("opacity", 0)
.attr("class", "tooltip")
.style("background-color", "white")
.style("border", "solid")
.style("border-width", "2px")
.style("border-radius", "5px")
.style("padding", "5px")
// Three function that change the tooltip when user hover / move / leave a cell
var mouseover = function(d) {
tooltip
.style("opacity", 1)
d3.select(this)
.style("stroke", "black")
.style("opacity", 1)
}
var mousemove = function(d) {
tooltip
.html("Car: " + d.Car + "<br>Manufacturer: " + d.Manufacturer + "<br>Miles Per Gallon (MPG): " + d.MPG)
.style("left", (d3.mouse(this)[0]+70) + "px")
.style("top", (d3.mouse(this)[1]) + "px")
}
var mouseleave = function(d) {
tooltip
.style("opacity", 0)
d3.select(this)
.style("stroke", "none")
.style("opacity", 0.8)
}
// add the squares
svg.selectAll()
.data(data, function(d) {return d.Origin+':'+d.Model_Year;})
.enter()
.append("rect")
.attr("x", function(d) { return x(d.Origin) })
.attr("y", function(d) { return y(d.Model_Year) })
.attr("rx", 10)
.attr("ry", 10)
.attr("width", x.bandwidth() )
.attr("height", y.bandwidth() )
.style("fill", function(d) { return myColor(d.MPG)} )
.style("stroke-width", 4)
.style("stroke", "none")
.style("opacity", 0.8)
.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)
})
// Add title to graph
svg.append("text")
.attr("x", 0)
.attr("y", -50)
.attr("text-anchor", "left")
.style("font-size", "22px")
.text("Heatmap of Cars by Model_Year, Origin and MPG");
// Add subtitle to graph
svg.append("text")
.attr("x", 0)
.attr("y", -20)
.attr("text-anchor", "left")
.style("font-size", "14px")
.style("fill", "grey")
.style("max-width", 400)
.text("Please hover the mouse over a Cell to get the details of the car");
// Defining the graph's axis labels
const xAxisLabel = "Origin of Car";
const yAxisLabel = "Model_Year";
//Create X axis label
svg.append("text")
.attr("x", width / 2 )
.attr("y", height + margin.bottom)
.style("text-anchor", "middle")
.text(xAxisLabel);
//Create Y axis label
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0-margin.left)
.attr("x",0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text(yAxisLabel);
</script>
</div>
</div>
</div>
<div class="w3-row">
<div class="w3-twothird w3-container">
<h3 class="w3-text-teal">Description of Results</h3>
<p>The heatmap uses colors to denote the fuel efficiency of the car in MPG. The bluer the
cell, the least fuel efficient the car is, and the more orange-red the cell is, the
more fuel efficient the car is.
</p>
<p>The heatmap shows that cars of Japanese origin are the most fuel efficient (more MPG), owing to the
pre-dominant orange-reddish hues of the cells denoting Japanese cars. The next in line of fuel efficiency are European cars,
and American cars are the least fuel efficient, owing to the dominant blue color of the cells.
</p>
<p>The heatmap also shows that cars produced in '82 are the most fuel efficient, as compared to the other model years</p>
</div>
</div>
<footer id="myFooter">
<div class="w3-container w3-theme-l2 w3-padding-32">
<h4>Visualization Programming with D3.js</h4>
</div>
</footer>
<!-- END MAIN -->
</div>
<script>
// Get the Sidebar
var mySidebar = document.getElementById("mySidebar");
// Get the DIV with overlay effect
var overlayBg = document.getElementById("myOverlay");
// Toggle between showing and hiding the sidebar, and add overlay effect
function w3_open() {
if (mySidebar.style.display === 'block') {
mySidebar.style.display = 'none';
overlayBg.style.display = "none";
} else {
mySidebar.style.display = 'block';
overlayBg.style.display = "block";
}
}
// Close the sidebar with the close button
function w3_close() {
mySidebar.style.display = "none";
overlayBg.style.display = "none";
}
</script>
</body>
</html>