Chartjs-How can I set y-axis label on Chart.js?

0👍

There is a workaround that you could fill the ctx with any value from the code shown below.

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
    $(function () {
    var chartBar = document.getElementById('barChart').getContext('2d');
    var data = {
            labels: ['', '', '', '', '', '', '', '', '', '', '', ''],
            datasets: [{
                label: 'Cantidad',
                data: [2, 4, 6, 7, 8, 4, 23, 13, 5, 20, 4],
                backgroundColor: 'rgba(209, 74, 88)',
            }]
        };

    var options = {
            maintainAspectRatio: false,
            scales: {
                yAxes: [{
                    ticks: {
                        min: 0,
                        beginAtZero: true,
                    },
                    gridLines: {
                        display: true,
                        color: "rgba(255,99,164,0.2)"
                    },
                    scaleLabel: {
                        display: true,
                        labelString:""
                    }
                }]

        },
        animation: {
            duration: 1,
            onComplete: function () {
                chartBar.fillStyle = "black";
                chartBar.fillText('Quantity', 50, 18);
            }
        }
        };

   var myChart = new  Chart(chartBar, {
            options: options,
            data: data,
            type:'line'

        });
    });
</script>

Reference:

https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillText

https://www.chartjs.org/docs/latest/configuration/animations.html

Leave a comment