-
Notifications
You must be signed in to change notification settings - Fork 35
/
01_bar.html
109 lines (87 loc) · 2.44 KB
/
01_bar.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3: Bar chart</title>
<script type="text/javascript" src="d3.js"></script>
<style type="text/css">
body {
background-color: gray;
}
svg {
background-color: white;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
</style>
</head>
<body>
<script type="text/javascript">
//Width, height, padding
var w = 600;
var h = 250;
var padding = 25;
//Array of dummy data values (simple!)
var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];
//Configure x and y scale functions
//Note each scale accounts for padding
var xScale = d3.scale.ordinal()
.domain(d3.range(dataset.length))
.rangeRoundBands([ padding, w - padding ], 0.05);
//Note y scale inverts values, so larger inputs return smaller
//outputs. That's because we want the y value for the "top"
//of each bar. You could flip the two range values to take
//the opposite approach.
var yScale = d3.scale.linear()
.domain([ 0, d3.max(dataset) ])
.rangeRound([ h - padding, padding ]);
//Configure y axis generator
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(5);
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//Create bars
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d, i) {
//x scale is ordinal, so using the index value for each element
//here simply moves subsequent bars farther to the right
return xScale(i);
})
.attr("y", function(d) {
//y scale is linear, so data value sets the starting point
//or "top" of the bar
return yScale(d);
})
.attr("width", xScale.rangeBand()) //All bars have the same width
.attr("height", function(d) {
//We do some math to get the height or bottom edge of each bar,
//by subtracting the scaled value (the "top"), from the height
//of the entire image, minus padding
return h - padding - yScale(d);
})
.attr("fill", "SteelBlue");
//Create y axis
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + padding + ",0)")
.call(yAxis);
</script>
</body>
</html>