You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.1 KiB
51 lines
1.1 KiB
4 years ago
|
import Chart from 'chart.js';
|
||
|
import PropTypes from 'prop-types';
|
||
|
import React, { Component } from 'react';
|
||
|
|
||
|
class BarChart extends Component {
|
||
|
constructor(props) {
|
||
|
super(props);
|
||
|
this.canvasRef = React.createRef();
|
||
|
}
|
||
|
|
||
|
componentDidMount() {
|
||
|
this.myChart = new Chart(this.canvasRef.current, {
|
||
|
type: 'bar',
|
||
|
options: {
|
||
|
maintainAspectRatio: false
|
||
|
},
|
||
|
data: {
|
||
|
labels: this.props.data.map((d) => d.label),
|
||
|
datasets: [{
|
||
|
label: this.props.title,
|
||
|
data: this.props.data.map((d) => d.value)
|
||
|
}]
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
componentDidUpdate() {
|
||
|
this.myChart.data.labels = this.props.data.map((d) => d.label);
|
||
|
this.myChart.data.datasets[0].data = this.props.data.map((d) => d.value);
|
||
|
this.myChart.update();
|
||
|
}
|
||
|
|
||
|
render() {
|
||
|
return (
|
||
|
<canvas ref={this.canvasRef} />
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
BarChart.propTypes = {
|
||
|
data: PropTypes.arrayOf(PropTypes.object).isRequired,
|
||
|
title: PropTypes.string.isRequired
|
||
|
};
|
||
|
|
||
|
BarChart.defaultProps = {
|
||
|
data: [],
|
||
|
title: ''
|
||
|
};
|
||
|
|
||
|
export default BarChart;
|