-
Notifications
You must be signed in to change notification settings - Fork 47
/
step-7-a-basic-bar-chart.html
73 lines (65 loc) · 2.08 KB
/
step-7-a-basic-bar-chart.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Step 7 - A Basic Bar Chart</title>
<link rel="stylesheet" href="normalize.css">
<style>
#chart {
height: 360px;
margin: 0 auto; /* NEW */
position: relative;
width: 360px;
}
rect {
fill: steelblue; /* NEW */
}
h1 { /* NEW */
font-size: 14px; /* NEW */
text-align: center; /* NEW */
} /* NEW */
</style>
</head>
<body>
<h1>Toronto Parking Tickets by Weekday in 2012</h1> <!-- NEW -->
<div id="chart"></div>
<script src="d3.v4.min.js"></script>
<script>
(function(d3) {
'use strict';
var width = 360;
var height = 360;
var svg = d3.select('#chart')
.append('svg')
.attr('width', width)
.attr('height', height)
.append('g');
var x = d3.scaleBand()
.range([0, width])
.paddingInner(0.1)
.round(true);
var y = d3.scaleLinear()
.range([height, 0]);
d3.csv('weekdays.csv', function(error, dataset) {
dataset.forEach(function(d) {
d.count = +d.count;
});
x.domain(dataset.map(function(d) {
return d.label;
}));
y.domain([0, d3.max(dataset, function(d) {
return d.count;
})]);
svg.selectAll('rect')
.data(dataset)
.enter()
.append('rect')
.attr('x', function(d) { return x(d.label); })
.attr('y', function(d) { return y(d.count); })
.attr('width', x.bandwidth())
.attr('height', function(d) { return height - y(d.count); });
});
})(window.d3);
</script>
</body>
</html>