amcharts / amcharts5

The newest, fastest, and most advanced amCharts charting library for JavaScript and TypeScript apps.
Other
353 stars 95 forks source link

KeepTargetHover Issue #1735

Closed jdsmith39 closed 1 month ago

jdsmith39 commented 1 month ago

When KeepTargetHover is true, the tooltip isn't staying when hovering across X axis point values. Hopefully this illustrates what I'm doing. In the first screenshot I'm hovering over the "USA" tooltip as I move the mouse cursor to the right. Instead of keeping the USA tooltip open, the China tooltip shows, which you can see from the 2nd screenshot. Screenshot 2024-09-19 075448 Screenshot 2024-09-19 075458 The below code pen demonstrates this behavior. Code Pen

Let me know if you need anything else.

martynasma commented 1 month ago

This is because XYCursor is snapping to whichever column is closer.

To fix that, you need to remove tooltip from series, and add tooltipHTML directly to column templates:

var series = chart.series.push(am5xy.ColumnSeries.new(root, {
  xAxis: xAxis,
  yAxis: yAxis,
  valueYField: "value",
  sequencedInterpolation: true,
  categoryXField: "country"
}));

series.getTooltip().setAll({
  keepTargetHover: true
})

series.columns.template.setAll({
  tooltipHTML: "<strong>{categoryX} this is a lot of text to make this very long</strong>: {valueY}"
})
jdsmith39 commented 1 month ago

Thanks for the quick response! I updated the Code Pen with your suggested changes (I think) and it still doesn't keep the tooltip open while hovering on it. Also, my use case is using a LineSeries, does that change how you handle this?

martynasma commented 1 month ago

You have a typo in keepTargetHover.

For LineSeries, there are no hoverable elements, so you will need to add bullets to hover on.

jdsmith39 commented 1 month ago

Thanks! Worked great for the columns.

I changed the code pen to use a LineSeries instead. Added the bullets. The tooltip shows up when hovering over the bullet, but the keepTargetHover: true isn't affecting it. What do I need to do differently here?

Code Pen

martynasma commented 1 month ago

Same thing as here: https://github.com/amcharts/amcharts5/issues/1735#issuecomment-2360888773

jdsmith39 commented 1 month ago

LineSeries doesn't seem to have columns.
Throws this error: "Uncaught TypeError: Cannot read properties of undefined (reading 'template')" I updated the Code Pen to show the error. I was going through the documentation (probably missed what I needed to see) and didn't see anything else that I could put the tooltip on instead.

martynasma commented 1 month ago

Of course they don't. That's why you have to set it on bullets.

var series = chart.series.push(am5xy.LineSeries.new(root, {
  xAxis: xAxis,
  yAxis: yAxis,
  valueYField: "value",
  sequencedInterpolation: true,
  categoryXField: "country"
}));

series.getTooltip().setAll({
  keepTargetHover: true
});

series.bullets.push(function (root) {
  return am5.Bullet.new(root, {
    sprite: am5.Circle.new(root, {
      radius: 7,
      fill: series.get("fill"),
      tooltipHTML: "<strong>{categoryX} this is a lot of text to make this very long</strong>: {valueY}"
    })
  });
});
jdsmith39 commented 1 month ago

Thank you very much. That is exactly what I needed.