Skip to content

Create custom tooltip from element instead of HTML string #4869

@Radiergummi

Description

@Radiergummi

Summary

Allow to provide an element instance from the custom tooltip callback in addition to just an HTML string. This would make it a lot easier to integrate with other libraries and frontend stacks.

API Changes

Really just this:

-   tooltipEl.innerHTML = fn({
+   const customTooltip = fn({
      ctx: this.ctx,
      series: w.globals.series,
      seriesIndex: i,
      dataPointIndex: j,
      y1,
      y2,
      w,
    })
+
+   if (typeof customTooltip === 'string') {
+     tooltipEl.innerHTML = customTooltip
+   } else {
+     tooltipEl.appendChild(customTooltip)
+   }

src/modules/tooltip/Labels.js, Lines 527–535

Intended Use Case

This allows the custom tooltip callback to not only return an HTML string, but also an HTML element instance, which solves a slew of issues and user questions, and allows more advanced integration, such as using a prop in React, or a slot in a Vue component, to render a tooltip.

The API change doesn't introduce any backwards incompatibilities.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions