Chart.js: Chart.defaults.global.tooltips.callbacks.label only works for bar, line

Created on 21 Oct 2020  路  5Comments  路  Source: chartjs/Chart.js

Expected Behavior


Global setting for tooltips for all chart types
I would like to use the global settings to set the display of the tooltips without having to do this via the "options".

Current Behavior


Chart.defaults.global.tooltips.callbacks.label only works for bar, line

Context

Chart.defaults.global.tooltips.callbacks.label = function ( 
            tooltipItem,
            data
        ) {
            let dataset = data.datasets[tooltipItem.datasetIndex];
            let datasetLabel = dataset.label || ""
            let suffix = dataset.unit || ""
            console.log('Tooltips.label',tooltipItem, data )
            return (
                " " + datasetLabel + ": " + dataset.data[tooltipItem.index].toLocaleString()  + " " + suffix
            );

Environment

  • Chart.js version: "chart.js": "^2.9.4"
  • Browser name and version: Chrome Version 86.0.4240.80 (Offizieller Build) (x86_64)

Am I doing something wrong ?

bug 2

All 5 comments

@zibous which chart types did you try? More specialized types such as the doughnut chart provide an overridden default since the labels change in a different way. See https://github.com/chartjs/Chart.js/blob/2.9/src/controllers/controller.doughnut.js#L98-L117

@etimberg
Thanks for your replay. I try:

pie, polarArea, doughnut -- do not work
bar, line -- ok, works

It may be that I cannot use Chart.defaults.global.tooltips for every chart type ?
What solution is there for this?

You should be able to override the defaults for the other chart types with something like the following. You would need to override it for the 'bubble', 'doughnut', 'pie', 'polarArea', and 'scatter' chart types.

Chart.defaults.doughnut.tooltips.callbacks.label = overrideFunction;

You can also remove the provided default(s):

delete Chart.defaults.pie.tooltips.callbacks.label;

@etimberg

Chart.defaults.doughnut.tooltips.callbacks.label = overrideFunction;

Thanks, now it is working, perfect 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SylarRuby picture SylarRuby  路  3Comments

gabrieldesouza picture gabrieldesouza  路  3Comments

JewelsJLF picture JewelsJLF  路  3Comments

bytesnz picture bytesnz  路  3Comments

benmccann picture benmccann  路  3Comments