[Chartjs]-Chart.js – tooltip callback function ignore hidden datasets when deselecting legend

1πŸ‘

βœ…

I revisited this problem today and found a solution really quick.

Accessing the β€˜hidden’ property can be done like in this post: Tell if an line is hidden

This is the updated code:

callbacks: {
    label: function(tooltipItem, data) {
        let total = 0;
        let value = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
        let stop = tooltipItem.datasetIndex+1;
        for (let j = 0; j < stop; j++) {
            if(!data.datasets[j]._meta[Object.keys(data.datasets[j]._meta)[0]].hidden) {
                total += data.datasets[j].data[tooltipItem.index];
            }
        }
    total = Math.floor(total * 100) / 100;
    return 'Value: ' + value + ' Total: ' + total;
    }
}

Leave a comment