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

tooltip on legend item #4023

Closed
d1manson opened this issue Mar 15, 2017 · 4 comments
Closed

tooltip on legend item #4023

d1manson opened this issue Mar 15, 2017 · 4 comments

Comments

@d1manson
Copy link

d1manson commented Mar 15, 2017

I want to provide a sentence or two of in-depth explanation when the user hovers over a dataset's entry in the legend.

I understand that the legend (as with the rest of the chart) is rendered on a canvas rather than with the DOM, so I have to hook into the chartjs internals. After much googling, I have got as far as:

  Chart.plugins.register({
    afterEvent: function(chart, e) {
      if(!e.type=="mousemove"){
        return;
      } 
      chart.legend.legendHitBoxes.forEach((box,ii) => {
        if(!(e.x > box.left && e.x < box.left + box.width &&
             e.y > box.top && e.y < box.top + box.height)){
          return
        }
        console.log(chart.data.datasets[ii].label); 
       // imagine I have a .tooltipText property for the dataset or something
      });
  });

which logs the dataset label when I hover over it in the legend. Next I need to show a tooltip.

Am I going about this in the right way so far? How do I show a tooltip?

@etimberg
Copy link
Member

@d1manson in general you're going about it the right way. You might be able to use the legend onHover property to avoid needed to have all kinds of internal look-ups in the legend.

To show the tooltip, I think you might get away with creating a new Chart.Tooltip object but that might be overkill for your needs. In your plugin you could always have some draw code that would draw a tooltip like box and then in the event handler you would set some options on the chart and call update() to make sure that it rendered afterwards

@d1manson
Copy link
Author

OK, I see the onHover in legend options, and it does seem to work...

new Chart(canvas, {
  type: 'line',
  data: ...,
  options: {
    legend: {
      onHover: function(e, target){
          var idx = target.datasetIndex;
          console.log("legend " + idx);
  }}}});

however this seems to fire for mouse-move, when what I want is mouse-enter and mouse-leave. I see I can also do something with the general hover options

 options: {
   hover: {
     onHover: function(e, targets){
    }}};

but that doesn't seem be particularly relevant to the legends...?

In terms of tooltips, could you give me a pointer to some code or docs.

Thanks for the quick reply! :)

@etimberg
Copy link
Member

etimberg commented Apr 1, 2017

Sorry for the delayed reply on this. I think you could make a modification to the legend plugin to trigger onHover on mouseenter or mouseleave instead of mousemove. I think all you have to do is change https://github.com/chartjs/Chart.js/blob/master/src/plugins/plugin.legend.js#L451-L454

I think you can even patch it by overwriting Chart.Legend.prototype.handleEvent but it is a private function that can change at any time.

@etimberg
Copy link
Member

etimberg commented May 2, 2017

Closing as out of date

@etimberg etimberg closed this as completed May 2, 2017
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

2 participants