-
Notifications
You must be signed in to change notification settings - Fork 145
/
react-apexcharts.jsx
148 lines (133 loc) Β· 3.85 KB
/
react-apexcharts.jsx
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
import ApexCharts from 'apexcharts/dist/apexcharts.common'
import React, { Component } from 'react'
import PropTypes from 'prop-types'
window.ApexCharts = ApexCharts
export default class Charts extends Component {
constructor (props) {
super(props)
if (React.createRef) {
this.chartRef = React.createRef()
} else {
this.setRef = el => this.chartRef = el
}
this.chart = null
}
render () {
const { ...props } = this.props
return React.createElement('div', {
ref: React.createRef
? this.chartRef
: this.setRef,
...props
})
}
componentDidMount () {
const current = React.createRef ? this.chartRef.current : this.chartRef
this.chart = new ApexCharts(current, this.getConfig())
this.chart.render()
}
getConfig () {
const { type, height, width, series, options } = this.props
const newOptions = {
chart: {
type,
height,
width
},
series
}
return this.extend(options, newOptions)
}
isObject(item) {
return (
item && typeof item === 'object' && !Array.isArray(item) && item != null
)
}
extend (target, source) {
if (typeof Object.assign !== 'function') {
(function () {
Object.assign = function (target) {
// We must check against these specific cases.
if (target === undefined || target === null) {
throw new TypeError('Cannot convert undefined or null to object')
}
let output = Object(target)
for (let index = 1; index < arguments.length; index++) {
let source = arguments[index]
if (source !== undefined && source !== null) {
for (let nextKey in source) {
if (source.hasOwnProperty(nextKey)) {
output[nextKey] = source[nextKey]
}
}
}
}
return output
}
})()
}
let output = Object.assign({}, target)
if (this.isObject(target) && this.isObject(source)) {
Object.keys(source).forEach((key) => {
if (this.isObject(source[key])) {
if (!(key in target)) {
Object.assign(output, {
[key]: source[key]
})
} else {
output[key] = this.extend(target[key], source[key])
}
} else {
Object.assign(output, {
[key]: source[key]
})
}
})
}
return output
}
componentDidUpdate (prevProps) {
if (!this.chart) return null
const { options, series, height, width } = this.props
const prevOptions = JSON.stringify(prevProps.options)
const prevSeries = JSON.stringify(prevProps.series)
const currentOptions = JSON.stringify(options)
const currentSeries = JSON.stringify(series)
if (
prevOptions !== currentOptions ||
prevSeries !== currentSeries ||
height !== prevProps.height ||
width !== prevProps.width
) {
if (prevSeries === currentSeries) {
// series has not changed, but options or size have changed
this.chart.updateOptions(this.getConfig())
} else if (
prevOptions === currentOptions &&
height === prevProps.height &&
width === prevProps.width
) {
// options or size have not changed, just the series has changed
this.chart.updateSeries(series)
} else {
// both might be changed
this.chart.updateOptions(this.getConfig())
}
}
}
componentWillUnmount () {
if (this.chart && typeof this.chart.destroy === 'function') this.chart.destroy()
}
}
Charts.propTypes = {
type: PropTypes.string.isRequired,
width: PropTypes.any,
height: PropTypes.any,
series: PropTypes.array.isRequired,
options: PropTypes.object.isRequired
}
Charts.defaultProps = {
type: 'line',
width: '100%',
height: 'auto'
}