Rickshaw.js
1.84 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import React from 'react';
import Rickshaw from 'rickshaw';
import PropTypes from 'prop-types';
class RickshawGraph extends React.Component {
static propTypes = {
height: PropTypes.number,
};
static defaultProps = {
height: 100,
};
constructor(props) {
super(props);
this.state = {
graph: null,
};
this.initRickshaw = this.initRickshaw.bind(this);
this.onResizeRickshaw = this.onResizeRickshaw.bind(this);
}
componentDidMount() {
this.initRickshaw();
window.addEventListener('resize', this.onResizeRickshaw);
}
componentWillUnmount() {
window.removeEventListener('resize', this.onResizeRickshaw);
}
onResizeRickshaw() {
this.state.graph.configure({ height: this.props.height });
this.state.graph.render();
}
initRickshaw() {
const seriesData = [[], []];
const random = new Rickshaw.Fixtures.RandomData(30);
for (let i = 0; i < 30; i += 1) {
random.addData(seriesData);
}
// eslint-disable-next-line
this.state.graph = new Rickshaw.Graph({
element: this.rickshawChart,
height: this.props.height,
series: [
{
color: '#ffdddd',
data: seriesData[0],
name: 'Uploads',
}, {
color: '#f55d5d',
data: seriesData[1],
name: 'Downloads',
},
],
});
const hoverDetail = new Rickshaw.Graph.HoverDetail({
graph: this.state.graph,
xFormatter: x => new Date(x * 1000).toString(),
});
hoverDetail.show();
setInterval(() => {
random.removeData(seriesData);
random.addData(seriesData);
this.state.graph.update();
}, 1000);
this.state.graph.render();
}
render() {
return (
<div
ref={(r) => {
this.rickshawChart = r;
}}
/>
);
}
}
export default RickshawGraph;