amcharts / amcharts5

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

Empty HTML tooltips cause error "DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node." #1157

Closed lindapaiste closed 1 year ago

lindapaiste commented 1 year ago

Bug description image

I came across an issue where HTML tooltips were causing my entire app to crash. After a bunch of playing around, I figured out the circumstances which produce the DOMException. You might consider this to be "user error" but I thought it was worth reporting.

Demo: https://codepen.io/linda-paiste/pen/abXNYvz?editors=0010

Relevant code:

const tooltip = xAxis.getTooltip();

tooltip.adapters.add("labelHTML", function (text, target) {
  const lines = chart.series.values.map((series) => {
    const tooltipDataItem = series.get("tooltipDataItem");
    const value = tooltipDataItem?.get("valueY");
    if (!value) return "";
    const color = series.get("stroke").toString();
    const name = series.get("name");
    return `<div style="display: flex;justify-content:space-between;">
    <div style="width:200px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;color:white;">${name}:</div>
    <span style="color:${color};text-align:right">${value}</span>
    </div>`;
  });
  return lines
    .filter(Boolean) // Removing this line eliminates the bug
    .join("\n");
});

The issue here is that the adapter sometimes returns an empty string '' (if all series have 0 values). When that happens, the chart shows the default x-axis tooltip instead of the custom HTML content. And then it will crash on the next tooltip.

martynasma commented 1 year ago

Thanks!

This issue was fixed in dev version, and will be released soon.

We'll notify you here when that happens.

martynasma commented 1 year ago

Fixed in 5.5.3.

[5.5.3] - 2023-11-01

Added

Changed

Fixed

Full change log.

Download options.

Make sure you clear your browser cache after upgrading. And feel free to contact us again if you are still experiencing this issue.