|
| 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 | +}); |
0 commit comments