Chartjs-ChartJS 4 migration: access other datasets from tooltip callback

1👍

As @kikon pointed out, data is accessible via context.chart.data. For some reason, that doesn’t show up for me when I console.dir() the context object so I was just completely overlooking it.

Anyway, for anyone this might help in the future, here’s the working version:

callbacks: {
    label: function(context) {
        const datasets = context.chart.data.datasets
        const countWith = datasets[0].data[context.dataIndex]
        const countWithout = datasets[1].data[context.dataIndex]
        const perc = (context.raw / (countWith + countWithout) * 100).toFixed(1)
        return `${context.dataset.label}: ${context.formattedValue} (${perc}%)`
    }
}

Leave a comment