Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tooltipTemplate equivalent in Chart.js 2.0 ? #2322

Closed
nivv opened this issue Apr 22, 2016 · 5 comments
Closed

tooltipTemplate equivalent in Chart.js 2.0 ? #2322

nivv opened this issue Apr 22, 2016 · 5 comments

Comments

@nivv
Copy link

nivv commented Apr 22, 2016

Can't find anything on this. What I want to do is to show percentages in the pie chart. Below worked in Chart.js 1.0

    this.chart = new Chart(ctx, {
        type: 'pie',
        animateRotate: true,
        responsive: true,
        animationEasing: "easeOut",
        animationSteps: 40,
        tooltipTemplate: " <%=label%>: <%= value %> - <%= numeral(circumference / 6.283).format('(0[.][00]%)') %>",
        data: topBrowsers
    });

skarmklipp 2016-04-22 12 24 16

@etimberg
Copy link
Member

@nivv in v2 there are a number of callback functions for creating the tooltip.

In your case, you want the label tooltip to be overridden.

In your config object:

{
  tooltip: {
    callbacks: {
      // tooltipItem is an object containing some information about the item that this label is for (item that will show in tooltip). 
      // data : the chart data item containing all of the datasets
      label: function(tooltipItem, data) {
        // Return string from this function. You know the datasetIndex and the data index from the tooltip item. You could compute the percentage here and attach it to the string.
      }
    }
  }
}

The default callback for doughnut charts is: https://github.com/nnnick/Chart.js/blob/master/src/controllers/controller.doughnut.js#L95-L97

@nivv
Copy link
Author

nivv commented Apr 22, 2016

@etimberg thanks for the swift response! I'll try that out!

Edit:

Noticed that it should be tooltips instead of tooltip

{
  tooltips: {
    callbacks: {
      // tooltipItem is an object containing some information about the item that this label is for (item that will show in tooltip). 
      // data : the chart data item containing all of the datasets
      label: function(tooltipItem, data) {
        // Return string from this function. You know the datasetIndex and the data index from the tooltip item. You could compute the percentage here and attach it to the string.
      }
    }
  }
}

Complete example

  this.chart = new Chart(ctx, {
      type: 'pie',
      options: {
          tooltips: {
              callbacks: {
                  label: function(tooltipItem, data) {
                      var value = data.datasets[0].data[tooltipItem.index];
                      var label = data.labels[tooltipItem.index];
                      var percentage = Math.round(value / totalSessions * 100);
                      return label + ' ' + percentage + '%';
                  }
              }
          },
      },
      data: topBrowsers
  });

@nivv nivv closed this as completed Apr 22, 2016
@Minifish57
Copy link

Hi,
I have a question about this tooltiptemplate
Before, in version 1.0, i've used to set multiTooltipTemplate though, does this functionnality still exist ?
Because, when I use this solution, I haven't all the values in the tooltip. Anybody has a Clue ?

@manjunathk19
Copy link

manjunathk19 commented Dec 7, 2016

I want to display icons instead of just text so I have to write custom HTML and it is not working

@michilehr
Copy link

Hi,
I have a question about this tooltiptemplate
Before, in version 1.0, i've used to set multiTooltipTemplate though, does this functionnality still exist ?
Because, when I use this solution, I haven't all the values in the tooltip. Anybody has a Clue ?

tooltips: { mode: 'label' }

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

No branches or pull requests

6 participants