forked from fraserxu/react-chartist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (54 loc) · 1.62 KB
/
index.js
File metadata and controls
70 lines (54 loc) · 1.62 KB
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
'use strict';
import React, {Component} from 'react';
class ChartistGraph extends Component {
displayName: 'ChartistGraph'
componentWillReceiveProps(newProps) {
this.updateChart(newProps);
}
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 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.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 default ChartistGraph;