-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
292 lines (221 loc) · 5.82 KB
/
index.js
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import * as d3 from 'd3'
/**
* Default config.
*/
const defaults = {
// target element or selector to contain the svg
target: '#chart',
// width of chart
width: 500,
// height of chart
height: 130,
// margin
margin: { top: 15, right: 0, bottom: 35, left: 60 },
// enable axis
axis: true,
// axis padding
axisPadding: 5,
// axis tick size
tickSize: 10,
// padding between bars
barPadding: 13,
// nice round values for axis
nice: false,
// custom x domain
xDomain: null,
// custom y domain
yDomain: null,
// color range
color: null,
// color interpolation
colorInterpolate: d3.interpolateHcl,
// easing function for transitions
ease: 'easeLinear',
// type of bar: rounded-rect, rect
type: 'rounded',
// mouseover callback for tooltips or value display
mouseover: _ => {},
// mouseout callback for tooltips or value display
mouseout: _ => {}
}
/**
* Zeroed margin.
*/
const zeroMargin = { top: 0, right: 0, bottom: 0, left: 0 }
/**
* BarChart.
*/
export default class BarChart {
/**
* Construct with the given `config`.
*/
constructor(config) {
this.set(config)
if (!this.axis) this.margin = zeroMargin
this.init()
}
/**
* Set configuration options.
*/
set(config) {
Object.assign(this, defaults, config)
}
/**
* Dimensions without margin.
*/
dimensions() {
const { width, height, margin } = this
const w = width - margin.left - margin.right
const h = height - margin.top - margin.bottom
return [w, h]
}
/**
* Handle mouseover.
*/
onMouseOver() {
const m = d3.mouse(this.chart.node())
const x = this.x.invert(m[0])
const i = this.xBisect(this.data, x, 1)
const data = this.data[i - 1]
this.mouseover(data)
}
/**
* Handle mouseleave.
*/
onMouseLeave() {
this.mouseout()
}
/**
* Initialize the chart.
*/
init() {
const { target, width, height, margin, axisPadding, tickSize, axis } = this
const { color, colorInterpolate } = this
const [w, h] = this.dimensions()
this.chart = d3.select(target)
.attr('width', width)
.attr('height', height)
.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`)
.on('mouseover', _ => this.onMouseOver())
.on('mouseleave', _ => this.onMouseLeave())
if (color) {
this.color = d3.scaleLinear()
.interpolate(colorInterpolate)
.range(color)
}
this.x = d3.scaleTime()
.range([0, w])
this.y = d3.scaleLinear()
.range([h, 0])
this.xAxis = d3.axisBottom()
.scale(this.x)
.ticks(5)
.tickPadding(8)
.tickSize(tickSize)
this.yAxis = d3.axisLeft()
.scale(this.y)
.ticks(3)
.tickPadding(8)
.tickSize(tickSize)
if (axis) {
this.chart.append('g')
.attr('class', 'x axis')
.attr('transform', `translate(0, ${h+axisPadding})`)
.call(this.xAxis)
this.chart.append('g')
.attr('class', 'y axis')
.attr('transform', `translate(${-axisPadding}, 0)`)
.call(this.yAxis)
}
this.xBisect = d3.bisector(d => d.bin).left
this.ease = d3[this.ease]
}
/**
* Render axis.
*/
renderAxis(data, options) {
const { chart, x, y, xAxis, yAxis, nice, t, color, xDomain, yDomain } = this
const yExtent = yDomain || d3.extent(data, d => d.value)
const xd = x.domain(xDomain || d3.extent(data, d => d.bin))
const yd = y.domain(yExtent)
if (color) color.domain(yExtent)
if (nice) {
xd.nice()
yd.nice()
}
const c = chart.transition(t)
c.select('.x.axis').call(xAxis)
c.select('.y.axis').call(yAxis)
}
/**
* Render bars.
*/
renderBars(data, { animate }) {
const { chart, x, y, t, barPadding, type, color } = this
const [w, h] = this.dimensions()
const width = w / data.length
const barWidth = width - barPadding
if (barWidth < 1) throw new Error('BarChart is too small for the amount of data provided')
const column = chart.selectAll('.column')
.data(data)
column.enter() // enter
.append('rect')
.attr('class', 'column')
.merge(column) // update
.transition(t)
.attr('x', d => x(d.bin))
.attr('rx', type == 'rounded' ? barWidth / 2 : 0)
.attr('ry', type == 'rounded' ? barWidth / 2 : 0)
.attr('width', barWidth)
.attr('height', h)
// exit
column.exit().remove()
const bar = chart.selectAll('.bar')
.data(data)
bar.enter() // enter
.append('rect')
.attr('class', 'bar')
.merge(bar) // update
.transition(t)
.attr('x', d => x(d.bin))
.attr('y', d => y(d.value))
.attr('rx', type == 'rounded' ? barWidth / 2 : 0)
.attr('ry', type == 'rounded' ? barWidth / 2 : 0)
.attr('width', barWidth)
.attr('height', d => h - y(d.value))
if (color) bar.style('fill', d => color(d.value))
// exit
bar.exit().remove()
const overlay = chart.selectAll('.overlay')
.data(data)
// enter
overlay.enter().append('rect')
.attr('class', 'overlay')
// update
overlay.attr('x', d => x(d.bin))
.attr('width', width)
.attr('height', h)
.style('fill', 'transparent')
// exit
overlay.exit().remove()
}
/**
* Render the chart against the given `data` which should be
* an array of objects with `bin` and `value` properties.
*/
render(data, options = {}) {
this.data = data
this.t = d3.transition()
.duration(options.animate ? 300 : 0)
.ease(this.ease)
this.renderAxis(data, options)
this.renderBars(data, options)
}
/**
* Update the chart against the given `data`.
*/
update(data, options = { animate: true }) {
this.render(data, options)
}
}