chartjs / Chart.js

Simple HTML5 Charts using the <canvas> tag
https://www.chartjs.org/
MIT License
64.52k stars 11.9k forks source link

Tooltip on mobile should go away when I tap outside of canvas #7738

Open lessless opened 4 years ago

lessless commented 4 years ago

Expected Behavior

When I tap outside of a canvas tooltip should disappear, akin to what happens when I tap inside a canvas. Or similarly to what happens on the desktop when I move mouse away from a circle.

Current Behavior

Tooltip stays visible when I tap outside of a canvas

Steps to Reproduce

Navigate to any tooltip example on mobile phone and tap on any data point. You will see a tooltip. Then tap outside of canvas and you will see that tooltip stays on a screen.

Context

It's expected that tooltips and hints are transient elements and they go away as soon as user leaves the context when they appeared.

Environment

kurkle commented 4 years ago

Does that happen in master too? (https://www.chartjs.org/samples/master/tooltips/custom-line.html) Works fine with android/chrome.

lessless commented 4 years ago

@kurkle works as expected in Chrome. Even latest.

ermish commented 3 years ago

I was able to reproduce this on iOS Safari. This works on safari desktop, and other browsers.

I'll work on making a PR for this 👍

kurkle commented 3 years ago

@Ermish any progress?

etimberg commented 2 years ago

Is this still an issue with Safari?

kurkle commented 2 years ago

I could actually reproduce this with FF 93 in emulation mode:

iphone-click-outside

kurkle commented 2 years ago

I could not find a event that FF on mobile emulation would fire on the canvas when tapping outside it. So as a workaround, one would need something like this:

if (event.target.tagName !== 'CANVAS') {
  const out = new Event('mouseout');
  for (const id of Object.keys(Chart.instances)) {
    Chart.instances[id].canvas.dispatchEvent(out);
  }
}

Note: FF on Android mobile does not need a workaround. So I'm not quite sure which devices actually need it.

pacastro commented 1 year ago

I'm having this issue in an iPhone 12 in a line chart when using options.interaction with intersect: 'false' and mode: 'index', can reproduce with Chrome in emulation mode for any kind of mobile device (no problem in desktop mode). Tooltip won't go away, only way is refreshing the page.

With intersect 'true' or using any other mode works ok, tooltip goes away when clicking outside of canvas. Only with 'index' in combination with intersect: 'false' tooltip won't go away.

arjen-n commented 1 year ago

I had this issue on iPhone 14 (Chrome and Safari) with my angular application.

chartjs version: 4.2.1 Angular version: 15.2.0

As far as i can see the only time when this behavior occurs is when you set the options.interaction.intersect on false and the options.interaction.mode on 'index' (like @pacastro mentioned).

I created a workaround for my angular application by using the example @kurkle provided. For me, the touchend gesture served as the best event to hide the tooltip.

Angular workaround:

@HostListener('document:touchend', ['$event.target'])
hideTooltip(target: HTMLElement): void {
  if (target.tagName.toLowerCase() !== "canvas") {
    this._chart.canvas.dispatchEvent(new Event('mouseout'));
  }
}

Plain JavaScript workaround:

document.addEventListener('touchend', function (event) {
  if (event.target && event.target.tagName.toLowerCase() !== "canvas") {
    CHART_INSTANCE.canvas.dispatchEvent(new Event('mouseout'));
  }
});