1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-14 07:10:05 +02:00
copilot/ui/src/components/mini-metric/graph.js

116 lines
2.1 KiB
JavaScript
Raw Normal View History

const buildArray = require('build-array');
const Chart = require('chart.js');
2017-01-10 21:12:50 +02:00
const fns = require('../../shared/functions');
const React = require('react');
2017-01-10 21:12:50 +02:00
const Styled = require('styled-components');
const whisker = require('chartjs-chart-box-plot');
whisker(Chart);
2017-01-10 21:12:50 +02:00
const {
remcalc
} = fns;
const {
default: styled
} = Styled;
const Container = styled.div`
position: relative;
height: ${remcalc(72)};
width: 100%;
`;
const Canvas = styled.canvas`
position: absolute;
bottom: 0;
`;
class Graph extends React.Component {
componentDidMount() {
const {
data = [],
labels = 0,
max = 100,
min = 0
} = this.props;
const _labels = !Array.isArray(labels)
? buildArray(labels || data.length).map((v, i) => '')
: labels;
this._chart = new Chart(this._refs.component, {
type: 'whisker',
responsive: true,
maintainAspectRatio: true,
options: {
scales: {
xAxes: [{
display: false,
barPercentage: 1.0,
categoryPercentage: 1.0
}],
yAxes: [{
display: false,
ticks: {
min: min,
max: max
}
}]
},
legend: {
display: false
}
},
data: {
labels: _labels,
datasets: [{
data
}]
}
});
}
componentWillReceiveProps(nextProps) {
const {
data = [],
labels = 0
} = this.props;
const _labels = !Array.isArray(labels)
? buildArray(labels || data.length).map((v, i) => '')
: labels;
this._chart.data.datasets = [{
data
}];
this._chart.data.labels = _labels;
this._chart.update(0);
}
ref(name) {
this._refs = this._refs || {};
return (el) => {
this._refs[name] = el;
};
}
render() {
return (
2017-01-10 21:12:50 +02:00
<Container>
<Canvas
innerRef={this.ref('component')}
/>
</Container>
);
}
}
Graph.propTypes = {
data: React.PropTypes.array,
labels: React.PropTypes.number,
max: React.PropTypes.number,
min: React.PropTypes.number
};
module.exports = Graph;