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

Not clear how to override the default tooltip #6216

Closed
1 of 3 tasks
gkpo opened this issue Apr 18, 2019 · 1 comment · Fixed by #6918
Closed
1 of 3 tasks

Not clear how to override the default tooltip #6216

gkpo opened this issue Apr 18, 2019 · 1 comment · Fixed by #6918

Comments

@gkpo
Copy link

gkpo commented Apr 18, 2019

Documentation Is:

  • Missing or needed
  • Confusing
  • Not Sure?

The problem

When you want to alter the default tooltip for a chart, it is not clear how to do it:
https://www.chartjs.org/docs/latest/configuration/tooltip.html#label-callback

I had to dig in the source code of chart.js to see what the default callback looked like.

My proposal for Changes

Replace the example in the documentation with an example that actually works:

Example

var chart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: {
        tooltips: {
            callbacks: {
                label: function(tooltipItem, data) {
                  var dataLabel = data.labels[tooltipItem.index];
                  var value = ': ' + data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];

                  if (helpers$1.isArray(dataLabel)) {
                    // show value on first line of multiline label
                    // need to clone because we are changing the value
                    dataLabel = dataLabel.slice();
                    dataLabel[0] += value;
                  } else {
                    dataLabel += value;
                  }

                  return dataLabel;
                }
            }
        }
    }
});
@rugk
Copy link

rugk commented Jun 11, 2019

Thanks, this is indeed strange. The actual up-to-date code can be found here:

label: function(tooltipItem, data) {

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

Successfully merging a pull request may close this issue.

2 participants