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

Change line colour on hover #3880

Closed
aseverino opened this issue Feb 8, 2017 · 11 comments
Closed

Change line colour on hover #3880

aseverino opened this issue Feb 8, 2017 · 11 comments

Comments

@aseverino
Copy link

I believe it would be nice to see a good interaction support with Stacked Line charts, where one could get a Tooltip and a background color change by hovering over the filling of a line.

image

@etimberg
Copy link
Member

etimberg commented Feb 8, 2017

@kilouco have you looked at the tooltip modes sample? https://github.com/chartjs/Chart.js/blob/master/samples/tooltips/interaction-modes.html

@aseverino
Copy link
Author

@etimberg yes I have.

None of the examples give me any clue on how to achieve a background color change based on y position and values tooltip based on x position.

Am I missing something?

@etimberg
Copy link
Member

etimberg commented Feb 8, 2017

oh, sorry I misunderstood what you're looking for. At the moment, there is no way to change the style of the line (fill/stroke) on hover.

Possible duplicate of #2136

@aseverino
Copy link
Author

I don't think it's a duplicate. He is asking for a change on line color on hover. I'm asking for a fill color change. The idea is pretty much the same, though.

@juanjalvarez
Copy link

Hey, currently on 2.7.3 and wondering if this feature is still missing from Chart.js. With @types/chart.js I can see that the dataset takes a backgroundColor and hoveredBackgroundColor but does not apply it to the "stacked" lines.

@djsharkk
Copy link

I'm stuck on this also, I need to be able to highlight the dataset with the line included.

@etimberg etimberg changed the title Hover behavior with Stacked Line Chart Change line colour on hover Oct 15, 2020
@etimberg
Copy link
Member

@kurkle looks like the dataset mode hover stopped working. Do you recall what happened to it?

@kurkle
Copy link
Member

kurkle commented Oct 16, 2020

@kurkle looks like the dataset mode hover stopped working. Do you recall what happened to it?

@etimberg looks like its working?
https://codepen.io/kurkle/pen/ZEOWbGv

@etimberg
Copy link
Member

So it is! I must have setup my test case wrong when I tried it locally.

@kurkle
Copy link
Member

kurkle commented Nov 14, 2020

So, this is actually quite trivial to achieve using Chart.js v3 and a custom interaction mode:

Chart.Interaction.modes.under = function(chart, e, options, useFinalPosition) {
  const metas = myChart.getSortedVisibleDatasetMetas();
  for(meta of metas) { // loop the metasets from top to bottom
    if (meta.dataset.interpolate(e, 'x').y < e.y) { // the line is above mouse position
      return [{datasetIndex: meta.index, element: meta.dataset}]; // could return all the points here too
    };
  }
  return [];
};

https://codepen.io/kurkle/pen/vYKbzKo

@etimberg
Copy link
Member

Closing since the above solution works well

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