-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistogram.html
247 lines (196 loc) · 10.1 KB
/
histogram.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
<!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>
<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="index.html" 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="#" 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-mutualfunds.csv</p>
<p><b>Interactivity</b></p>
<p>The histogram offers interacivity to the user by permitting the user to change the
number of bins used to plot the histogram. This gives a deeper inspection of the graph
</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: Histogram</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: 10, right: 30, bottom: 30, left: 40},
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 + ")");
// get the data
d3.csv("https://raw.githubusercontent.com/vjajala/dataViz/main/a1-mutualfunds.csv", function(data) {
// Defining graph title and axis labels
const xAxisLabel = "Mgr_tenure";
const yAxisLabel = "Frequency";
const title = `Histogram of ${xAxisLabel} of Various Companies`;
// X axis: scale and draw:
var x = d3.scaleLinear()
.domain([0, 40]) // can use this instead of 1000 to have the max of data: d3.max(data, function(d) { return +d.price })
.range([0, width]);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Y axis: initialization
var y = d3.scaleLinear()
.range([height, 0]);
var yAxis = svg.append("g")
//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);
// A function that builds the graph for a specific value of bin
function update(nBin) {
// set the parameters for the histogram
var histogram = d3.histogram()
.value(function(d) { return d.Mgr_tenure; }) // I need to give the vector of value
.domain(x.domain()) // then the domain of the graphic
.thresholds(x.ticks(nBin)); // then the numbers of bins
// And apply this function to data to get the bins
var bins = histogram(data);
// Y axis: update now that we know the domain
y.domain([0, d3.max(bins, function(d) { return d.length; })]); // d3.hist has to be called before the Y axis obviously
yAxis
.transition()
.duration(1000)
.call(d3.axisLeft(y));
// Join the rect with the bins data
var u = svg.selectAll("rect")
.data(bins)
// Manage the existing bars and eventually the new ones:
u
.enter()
.append("rect") // Add a new rect for each new elements
.merge(u) // get the already existing elements as well
.transition() // and apply changes to all of them
.duration(1000)
.attr("x", 1)
.attr("transform", function(d) { return "translate(" + x(d.x0) + "," + y(d.length) + ")"; })
.attr("width", function(d) { return x(d.x1) - x(d.x0) -1 ; })
.attr("height", function(d) { return height - y(d.length); })
.style("fill", "#69b3a2")
// If less bar in the new histogram, I delete the ones not in use anymore
u
.exit()
.remove()
}
// Initialize with 20 bins
update(20)
// Listen to the button -> update if user change it
d3.select("#nBin").on("input", function() {
update(+this.value);
});
});
</script>
<p>
<label>Change Number of Bins</label>
<input type="number" min="0" max="40" step="2" value="10" id="nBin">
</p>
</div>
</div>
</div>
<div class="w3-row">
<div class="w3-twothird w3-container">
<h3 class="w3-text-teal">Description of Results</h3>
<p>The histogram provides functionality for changing the number of bins used for plotting.
Increasing the number of bins to around 40 shows that most companies have an average Mgr_tenure of between 0 to 5 years
</p>
<p>Additionally, the histogram shows that an Mgr_tenure of about 2 years has the highest frequency, implying that most
companies have their managers serving for a term of about 2 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>