-
Notifications
You must be signed in to change notification settings - Fork 95
/
index.js
76 lines (63 loc) · 1.79 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
import React, { Component, cloneElement, Children } from 'react';
import PropTypes from 'prop-types'
class ChartistGraph extends Component {
displayName: 'ChartistGraph'
componentWillUnmount() {
if (this.chartist) {
try {
this.chartist.detach();
} catch (err) {
throw new Error('Internal chartist error', err);
}
}
}
componentDidMount() {
this.updateChart(this.props);
}
componentDidUpdate() {
this.updateChart(this.props);
}
updateChart(config) {
let Chartist = require('chartist');
let { type, data } = config;
let options = config.options || {};
let responsiveOptions = config.responsiveOptions || [];
let event;
if (this.chartist) {
this.chartist.update(data, options, responsiveOptions);
} else {
this.chartist = new Chartist[type](this.chart, data, options, responsiveOptions);
if (config.listener) {
for (event in config.listener) {
if (config.listener.hasOwnProperty(event)) {
this.chartist.on(event, config.listener[event]);
}
}
}
}
return this.chartist;
}
render() {
const { className, style, children, data, type } = this.props;
const childrenWithProps = children && Children.map(children, (child) => (
cloneElement(child, {
type,
data
})
));
return (
<div className={`ct-chart ${className || ''}`} ref={(ref) => this.chart = ref} style={style}>
{childrenWithProps}
</div>
)
}
}
ChartistGraph.propTypes = {
type: PropTypes.oneOf(['Line', 'Bar', 'Pie']).isRequired,
data: PropTypes.object.isRequired,
className: PropTypes.string,
options: PropTypes.object,
responsiveOptions: PropTypes.array,
style: PropTypes.object
}
export default ChartistGraph;