Skip to content

Commit 43a507a

Browse files
committed
Initial work on bar chart
1 parent d887e35 commit 43a507a

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

step-7-a-basic-bar-chart.html

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Step 7 - A Basic Bar Chart</title>
6+
<link rel="stylesheet" href="normalize.css">
7+
<style>
8+
#chart {
9+
height: 360px;
10+
margin: 0 auto; /* NEW */
11+
position: relative;
12+
width: 360px;
13+
}
14+
rect {
15+
fill: steelblue; /* NEW */
16+
}
17+
h1 { /* NEW */
18+
font-size: 14px; /* NEW */
19+
text-align: center; /* NEW */
20+
} /* NEW */
21+
</style>
22+
</head>
23+
<body>
24+
<h1>Toronto Parking Tickets by Weekday in 2012</h1> <!-- NEW -->
25+
<div id="chart"></div>
26+
<script src="d3.v3.min.js"></script>
27+
<script>
28+
(function(d3) {
29+
'use strict';
30+
31+
var width = 360;
32+
var height = 360;
33+
34+
var svg = d3.select('#chart')
35+
.append('svg')
36+
.attr('width', width)
37+
.attr('height', height)
38+
.append('g');
39+
40+
var x = d3.scale.ordinal()
41+
.rangeBands([0, width], 0.1);
42+
43+
var y = d3.scale.linear()
44+
.range([height, 0]);
45+
46+
d3.csv('weekdays.csv', function(error, dataset) {
47+
dataset.forEach(function(d) {
48+
d.count = +d.count;
49+
});
50+
51+
x.domain(dataset.map(function(d) {
52+
return d.label;
53+
}));
54+
55+
y.domain([0, d3.max(dataset, function(d) {
56+
return d.count;
57+
})]);
58+
59+
svg.selectAll('rect')
60+
.data(dataset)
61+
.enter()
62+
.append('rect')
63+
.attr('x', function(d) { return x(d.label); })
64+
.attr('y', function(d) { return y(d.count); })
65+
.attr('width', x.rangeBand())
66+
.attr('height', function(d) { return height - y(d.count); });
67+
});
68+
})(window.d3);
69+
</script>
70+
</body>
71+
</html>

0 commit comments

Comments
 (0)