Chart.js #6342

by Akihiko Kusanagi

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels"></script>
<canvas id="chart1" width=600 height=300></canvas>

JavaScript

new Chart(document.getElementById('chart1'), {
    type: 'bar',
    data: {
        labels: [''],
        datasets: [{
        	data: [0, 0],
          backgroundColor: '#fff'
        }, {
        	data: [25, 25],
          backgroundColor: '#fdd'
        }, {
        	data: [20.83, 20.83],
          backgroundColor: '#fbb'
        }, {
        	data: [25, 25],
          backgroundColor: '#f99'
        }, {
        	data: [12.5, 12.5],
          backgroundColor: '#f77'
        }, {
        	data: [16.67, 16.67],
          backgroundColor: '#f55'
        }, {
        	data: [0, 0],
          backgroundColor: '#f33'
        }]
    },
    options: {
        legend: {
            display: false
        },
        scales: {
            yAxes: [{
                stacked: true,
                ticks: {
                    beginAtZero: true,
                    max: 100,
                    callback: function(value, index, values) {
                        return value + '%';
                    }
                }
            }],
            xAxes: [{
                stacked: true,
                ticks: {
                    beginAtZero: true,
                    display: false,
                }
            }]

        },
        plugins: {
            datalabels: {
                formatter: (value, context) => {
                    return value + '% ' + '= 1';
                }
            }
        }
    }
});