2016-11-09 15:58:10 +02:00
|
|
|
const buildArray = require('build-array');
|
|
|
|
const Chart = require('chart.js');
|
|
|
|
const React = require('react');
|
|
|
|
|
|
|
|
module.exports = React.createClass({
|
|
|
|
ref: function(name) {
|
|
|
|
this._refs = this._refs || {};
|
|
|
|
|
|
|
|
return (el) => {
|
|
|
|
this._refs[name] = el;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
fromData: function(data) {
|
|
|
|
return (data || []).map((d) => {
|
|
|
|
return d.cpu;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
componentDidMount: function() {
|
2016-11-09 17:00:30 +02:00
|
|
|
const {
|
|
|
|
data = [],
|
|
|
|
bg,
|
|
|
|
border
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const bars = this.fromData(data);
|
2016-11-09 15:58:10 +02:00
|
|
|
|
|
|
|
this._chart = new Chart(this._refs.component, {
|
|
|
|
type: 'bar',
|
2016-11-09 17:00:30 +02:00
|
|
|
options: {
|
|
|
|
scales: {
|
|
|
|
xAxes: [{
|
|
|
|
display: false
|
|
|
|
}],
|
|
|
|
yAxes: [{
|
|
|
|
display: false
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
legend: {
|
|
|
|
display: false
|
|
|
|
}
|
|
|
|
},
|
2016-11-09 15:58:10 +02:00
|
|
|
data: {
|
|
|
|
labels: buildArray(bars.length).map((v, i) => ''),
|
|
|
|
datasets: [{
|
2016-11-09 17:00:30 +02:00
|
|
|
borderWidth: 1,
|
|
|
|
borderColor: border,
|
|
|
|
backgroundColor: bg,
|
2016-11-09 15:58:10 +02:00
|
|
|
data: bars
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
componentWillReceiveProps: function(nextProps) {
|
2016-11-09 17:00:30 +02:00
|
|
|
const {
|
|
|
|
data = [],
|
|
|
|
bg,
|
|
|
|
border
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const bars = this.fromData(data);
|
2016-11-09 15:58:10 +02:00
|
|
|
|
|
|
|
this._chart.data.labels = buildArray(bars.length).map((v, i) => '');
|
2016-11-09 17:00:30 +02:00
|
|
|
this._chart.data.datasets[0].backgroundColor = bg;
|
|
|
|
this._chart.data.datasets[0].borderColor = border;
|
2016-11-09 15:58:10 +02:00
|
|
|
this._chart.data.datasets[0].data = bars;
|
|
|
|
|
|
|
|
this._chart.update(0);
|
|
|
|
},
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<canvas
|
|
|
|
ref={this.ref('component')}
|
|
|
|
width='400'
|
|
|
|
height='400'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|