-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscatter_plot.html
More file actions
220 lines (186 loc) · 7.12 KB
/
scatter_plot.html
File metadata and controls
220 lines (186 loc) · 7.12 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
<!DOCTYPE html>
<meta charset="utf-8">
<html lang="en">
<head>
<title>Visualization Programming Using D3.js</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<style>
/* Remove the navbar's default margin-bottom and rounded borders */
.navbar {
margin-bottom: 0;
border-radius: 0;
}
/* Set height of the grid so .sidenav can be 100% (adjust as needed) */
.row.content {height: 450px}
/* Set gray background color and 100% height */
.sidenav {
padding-top: 20px;
background-color: #f1f1f1;
height: 100%;
}
/* Set black background color, white text and some padding */
footer {
background-color: #555;
color: white;
padding: 15px;
}
/* On small screens, set height to 'auto' for sidenav and grid */
@media screen and (max-width: 767px) {
.sidenav {
height: auto;
padding: 15px;
}
.row.content {height:auto;}
}
</style>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Visualization Programming using D3.js</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
</div>
</div>
</nav>
<div class="container-fluid text-center">
<div class="row content">
<div class="col-sm-2 sidenav">
<p><b>Background Information</b></p>
<p>Dataset used: <a href="https://github.com/IsaacMwendwa/dataViz">a1-grocerystoresurvey.csv</a></p>
<p>The interactivity of the scatter plot is explored by adjusting the limits of the
X-axis, in steps of 10,000, or to whichever value is desired.</p>
<p>This gives a closer inspection of the Visualization</p>
</div>
<div class="col-sm-8 text-left">
<h1>Visualization Programming using D3.js: Scatter Plot</h1>
<!--div where the graph will take place -->
<div id="my_dataviz">
<script>
// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 30, left: 60},
width = 460 - margin.left - margin.right,
height = 400 - 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/IsaacMwendwa/dataViz/main/a1-grocerystoresurvey.csv", function(data) {
// Defining graph title and axis labels
const xAxisLabel = "Income";
const yAxisLabel = "PurchaseAmount";
const title = `Scatter Plot of ${yAxisLabel} vs ${xAxisLabel}`;
// Add X axis
var x = d3.scaleLinear()
.domain([0, 100000])
.range([ 0, width ]);
var xAxis = svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Add Y axis
var y = d3.scaleLinear()
.domain([0, 700])
.range([ height, 0]);
svg.append("g")
.call(d3.axisLeft(y));
//Create Title
svg.append("text")
.attr("x", width / 2 )
.attr("y", 0)
.style("text-anchor", "middle")
.text(title);
//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);
// Add dots
svg.append('g')
.selectAll("dot")
.data(data)
.enter()
.append("circle")
.attr("cx", function (d) { return x(d.Income); } )
.attr("cy", function (d) { return y(d.PurchaseAmount); } )
.attr("r", 5)
.style("fill", "#69b3a2" )
// A function that update the plot for a given xlim value
function updatePlot() {
// Get the value of the button
xlim = this.value
// Update X axis
x.domain([3,xlim])
xAxis.transition().duration(1000).call(d3.axisBottom(x))
// Update chart
svg.selectAll("circle")
.data(data)
.transition()
.duration(1000)
.attr("cx", function (d) { return x(d.Income); } )
.attr("cy", function (d) { return y(d.PurchaseAmount); } )
}
// Add an event listener to the button created in the html part
d3.select("#buttonXlim").on("input", updatePlot )
})
</script>
<!-- Add a radio button -->
<label>Change X-axis Limits</label>
<input type="number" min="1000" max="100000" step="10000" value="50000" id="buttonXlim">
</div>
<div class="col-sm-8 text-left">
<h3>Description of Findings</h3>
<p>The scatter plot of PurchaseAmount versus Income yields a closely knit plot, depicting that
the two variables are strongly correlated.</p>
<p>The scatter plot also shows that when there is a weak correlation between income and PurchaseAmount,
when the PurchaseAmount exceeds 250</p>
<p></p>
</div>
</div>
<div class="col-sm-2 sidenav">
<div class="well">
<p><b>Visualizations used</b></p>
<p><a href=index.html>Bubble Plot</a></p>
<p><a href=#>Scatter Plot</a></p>
<p><a href=histogram.html>Histogram</a></p>
<p><a href=heatmap.html>Heatmap</a></p>
<p><a href=density_plot.html>Density Plot</a></p>
</div>
<div class="well">
<p><b>Interactivity</b></p>
<p>The visualizations provide various forms of interactivity to the user, including
tooltips by hovering the mouse over a cell, adjusting x-axis, changing categories to visualize </p>
</div>
</div>
</div>
</div>
<footer class="container-fluid text-center">
<p>Visualization Programming using D3.js</p>
</footer>
</body>
</html>