Skip to content

Commit d196167

Browse files
jerairrestgor181
authored andcommitted
Added a random line example (#67)
1 parent 2a96e6a commit d196167

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import React from 'react';
2+
import {Line} from 'react-chartjs-2';
3+
4+
const initialState = {
5+
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
6+
datasets: [
7+
{
8+
label: 'My First dataset',
9+
fill: false,
10+
lineTension: 0.1,
11+
backgroundColor: 'rgba(75,192,192,0.4)',
12+
borderColor: 'rgba(75,192,192,1)',
13+
borderCapStyle: 'butt',
14+
borderDash: [],
15+
borderDashOffset: 0.0,
16+
borderJoinStyle: 'miter',
17+
pointBorderColor: 'rgba(75,192,192,1)',
18+
pointBackgroundColor: '#fff',
19+
pointBorderWidth: 1,
20+
pointHoverRadius: 5,
21+
pointHoverBackgroundColor: 'rgba(75,192,192,1)',
22+
pointHoverBorderColor: 'rgba(220,220,220,1)',
23+
pointHoverBorderWidth: 2,
24+
pointRadius: 1,
25+
pointHitRadius: 10,
26+
data: [65, 59, 80, 81, 56, 55, 40]
27+
}
28+
]
29+
};
30+
31+
32+
33+
const Graph = React.createClass({
34+
displayName: 'Graph',
35+
componentWillMount(){
36+
this.setState(initialState);
37+
},
38+
componentDidMount(){
39+
40+
var _this = this;
41+
42+
setInterval(function(){
43+
var oldDataSet = _this.state;
44+
var newData = [];
45+
46+
for(var x=0; x< _this.state.labels.length; x++){
47+
newData.push(Math.floor(Math.random() * 100));
48+
}
49+
50+
var newDataSet = Object.assign({}, oldDataSet);
51+
newDataSet.data = newData;
52+
53+
_this.setState({datasets: [newDataSet]});
54+
}, 5000);
55+
},
56+
render() {
57+
return (
58+
<Line data={this.state} />
59+
);
60+
}
61+
});
62+
63+
64+
65+
66+
export default React.createClass({
67+
displayName: 'RandomizedDataLineExample',
68+
69+
render() {
70+
return (
71+
<div>
72+
<h2>Random Animated Line Example</h2>
73+
<Graph />
74+
</div>
75+
);
76+
}
77+
});

example/src/example.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import RadarExample from './components/radar';
1111
import PolarExample from './components/polar';
1212
import BubbleExample from './components/bubble';
1313
import MixedDataExample from './components/mix';
14+
import RandomizedDataLineExample from './components/randomizedLine';
1415

1516
class App extends React.Component {
1617
render() {
@@ -36,6 +37,8 @@ class App extends React.Component {
3637
<BubbleExample />
3738
<hr />
3839
<MixedDataExample />
40+
<hr />
41+
<RandomizedDataLineExample />
3942
</div>
4043
);
4144
}

0 commit comments

Comments
 (0)