forked from fraserxu/react-chartist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
85 lines (69 loc) · 2.16 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
'use strict';
import React, {Component} from 'react';
const Chartist = require('chartist');
class ChartistGraph extends Component {
displayName: 'ChartistGraph'
shouldComponentUpdate(newProps) {
const { data, className, type, options, style, responsiveOptions } = this.props;
return data !== newProps.data
|| className !== newProps.className
|| type !== newProps.type
|| options !== newProps.options
|| style !== newProps.style
|| responsiveOptions !== newProps.responsiveOptions;
}
componentWillUpdate(newProps) {
this.updateChart(newProps);
}
shouldComponentUpdate(nextProps, nextStates) {
return nextProps != this.props;
}
componentWillUnmount() {
if (this.chartist) {
try {
this.chartist.detach();
}
catch (err) {
throw new Error('Internal chartist error', err);
}
}
}
componentDidMount() {
this.updateChart(this.props);
}
updateChart(config) {
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.refs.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 = this.props.className ? ' ' + this.props.className : '';
const style = this.props.style ? this.props.style : {};
return (<div className={'ct-chart' + className} ref='chart' style={style} />)
}
}
ChartistGraph.propTypes = {
type: React.PropTypes.string.isRequired,
data: React.PropTypes.object.isRequired,
className: React.PropTypes.string,
options: React.PropTypes.object,
responsiveOptions: React.PropTypes.array,
style: React.PropTypes.object
}
export const Graph = ChartistGraph;
export const Interpolation = Chartist.Interpolation;
export const Axis = Chartist.Axis;