Skip to content
Snippets Groups Projects
BarCharts.js 1.23 KiB
Newer Older
import {
    Chart as ChartJS,
    CategoryScale,
    LinearScale,
    BarElement,
    Title,
    Tooltip,
    Legend,
} from 'chart.js';
import { Component } from 'react';
import { Bar } from 'react-chartjs-2';

ChartJS.register(
    CategoryScale,
    LinearScale,
    BarElement,
    Title,
    Tooltip,
    Legend
);

const default_options = {
    indexAxis: 'y',
    elements: {
        bar: {borderWidth: 1,},

    },
    responsive: true,
    plugins: {
        legend: {display: false},
    },
    title: {
        display: false,
        text: 'TITLE',
    },
};


class BarChart extends Component {
    constructor(props) {
        super(props);
/*
TODO
* Sort data with labels
* Write extractLabels, extractData and extractBGColor
* See if these functions can or should be merged and how to set state if it is.
*/
        this.state = {
            options: this.props.options || default_options,
            data: {
                labels: extractLabels(this.props.data),
                datasets: [{
                    data: extractData(this.props.data),
                    borderColor: 'black',
                    backgroundColor: extractBGColor(this.props.data)
                }]
            },
        };
    }

}




export default BarChart;