2017-01-10 20:06:01 +02:00
|
|
|
const buildArray = require('build-array');
|
|
|
|
const Chart = require('chart.js');
|
2017-01-10 21:12:50 +02:00
|
|
|
const fns = require('../../shared/functions');
|
2017-01-10 20:06:01 +02:00
|
|
|
const React = require('react');
|
2017-01-10 21:12:50 +02:00
|
|
|
const Styled = require('styled-components');
|
2017-01-10 20:06:01 +02:00
|
|
|
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;
|
|
|
|
`;
|
|
|
|
|
2017-01-10 20:06:01 +02:00
|
|
|
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>
|
2017-01-10 20:06:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Graph.propTypes = {
|
|
|
|
data: React.PropTypes.array,
|
|
|
|
labels: React.PropTypes.number,
|
|
|
|
max: React.PropTypes.number,
|
|
|
|
min: React.PropTypes.number
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Graph;
|