import Chart from 'chart.js/auto'; import PropTypes from 'prop-types'; import React, { Component } from 'react'; import colors from 'Styles/Variables/colors'; class DoughnutChart extends Component { constructor(props) { super(props); this.canvasRef = React.createRef(); } componentDidMount() { this.myChart = new Chart(this.canvasRef.current, { type: 'doughnut', options: { maintainAspectRatio: false, plugins: { title: { display: true, text: this.props.title }, legend: { position: 'bottom' } } }, data: { labels: this.props.data.map((d) => d.label), datasets: [{ label: this.props.title, data: this.props.data.map((d) => d.value), backgroundColor: colors.chartColors }] } }); } 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 ( ); } } DoughnutChart.propTypes = { data: PropTypes.arrayOf(PropTypes.object).isRequired, title: PropTypes.string.isRequired }; DoughnutChart.defaultProps = { data: [], title: '' }; export default DoughnutChart;