-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chart.ts
328 lines (294 loc) · 7.87 KB
/
Chart.ts
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import { select } from 'd3-selection'
import Tooltip from '../Tooltip'
import {
addColoursToConfig,
transformDataKeys,
throttle,
debounce,
} from '../../utilities'
import {
ChartConfig,
ChartData,
ChartEventListener,
ChartParams,
ChartType,
DivSelection,
Padding,
SVGSelection,
} from '../../types'
import Key from '../Key/Key'
import Axis from '../Axis/Axis'
/**
* The parent class for all types of Chart used to initialise all of the base settings universal to all charts.
* In handles data setting/updating, document resizing and includes the tooltip.
*
* @public
*/
export class Chart {
/**
* SVG DOM object for displaying the chart
*/
protected svg: SVGSVGElement
/**
* SVG d3 object for d3 operations on the chart
*/
protected d3Svg: SVGSelection
/**
* Default time for d3 transitions on the chart
*/
protected transitionTime: number = 500
/**
* DOM reference to container element that wraps SVG
*/
private container!: HTMLElement
/**
* DOM reference to loader display element
*/
private loader: HTMLElement
/**
* d3 reference to chart title element
*/
private d3Title?: DivSelection
/**
* Chart's tooltip object instance
*/
protected tooltip!: Tooltip
/**
* The current calculated width of the chart
*/
protected width!: number
/**
* The current calculated height of the chart
*/
protected height!: number
/**
* The current calculated inner width of the chart
*/
protected innerWidth!: number
/**
* The current calculated inner height of the chart
*/
protected innerHeight!: number
/**
* The width before any browser resize
*/
private initialWidth!: number
/**
* The padding for the chart within the container
*/
protected padding: Padding = {
l: 5,
r: 5,
t: 5,
b: 5,
}
/**
* The chart's config object
*/
protected chartConfig!: ChartConfig
/**
* The chart's data array
*/
protected chartData!: ChartData
/**
* The chart's type
*/
protected chartType: ChartType = 'bar'
/**
* The chart's key object instance
*/
protected key!: Key
/**
* The chart's axis object instance
*/
protected axis!: Axis
/**
* An event watcher for the user resizing the browser
*/
protected resizeWatcher!: ChartEventListener
/**
* An event watcher for the user moving the mouse out of the chart
*/
protected chartOutWatcher!: ChartEventListener
/**
* The chart's resize offset
*/
protected resizeOffset: number = 0
/**
* Whether to transform keys from an unknown data schema
*/
protected doTransform: boolean
constructor(chartParams = {} as ChartParams) {
const {
chartConfig,
chartData,
containerId,
shouldAddColors,
doTransform,
transitionTime,
container,
} = chartParams
this.svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
this.d3Svg = select(this.svg)
this.transitionTime = transitionTime ?? this.transitionTime
this.padding = chartConfig?.padding ?? this.padding
this.loader = document.createElement('div')
this.doTransform = Boolean(doTransform)
this.setContainer(container ?? document.getElementById(containerId ?? ''))
this.setConfig(chartConfig, shouldAddColors)
this.setData(chartData)
}
/**
* Sets the local container object.
*#
* @param {HTMLElement} container Required DOM element
* @throws {Error} invalid DOM element
*/
private setContainer = (container: HTMLElement | null) => {
if (container?.nodeName) {
container.appendChild(this.loader)
this.container = container
} else {
throw new Error('No valid DOM element or ID provided for chart.')
}
}
/**
* Sets the local config options for the chart.
*
* @param {ChartConfig} chartConfig JSON configuration object
* @param {boolean} shouldAddColors determine whether to add random colours to the config
* @throws {Error} missing configuration
*/
private setConfig = (chartConfig: ChartConfig, shouldAddColors = false) => {
const shouldUpdateTrim = this.chartConfig?.doTrim !== chartConfig?.doTrim
if (chartConfig && chartConfig.toString() === '[object Object]') {
this.chartConfig = structuredClone(chartConfig)
if (this.chartConfig?.itemValues && shouldAddColors) {
this.chartConfig.itemValues = addColoursToConfig(
this.chartConfig.itemValues
)
}
shouldUpdateTrim && this.setDimensions()
} else {
throw new Error('No valid configuration provided for chart.')
}
}
/**
* Sets the local data for the chart.
*
* @param {ChartData} chartData array of JSON objects
* @throws {Error} missing data
*/
private setData = (chartData: ChartData) => {
if (chartData && Array.isArray(chartData) === true) {
this.chartData = structuredClone(chartData)
if (this.chartConfig && this.chartData && this.doTransform) {
this.chartData = transformDataKeys(this.chartConfig, this.chartData)
}
} else {
throw new Error('No valid data provided for chart.')
}
}
/**
* Updates the local config for the chart.
*
* @method updateConfig
* @param {ChartConfig} chartConfig config JSON style object
*/
public updateConfig(chartConfig: ChartConfig) {
this.setConfig(chartConfig)
if (this.renderChart) {
this.renderChart()
}
}
/**
* Updates the local data for the chart.
*
* @param {ChartData} chartData array of JSON objects
*/
public updateData(chartData: ChartData, bRender = true) {
this.setData(chartData)
this.setDimensions()
if (this.axis) {
this.axis.render()
}
if (this.renderChart && bRender) {
this.renderChart()
}
}
/**
* Sets the local chart dimensions based on the size of the container.
*
* @method setDimensions
* @throws {Error} missing DOM element
*/
protected setDimensions() {
if (this.container && this.container.nodeName) {
this.width = this.container.clientWidth
this.height = this.container.clientHeight
this.innerWidth = this.width - this.padding.l - this.padding.r
this.innerHeight = this.height - this.padding.t - this.padding.b
} else {
throw new Error(
'Cannot set dimensions of chart without container element.'
)
}
}
/**
* Render the chart
*/
protected renderChart() {
if (this.d3Title && this.chartConfig?.title) {
this.d3Title.text(this.chartConfig.title)
}
}
/**
* Check chart is ready and render.
*
* @throws {Error} chart not ready for initialisation
*/
public init() {
if (!this.container) return
this.setDimensions()
if (
this.chartData &&
this.chartConfig &&
!isNaN(this.width) &&
!isNaN(this.height)
) {
this.initialWidth = this.width
this.tooltip = new Tooltip(this.container).create()
this.d3Title = select(this.container).append('div').attr('class', 'title')
this.svg.setAttribute('class', 'chart')
this.container.appendChild(this.svg)
const throttledResize = throttle(() => this.onResize(), 100)
const debouncedResize = debounce(() => this.onResize(), 1500)
const onHideTooltip = () => this.tooltip?.hide()
this.resizeWatcher ??= window.addEventListener('resize', () => {
throttledResize()
debouncedResize()
})
this.chartOutWatcher ??= this.svg.addEventListener(
'mouseout',
onHideTooltip
)
this.renderChart?.()
this.loader && this.container.removeChild(this.loader)
return this
}
throw new Error('The chart is not ready for initialisation.')
}
/**
* Resize the chart
*
*/
public onResize() {
this.setDimensions()
this.resizeOffset = this.width - this.initialWidth
if (this.renderChart) {
this.renderChart()
}
this.tooltip?.hide()
}
}
export default Chart