-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
175 lines (137 loc) · 3.31 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
import { select as d3select,
scaleLinear as d3scaleLinear,
max as d3max,
axisLeft as d3axisLeft,
axisRight as d3axisRight,
axisBottom as d3axisBottom,
axisTop as d3axisTop } from 'd3'
/**
* Default config.
*/
const defaults = {
// target element or selector to contain the svg
target: null,
// class of the svg
className: 'bar',
// width of chart
width: 500,
// height of chart
height: 200,
// padding between bars
barPadding: 5,
// margin
margin: { top: 20, right: 20, bottom: 40, left: 40 },
// bars fill color
barsFill: 'steelblue',
// enable axis
enableAxis: false,
// axis orient
axisOrient: { x: 'bottom', y: 'left' },
// axis padding
axisPadding: 5
}
/**
* d3 axis methods map.
*/
const axisMap = {
left: d3axisLeft,
right: d3axisRight,
top: d3axisTop,
bottom: d3axisBottom
}
/**
* BarChart.
*/
export default class Bar {
/**
* Construct with the given `config`.
*/
constructor (options) {
this.set(options)
this.init()
}
/**
* Set configuration options.
*/
set (options) {
Object.assign(this, defaults, options)
}
/**
* 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]
}
/**
* Initialize the chart.
*/
init (options) {
const { target, className, width, height, margin,
enableAxis, axisOrient } = this
const [w, h] = this.dimensions()
this.chart = d3select(target)
.append('svg')
.attr('class', className)
.attr('width', width)
.attr('height', height)
.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`)
this.xScale = d3scaleLinear()
.range([0, w])
this.yScale = d3scaleLinear()
.range([h, 0])
if (enableAxis) {
this.xAxis = axisMap[axisOrient.x](this.xScale)
this.yAxis = axisMap[axisOrient.y](this.yScale)
}
}
/**
* Render axis.
*/
renderAxis (data) {
if (!this.enableAxis) return
const { xScale, yScale, xAxis, yAxis, axisPadding } = this
const [w, h] = this.dimensions()
xScale.domain([0, data.length])
yScale.domain([0, d3max(data, d => d.value)])
this.chart.append('g')
.attr('class', 'x axis')
.attr('transform', `translate(0, ${h + axisPadding})`)
.call(xAxis)
this.chart.append('g')
.attr('class', 'y axis')
.attr('transform', `translate(${-axisPadding}, 0)`)
.call(yAxis)
}
/**
* Render bars.
*/
renderBars (data) {
const { barPadding, yScale, barsFill } = this
const [w, h] = this.dimensions()
const barWidth = w / data.length
yScale.domain([0, d3max(data, d => d.value)])
this.chart
.selectAll('.bar')
.data(data)
.enter()
.append('rect')
.attr('class', 'bar')
.attr('y', d => yScale(d.value))
.attr('width', barWidth - barPadding)
.attr('height', d => h - yScale(d.value))
.attr('transform', (d, i) => `translate(${i * barWidth}, 0)`)
.style('fill', barsFill)
}
/**
* Render the chart through the given `data`
*/
render (data) {
this.data = data
this.renderAxis(data)
this.renderBars(data)
}
}