[Chartjs]-Chart.js 2.0: How to change title of tooltip

30๐Ÿ‘

โœ…

I encountered a similar problem too, but was resolved with this.

return 'Date: ' + tooltipItems[0].xLabel + ' GMT+2';

2๐Ÿ‘

I needed to manipulate title tooltip, and solved it. Reviewing the source chart.js for v 3.7.1, find callback for title, line 11759:

callbacks: {
    title(tooltipItems) {
    if(tooltipItems.length > 0) {
        const item = tooltipItems[0];
        const labels = item.chart.data.labels;
        const labelCount = labels ? labels.length : 0;
        if (this && this.options && this.options.mode === 'dataset') {
            return item.dataset.label || '';
        } else if (item.label) {
            return item.label;
        } else if (labelCount > 0 && item.dataIndex < labelCount) {
            return labels[item.dataIndex];
        }
    }
    return '';
    }
}

In my tooltip case, work with "item.label" return. Virtually copy and paste the code and works fine, in my case needed some text cut, over the return title.

Sorry by the english, helped by Google Translate.

Leave a comment