1
votes

I have a bar chart that has a label over each bar.

The problem is that when I hover on the chart, all of the labels disappear.

Before hovering:

enter image description here

After hovering:

enter image description here

and here's my code:

let barChartData = {
    labels:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
    datasets: [{
        label: 'Sessions',
        backgroundColor: 'rgba(0,163,224, 0.7)',
        borderColor: 'rgba(0,163,224, 1)',
        borderWidth: 1,
        data:[81, 69, 60, 51, 37, 35, 45, 65, 86, 58, 64, 39, 48, 29, 69, 80, 52, 61, 56, 40, 51, 31, 70, 51, 32, 51, 27, 30, 44, 59, 46]
    }]
};
let ctx = document.getElementById(id).getContext("2d");
let chart = new Chart(ctx, {
    type: 'bar',
    data: barChartData,
    options: {
        responsive: true,
        "animation": {
            "duration": 1,
            "onComplete": function () {
                let chartInstance = this.chart, ctx = chartInstance.ctx;
                ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, Chart.defaults.global.defaultFontStyle, Chart.defaults.global.defaultFontFamily);
                ctx.textAlign = 'center';
                ctx.textBaseline = 'bottom';
                ctx.fontColor = 'black'
                this.data.datasets.forEach(function (dataset, i) {
                    let meta = chartInstance.controller.getDatasetMeta(i);
                    meta.data.forEach(function (bar, index) {
                        let data = dataset.data[index];
                        ctx.fillText(data, bar._model.x, bar._model.y - 5);
                    });
                });
            }
        },
        plugins: {
            datalabels: {
                color: '#000000'
            }
        },
        legend: {
            display: false
        },
        tooltips: {
            enabled: true,
            mode: 'label'
        },
    }
});
I removed the animation field but didn't work.

the chartjs version I'm using is 2.7.3

1
It could be a bug in Chartjs 2.7.3. The problem doesn't occur in Chartjs 2.9.3.Thomas Sablik
@ThomasSablik got it. I edited the question. thank you.Nasrin
@ThomasSablik I changed to Chartjs 2.9.3, but I have the same problem.Nasrin

1 Answers

3
votes

You can fix the problem with an update to Chart.js 2.9.3 and passing an empty hover function:

<html>
<head>

</head>
<body>
<canvas id="container"></canvas>
<script src="https://pagecdn.io/lib/chart/2.9.3/Chart.bundle.min.js"></script>
</body>
</html>
let barChartData = {
    labels:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
    datasets: [{
        label: 'Sessions',
        backgroundColor: 'rgba(0,163,224, 0.7)',
        borderColor: 'rgba(0,163,224, 1)',
        borderWidth: 1,
        data:[81, 69, 60, 51, 37, 35, 45, 65, 86, 58, 64, 39, 48, 29, 69, 80, 52, 61, 56, 40, 51, 31, 70, 51, 32, 51, 27, 30, 44, 59, 46]
    }]
};
const id = 'container';
let ctx = document.getElementById(id).getContext("2d");
let chart = new Chart(ctx, {
    type: 'bar',
    data: barChartData,
    options: {
        responsive: true,
        "animation": {
            "duration": 1,
            "onComplete": function () {
                let chartInstance = this.chart, ctx = chartInstance.ctx;
                ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, Chart.defaults.global.defaultFontStyle, Chart.defaults.global.defaultFontFamily);
                ctx.textAlign = 'center';
                ctx.textBaseline = 'bottom';
                ctx.fontColor = 'black'
                this.data.datasets.forEach(function (dataset, i) {
                    let meta = chartInstance.controller.getDatasetMeta(i);
                    meta.data.forEach(function (bar, index) {
                        let data = dataset.data[index];
                        ctx.fillText(data, bar._model.x, bar._model.y - 5);
                    });
                });
            }
        },
        hover: () => {},
        plugins: {
            datalabels: {
                color: '#000000'
            }
        },
        legend: {
            display: false
        },
        tooltips: {
            enabled: true,
            mode: 'label'
        },
    }
});

This fix doesn't work with Chart.js 2.7.3. I don't know if it's a bug or a different behavior on purpose.

Live example