1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-14 15:20:06 +02:00
copilot/legacy/spikes/graphs-fe/rickshaw/client/chart.js
Sérgio Ramos 8295bd6882 chore: initial lerna setup
this shall be a progressive process
2017-05-25 10:56:50 +01:00

78 lines
1.4 KiB
JavaScript

const Rickshaw = require('rickshaw');
const ReactRedux = require('react-redux');
const React = require('react');
const style = require('./style.css');
const {
connect
} = ReactRedux;
const {
Graph
} = Rickshaw;
let i = 0;
const Component = React.createClass({
ref: function(name) {
this._refs = this._refs || {};
return (el) => {
this._refs[name] = el;
};
},
fromData: function(data) {
return (data || []).map((d, i) => {
return {
y: d.cpu,
x: i
};
});
},
componentDidMount: function() {
this._chart = new Graph({
element: this._refs.component,
renderer: 'bar',
width: 500,
height: 200,
series: [{
data: this.fromData(this.props.data)
}]
});
this._chart.render();
},
componentWillReceiveProps: function(nextProps) {
this._chart.series[0].data = this.fromData(this.props.data);
this._chart.update();
},
render: function() {
const {
data = []
} = this.props;
const className = (data.length && data[data.length - 1].cpu > 50)
? style.red
: style.blue;
return (
<div
className={`${style.rickshaw_graph} ${className}`}
ref={this.ref('component')}
/>
);
}
});
const mapStateToProps = ({
data
}) => {
return {
data
};
};
module.exports = connect(
mapStateToProps
)(Component);