Skip to content

Zoom and Navigator

Zoom

zoom: { enabled: true } enables wheel zoom around the cursor, panning by dragging, and reset by double click. Right click (with contextMenu) opens a menu with PNG download and zoom reset.

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

export function createOptions(): ChartOptions {
  return {
    data: getData(),
    title: { text: 'Zoom and pan' },
    subtitle: { text: 'wheel/pinch — zoom, drag — select area, alt+drag — pan, dblclick — reset' },
    series: [{ type: 'line', xField: 'index', yField: 'value', name: 'Value', marker: { enabled: false } }],
    axes: [
      { type: 'number', position: 'bottom', nice: false },
      { type: 'number', position: 'left' },
    ],
    zoom: { enabled: true, dragSelect: true, panKey: 'alt' },
    contextMenu: { enabled: true },
    legend: { enabled: false },
  };
}
ts
export function getData() {
  const points = [];
  let value = 100;
  for (let i = 0; i < 120; i++) {
    value += Math.sin(i / 7) * 4 + ((i * 13) % 9) - 4;
    points.push({ index: i, value: Math.round(value * 10) / 10 });
  }
  return points;
}

A strip below the plot area with a window of the visible range and handles:

Modular build

Zoom is part of the core, while when building with grafit-charts/core the navigator is a separate module: register(navigatorModule).

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

export function createOptions(): ChartOptions {
  return {
    data: getData(),
    title: { text: 'Navigator' },
    subtitle: { text: 'the window and handles below control the visible range' },
    series: [{ type: 'area', xField: 'date', yField: 'value', name: 'Metric' }],
    axes: [
      { type: 'time', position: 'bottom' },
      { type: 'number', position: 'left' },
    ],
    navigator: { enabled: true, min: 0.6, max: 1 },
    zoom: { enabled: true },
    legend: { enabled: false },
  };
}
ts
export function getData() {
  const start = Date.UTC(2024, 0, 1);
  const day = 24 * 60 * 60 * 1000;
  const points = [];
  let value = 50;
  for (let i = 0; i < 365; i++) {
    value += Math.sin(i / 30) * 1.5 + ((i * 7) % 5) - 2;
    points.push({ date: new Date(start + i * day), value: Math.round(value * 10) / 10 });
  }
  return points;
}
OptionTypeDefaultDescription
enabledbooleanfalseenable the navigator
sync.groupIdstringshared groupchart synchronization group
sync.nodeInteractionbooleantruesynchronization of node highlighting
zoom.axes'x' | 'y' | 'xy''x'zoomable axes
zoom.wheelZoombooleantruewheel zoom
zoom.wheelStepnumber0.1zoom step per wheel tick
zoom.dragPanbooleantruepanning by dragging
zoom.panKey'alt' | 'ctrl' | 'shift' | 'meta'pan modifier (without it, drag is box-zoom)
zoom.dragSelectbooleanfalsearea selection → zoom
zoom.doubleClickResetbooleantruereset by double click
zoom.minRationumber0.05minimum window width (fraction of the domain)
navigator.heightPixels24navigator strip height
navigator.miniChart.enabledbooleantrueseries miniature in the strip
navigator.min0..10window start
navigator.max0..11window end

A pinch gesture on touch devices zooms around the gesture center. The navigator window and zoom are a single state: the wheel updates the navigator and vice versa. The current window is saved in the chart state.