-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathbarChart.html
160 lines (141 loc) · 4.9 KB
/
barChart.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script type="text/javascript" src="d3/d3.v3.js"></script>
<title>d3 Examples</title>
</head>
<body>
<header>
<div class="container">
<div class="row">
<div class="col-md-2">
<img src="resources/d3logo.png">
</div>
<div class="col-md-10">
<h1 class="logo">D3 Examples</h1>
<h4>Richard Dalton</h4>
<div class="btn-group btn-group-md" role="group" aria-label="...">
<a href="index.html" class="btn btn-default" role="button">Home</a>
<a href="svg.html" class="btn btn-default" role="button">SVG</a>
<a href="barChart.html" class="btn btn-default" role="button">Bar</a>
<a href="pieChart.html" class="btn btn-default" role="button">Pie</a>
<a href="bubbleChart.html" class="btn btn-default" role="button">Bubble</a>
<a href="force.html" class="btn btn-default" role="button">Force</a>
<a href="globe.html" class="btn btn-default" role="button">Globe</a>
<a href="https://d3js.org" class="btn btn-default" role="button">D3.js</a>
</div>
</div>
</div>
</div>
</header>
<div class="container">
<h3>Bar Chart</h3>
<p>We can draw a bar chart by usig svg to render a rectangle of the appropriate size for each datapoint.</p>
<p>In this example, we also use a D3 logarithmic scale, to map the domain of values 5-1300 to the range of
bar heights 0-500.</p>
<div class="col-md-6">
<div id="draw_here"></div>
</div>
<div class="col-md-6">
<pre>
var w = 500;
var h = 500;
var barPadding = 1;
var dataset = [ 5, 10, 100, 279, 500, 25, 750, 1200, 800, 1300,
110, 102, 15, 200, 175, 168, 180, 230, 205 ];
var scale = d3.scale.log();
scale.domain([5, 1300]);
scale.range([0, 500]);
var svg = d3.select("#draw_here")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d, i) {
return i * (w / dataset.length);
})
.attr("y", function(d) {
return h - scale(d);
})
.attr("width", w / dataset.length - barPadding)
.attr("height", function(d) {
return scale(d);
})
.attr("fill", function(d) {
return "rgb(0, 0, " + (d * 10) + ")";
});
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(function(d) {
return d;
})
.attr("text-anchor", "middle")
.attr("x", function(d, i) {
return i * (w / dataset.length) + (w / dataset.length - barPadding) / 2;
})
.attr("y", function(d) {
return h - scale(d) + 14;
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "white");
</pre>
</div>
</div>
<script type="text/javascript">
var w = 500;
var h = 500;
var barPadding = 1;
var dataset = [ 5, 10, 100, 279, 500, 25, 750, 1200, 800, 1300,
110, 102, 15, 200, 175, 168, 180, 230, 205 ];
var scale = d3.scale.log();
scale.domain([5, 1300]);
scale.range([0, 500]);
var svg = d3.select("#draw_here")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d, i) {
return i * (w / dataset.length);
})
.attr("y", function(d) {
return h - scale(d);
})
.attr("width", w / dataset.length - barPadding)
.attr("height", function(d) {
return scale(d);
})
.attr("fill", function(d) {
return "rgb(0, 0, " + (d * 10) + ")";
});
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(function(d) {
return d;
})
.attr("text-anchor", "middle")
.attr("x", function(d, i) {
return i * (w / dataset.length) + (w / dataset.length - barPadding) / 2;
})
.attr("y", function(d) {
return h - scale(d) + 14;
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "white");
</script>
</body>
</html>