Skip to content

Custom Tooltips Content (tooltip.body) #2384

Closed

Description

@zahlprish

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']
            }
        }
    }
});

Activity

  1. tervay commented on Apr 27, 2016

    @tervay

    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']
                }
            }
        }
    });
    
  2. brizido commented on Apr 28, 2016

    @brizido

    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?

  3. etimberg commented on Apr 28, 2016

    @etimberg
    Member

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

  4. brizido commented on Apr 28, 2016

    @brizido

    @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?

  5. brizido commented on Apr 29, 2016

    @brizido

    @etimberg Pull request here: #2416

  6. zprisk commented on May 4, 2016

    @zprisk

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

  7. panzarino commented on May 4, 2016

    @panzarino
    Contributor

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

  8. henrrywelson commented on Dec 27, 2019

    @henrrywelson

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions