Skip to content

Tooltip

Enabled by default: hovering shows the nearest node, the node itself is highlighted, and the other series are dimmed.

Modular build

When building with grafit-charts/core, the tooltip is a separate module: register(tooltipModule).

By default the tooltip shows the xField value as the heading and a "series name: value" pair. The content is customized via tooltip.renderer on the series:

ts
import { getData } from './data';
import type { ChartOptions } from 'grafit-charts';

const rub = new Intl.NumberFormat('ru-RU', { style: 'currency', currency: 'RUB', maximumFractionDigits: 0 });

export function createOptions(): ChartOptions {
  return {
    data: getData(),
    title: { text: 'Orders this week' },
    series: [
      {
        type: 'bar',
        xField: 'day',
        yField: 'orders',
        name: 'Orders',
        tooltip: {
          // renderer returns the tooltip structure; datum gives access to the whole data row
          renderer: ({ datum, yValue, color }) => ({
            heading: `${datum.day} — ${yValue} orders`,
            rows: [{ label: 'Revenue', value: rub.format(datum.revenue as number), color }],
          }),
        },
      },
    ],
    legend: { enabled: false },
  };
}
ts
export function getData() {
  return [
    { day: 'Mon', orders: 142, revenue: 388000 },
    { day: 'Tue', orders: 158, revenue: 412000 },
    { day: 'Wed', orders: 149, revenue: 395500 },
    { day: 'Thu', orders: 173, revenue: 460200 },
    { day: 'Fri', orders: 196, revenue: 540800 },
    { day: 'Sat', orders: 224, revenue: 618400 },
    { day: 'Sun', orders: 201, revenue: 552300 },
  ];
}

Modes

mode: 'single' (default) shows the value of the nearest node; mode: 'shared' shows the values of all visible series of the category in a single tooltip; nodes of all series are highlighted and no dimming is applied:

ts
import { getData } from './data';
import type { ChartOptions } from 'grafit-charts';

export function createOptions(): ChartOptions {
  return {
    data: getData(),
    title: { text: 'Shared tooltip' },
    subtitle: { text: "mode: 'shared' + range: 'nearest' — tooltip from anywhere" },
    series: [
      { type: 'line', xField: 'month', yField: 'plan', name: 'Plan' },
      { type: 'line', xField: 'month', yField: 'fact', name: 'Actual' },
      { type: 'line', xField: 'month', yField: 'forecast', name: 'Forecast', lineDash: [4, 3] },
    ],
    tooltip: { mode: 'shared', range: 'nearest' },
    crosshair: { enabled: true },
  };
}
ts
export function getData() {
  return [
    { month: 'Jan', plan: 120, fact: 134, forecast: 128 },
    { month: 'Feb', plan: 125, fact: 118, forecast: 124 },
    { month: 'Mar', plan: 130, fact: 142, forecast: 136 },
    { month: 'Apr', plan: 135, fact: 129, forecast: 133 },
    { month: 'May', plan: 140, fact: 151, forecast: 146 },
    { month: 'Jun', plan: 145, fact: 148, forecast: 150 },
  ];
}

Options (chart-level)

OptionTypeDefaultDescription
enabledbooleantrueshow the tooltip
mode'single' | 'shared''single'one node or the whole category
position.anchorTo'node' | 'center' | 'pointer''node'node edge, node center, or the cursor
position.xOffsetPixels0tooltip offset
yOffsetPixels0tooltip offset
rangePixels | 'exact' | 'nearest'30number — radius in px; 'exact' — only direct hits on a node; 'nearest' — nearest node from anywhere in the plot area

Series options

series[].tooltip.renderer(params) returns a string (which becomes the heading) or a structure:

ts
renderer: ({ datum, xValue, yValue, seriesName, color }) => ({
  heading: String(xValue),
  rows: [{ label: seriesName, value: String(yValue), color }],
});

params.datum is the entire data row: the tooltip can display fields that are not part of the series.

Highlighting is controlled by the highlight block:

OptionTypeDefaultDescription
enabledbooleantruehighlight the node and dim the other series
dimOpacityFraction0.8opacity of non-highlighted series (1 — no dimming)
highlight.enabledbooleantruenode highlighting and dimming
highlight.dimOpacityFraction0.8opacity of non-highlighted series