-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
190 lines (147 loc) · 3.86 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PSET8</title>
<script type="text/javascript" src="https://d3js.org/d3.v7.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,500" rel="stylesheet">
<style type="text/css">
.label_styling {
font-family:"Open Sans";
font-weight: 300;
font-size: 14px;
}
.title_styling {
font-family:"Open Sans";
font-weight: 500;
font-size: 18px;
}
.source_styling {
font-family:"Open Sans";
font-weight: 300;
font-size: 10px;
}
h1 {
font-family:"Open Sans";
font-size:50px;
font-weight:700;
line-height:1.3;
}
h2 {
font-family:"Open Sans";
font-size:18px;
font-weight:700;
}
.yAxis line {
display : none;
}
.tick text{
font-size: 14px;
}
#tooltip {
position: absolute;
padding: 5px;
background-color: white;
border: solid gray 1px;
Z-index:1;
}
#tooltip.hidden {
display: none;
}
</style>
</head>
<body>
<main role="main" class="container" >
<div class="row">
<br><br>
<h1>Boston 311 Requests</h1>
<hr>
</div>
<div class="row">
<div class="col-7">
<div id="bar-chart"></div>
<div id="tooltip" class="hidden">
<p>
<span id="value">100</span>
</p>
</div>
</main>
<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<!-- Custom JS -->
<script type="text/javascript">
const width = 1500;
const height = 1000;
const marginLeft = 300;
const marginTop = 30;
const marginRight = 30;
const marginBottom = 30;
const barHeight = 30;
const gap = 5;
//define svg
const svg = d3.select("#bar-chart")
.append("svg")
.attr("viewBox",[0, 0, width, height + marginBottom])
function plotBarChart(dataset) {
//define x axis
const xValues = dataset.map(d => d.total_count);
const x = d3.scaleLinear()
.domain([0,d3.max(xValues)])
.range([0,width-marginLeft-marginRight]);
const xAxis = d3.axisTop(x)
.ticks(6)
.tickSize(14);
// define scales
// y linear scale
const yValues = dataset.map (d => d.Name);
const y = d3.scaleBand()
.domain(yValues)
.range([height, 0],0.5);
y.paddingInner(0.2);
const yAxis = d3.axisLeft(y)
.tickPadding(0.15);
svg.append("g")
.attr("class", "bars")
.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("y", d => marginTop + y(d.Name))
.attr("x", marginLeft)
.attr("width", d => x(d.total_count))
.attr("height", barHeight)
.attr("fill", "teal")
.on("mouseover", function(event,d) {
d3.select("#tooltip")
.style("left", event.pageX + "px")
.style("top", event.pageY + "px")
.select("#value")
.html("<p>" + String(d.total_count) +"</p>")
d3.select("#tooltip")
.classed("hidden", false);
})
.on("mouseout", function() {
d3.select("#tooltip")
.classed("hidden", true);
});
//add x axis
svg.append("g")
.call(xAxis)
.attr("transform", "translate ("+ marginLeft + "," + marginTop +")");
//add y axis
svg.append("g")
.call(yAxis)
.attr("class", "yAxis")
.attr("transform", "translate ("+ marginLeft + "," + marginTop +")");
};
// load csv data
d3.csv("./boston_311.csv", d3.autoType)
.then(function(data) {
data.sort(function (b, a) {return b.total_count - a.total_count;});
console.log(data);
plotBarChart(data);
})
</script>
</body>
</html>