Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Custom Tooltips Content (tooltip.body)

Open zahlprish opened this issue 10 years ago • 8 comments

Chart Type: PIE / DOUGHNUT Feature: Tooltips

Hello!

This is mostly just a question. I am attempting to override the tooltip.body value for each pie sector to my own value. I am attempting to change the tool tip to a custom value:

See my attempt below

// Instanciate the doughnut graph.
var myDoughnutChart = new Chart(document.getElementById("market-activation-percentage"), {
    type: 'doughnut',
    data: {
        labels: marketActivationLbls,
        datasets: [{
            data: Object.keys(realData.market_activation_percentage.data.datasets).map(function(key) {
                return realData.market_activation_percentage.data.datasets[key].data[0]
            }),
            backgroundColor: Object.keys(realData.market_activation_percentage.data.datasets).map(function(key) {
                return realData.market_activation_percentage.data.datasets[key].backgroundColor
            }),
            hoverBackgroundColor: Object.keys(realData.market_activation_percentage.data.datasets).map(function(key) {
                return realData.market_activation_percentage.data.datasets[key].hoverBackgroundColor
            })
        }]
    },
    options: {
        legend: {
            display: false,
        },
        tooltips: {
            custom: function(tooltip) {
                // tooltip will be false if tooltip is not visible or should be hidden
                if (!tooltip) {
                    return;
                }

                tooltip.body = ['TEST']
            }
        }
    }
});

zahlprish avatar Apr 26 '16 22:04 zahlprish

I pretty printed this for you (I don't know enough about chart.js to provide a solution).

You can use 3 grave accents instead of just 1 to show code for multiple lines in a row, like this:

foo();

and it will show:

foo();

Anyway, here's your code cleaned up:

// Instanciate the doughnut graph.
var myDoughnutChart = new Chart(document.getElementById("market-activation-percentage"), {
    type: 'doughnut',
    data: {
        labels: marketActivationLbls,
        datasets: [{
            data: Object.keys(realData.market_activation_percentage.data.datasets).map(function(key) {
                return realData.market_activation_percentage.data.datasets[key].data[0]
            }),
            backgroundColor: Object.keys(realData.market_activation_percentage.data.datasets).map(function(key) {
                return realData.market_activation_percentage.data.datasets[key].backgroundColor
            }),
            hoverBackgroundColor: Object.keys(realData.market_activation_percentage.data.datasets).map(function(key) {
                return realData.market_activation_percentage.data.datasets[key].hoverBackgroundColor
            })
        }]
    },
    options: {
        legend: {
            display: false,
        },
        tooltips: {
            custom: function(tooltip) {
                // tooltip will be false if tooltip is not visible or should be hidden
                if (!tooltip) {
                    return;
                }

                tooltip.body = ['TEST']
            }
        }
    }
});

tervay avatar Apr 27 '16 01:04 tervay

I believe I found the issue:

On the file Chart.bundle.js the tooltip.update() is called every time the mouse moves, and since the update doesn't receive a changed variable, the changed flag is set as undefined, which makes the 'custom' function not being called and therefore the body of the tooltip not being updated. A solution would be to pass changed=true on the eventHandler:

eventHandler: function eventHandler(e) {
....
this.tooltip.update(true);

I was able to successfully override the tooltip body with this change.

Should I create a PR for this?

brizido avatar Apr 28 '16 14:04 brizido

@brizido if you could create a PR that'd be great. It's easier to have a look that way

etimberg avatar Apr 28 '16 14:04 etimberg

@etimberg Actually the change is on the file core.controlle.js.

Can you please add me as an active contributor so I can create a separate branch for the PR?

brizido avatar Apr 28 '16 14:04 brizido

@etimberg Pull request here: https://github.com/chartjs/Chart.js/pull/2416

brizido avatar Apr 29 '16 11:04 brizido

Changing this.tooltip.update(); to this.tooltip.update(true); appears to make the custom tool tips work as I would expect! Nice job @brizido

zprisk avatar May 04 '16 19:05 zprisk

Fixed with PR #2416, fix should be included in next release.

panzarino avatar May 04 '16 23:05 panzarino

How to add an unit for data in tooltips, like "1" , "1 kg"

henrrywelson avatar Dec 27 '19 03:12 henrrywelson