Simple interactive stacked bar graph with D3.js
16 Feb 2016A picture is worth a thousand words. And It will be more funny and effective if we can interact with the chart.
The hardwares and the web technologies make it possible for average person to achieve interactive data visualization on the Internet. Among these web technologies, JavaScript equipped with many extension packages is the most powerful one. Here is an example using D3.js to make an interactive stacked bar chart.
Stacked Bars
Original codes
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var data = [{apple:3,banana:4,orange:5,year:2011},
{apple:4,banana:7,orange:2,year:2012},
{apple:1,banana:5,orange:6,year:2013},
{apple:2,banana:2,orange:2,year:2014},
{apple:7,banana:1,orange:8,year:2015}]
var keys=d3.keys(data[0])
.filter(function(key) {
return key!=='year';
})
var yearFruit=keys.map(function(fruit) {
return data.map(function(d) {
return {
x:d.year,
y:+d[fruit]
}
})
})
var stackedData=d3.layout.stack()(yearFruit)
var width=300,height=400
var svg=d3.select('#stacked_bar')
.append('svg')
.attr({
width:width+30,
height:height+30
})
.style({
display:'block',
margin:'0 auto'
})
svg.append('rect')
.attr({
width:width+30,
height:height+30,
fill:'white',
stroke:'black',
'stroke-width':1
})
var mainGroup=svg.append('g')
.attr('transform','translate('+25+','+7+')')
mainGroup.append('rect')
.attr({
fill:'rgba(0,0,0,0.1)',
width:width,
height:height
})
var yScale=d3.scale.linear()
.domain([0,
d3.max(stackedData,function(d) {
return d3.max(d,function (d) {
return d.y0+d.y
})
})
])
.range([0,height])
var xScale=d3.scale.ordinal()
.domain(d3.range(stackedData[0].length))
.rangeRoundBands([0,width],0.05)
var colors=d3.scale.ordinal()
.range(['red','yellow','orange'])
var groups=mainGroup.selectAll('g')
.data(stackedData)
.enter()
.append('g')
.attr({
fill:function(d,i) {
return colors(i)
},
class:function(d,i) {
return keys[i]
}
})
groups.selectAll('rect')
.data(function(d) {return d})
.enter()
.append('rect')
.attr({
x:function(d,i) {
return xScale(i)
},
y:function(d,i) {
return height-yScale(d.y)-yScale(d.y0)
},
height:function(d) {
return yScale(d.y)
},
width:xScale.rangeBand()
})
.on('mouseover',function(d) {
d3.select(this)
.transition()
.duration(500)
.attr({
fill:'rgba(0,0,250,0.5)',
'stroke-width':3,
stroke:'white'
})
})
.on('mouseout',function(d) {
d3.select(this)
.transition()
.duration(500)
.attr({
fill:d3.select(this.parentNode).attr('fill'),
stroke:'none'
})
})
groups.selectAll('text')
.data(function(d) {return d})
.enter()
.append('text')
.text(function(d,i) {
return d3.select(this.parentNode).attr('class')+':'+d.y
})
.attr({
x:function(d,i) {
return xScale(i)+xScale.rangeBand()/2
},
y:function(d,i) {
return height-yScale(d.y)/2-yScale(d.y0)
},
fill:function(d) {
return d3.select(this.parentNode).attr('fill')
},
'alignment-baseline': 'middle',
'text-anchor': 'middle'
})
.style({
'font-size':'12px',
'font-weight':'bold',
'text-transform':'capitalize',
'font-family':'sans-serif'
})
var xAxis=d3.svg.axis().orient('bottom').scale(xScale)
.tickFormat(function(i) {return data[i].year})
var xAxisGroup=svg.append('g')
xAxisGroup
.attr({transform:'translate('+25+','+(height+7)+')'})
.style({
'font-size':'14px',
'font-family':'serif'
})
var xAxisNodes=xAxisGroup.call(xAxis)
var xDomain=xAxisNodes.selectAll('.domain')
var xTicks=xAxisNodes.selectAll('.tick line')
xDomain.attr({
fill:'none',
'stroke-width':1,
stroke:'black'
})
xTicks.attr({
fill:'none',
'stroke-width':1,
stroke:'black'
})
var revYScale=d3.scale.linear()
.domain([0,
d3.max(stackedData,function(d) {
return d3.max(d,function (d) {
return d.y0+d.y
})
})
])
.range([height,0])
var yAxis=d3.svg.axis().orient('left').scale(revYScale)
var yAxisGroup=svg.append('g')
yAxisGroup
.attr({transform:'translate('+23+','+7+')'})
.style({
'font-size':'14px',
'font-family':'serif'
})
var yAxisNodes=yAxisGroup.call(yAxis)
var yDomain=yAxisNodes.selectAll('.domain')
var yTicks=yAxisNodes.selectAll('.tick line')
yDomain.attr({
fill:'none',
'stroke-width':1,
stroke:'black'
})
yTicks.attr({
fill:'none',
'stroke-width':1,
stroke:'black'
})
</script>