Skip to content

Struggling to use annotations in combination with tooltips #871

@goenning

Description

@goenning

I basically want the annotation to follow the tooltip so it highlights the current category where the tooltip is shown.

I have something that is partially working, but it's causing hundreds of updates (you can see the bar flashing on this video)

chartjs.mp4

This is what I have:

{
  type: "line",
  xMin: 3,
  xMax: 3,
  borderColor: "rgba(0, 0, 0, 0.05)",
  borderWidth: (ctx) => {
    console.log(ctx);
    const columns = ctx.chart.data.datasets[0].data.length;
    const chartWidth = ctx.chart.width + 100;
    return chartWidth / columns;
  },
},

And then, on the tooltip I use the external option to render a HTML tooltip, as well as do something like this:

const annotations = chartInstance.options.plugins?.annotation?.annotations;
if (annotations && annotations.length > 0) {
  annotations[0].xMin = tooltip.dataPoints[0].dataIndex;
  annotations[0].xMax = tooltip.dataPoints[0].dataIndex;
  clearTimeout(timer);
  timer = window.setTimeout(() => chart.update("none"), 200);
}

Which it kind of works, but it's not optimal. Also open to other ways of doing this if annotation is not the right plugin for this.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions