Skip to content

Annotations

Declarative marks in data coordinates — drawn on top of the series and surviving zoom/resize. Interactive drawing is planned for future phases.

Modular build

When building with grafit-charts/core, annotations are a separate module: register(annotationsModule).

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

export function createOptions(): ChartOptions {
  return {
    data: getData(),
    title: { text: 'Annotations' },
    series: [{ type: 'line', xField: 'month', yField: 'price', name: 'Price' }],
    annotations: [
      { type: 'horizontal-line', value: 180, stroke: '#e5484d', label: { text: 'resistance 180' } },
      { type: 'range', axis: 'x', range: ['Mar', 'Apr'], label: { text: 'correction' } },
      { type: 'line', start: { x: 'Jan', y: 140 }, end: { x: 'Aug', y: 196 }, stroke: '#21a06c', lineDash: [6, 4] },
      { type: 'text', x: 'Jun', y: 192, text: 'peak' },
    ],
    legend: { enabled: false },
  };
}
ts
export function getData() {
  return [
    { month: 'Jan', price: 142 },
    { month: 'Feb', price: 149 },
    { month: 'Mar', price: 161 },
    { month: 'Apr', price: 155 },
    { month: 'May', price: 171 },
    { month: 'Jun', price: 188 },
    { month: 'Jul', price: 179 },
    { month: 'Aug', price: 195 },
  ];
}

Horizontal and vertical lines can be dragged with the mouse (always enabled).

Types

TypeFieldsDescription
horizontal-linevalue, stroke?, lineDash?, label?horizontal level
vertical-linevalue (category/date), …vertical mark
linestart: {x, y}, end: {x, y}arbitrary segment (trend line)
textx, y, text, color?, fontSize?label at a data point
rangeaxis: 'x' | 'y', range: [a, b], fill?, label?filled range

Full list of options

OptionTypeDefaultDescription
strokeWidthlines1annotation line width
fillOpacityrange0.12range fill opacity
label.textstringline label (horizontal/vertical-line)
label.fontSizePixels11label font size
label.colorColorValueline colorlabel color

Coordinates are specified as data values: categories/dates for X, numbers for Y.

horizontal-line and vertical-line can be dragged with the mouse — the value updates along the scale (categorical lines snap to the nearest category).