Skip to content

API ​

Written from the source at each release.

Functions ​

FunctionFromDoes
createChart(host, spec, options?)nervuredraws a chart and returns its handle
Chartnervure/reactReact component
NervureChartnervure/vueVue component
chartnervure/svelteSvelte action
<nervure-chart>nervure/elementweb component
live, poll, events, socket, appendnervure/livelive data
eventStreamnervure/serverevent stream for your server

Specs, options and handle ​

ts
/** Colours and fonts. Every field is optional when passed by the caller. */
export interface Theme { strong: string; label: string; muted: string; grid: string; halo: string; surface: string; font: string; /** Colours used when a series or a point has none. */ palette: string[] }

/** A line across the chart: a target, an average, a date. */
export interface ReferenceLine { value?: number; /** Vertical line on a category (xy) or on the x axis (scatter). */ at?: string | number; axis?: number; label?: string; color?: string; dashed?: boolean }
/** A note pinned on the chart. `at` is a category (xy) or an x value (scatter). */
export interface Annotation { at: string | number; value: number; text: string; color?: string; axis?: number }

export interface Series {
  name: string;
  data: (number | null)[];
  color?: string;
  kind?: 'line' | 'bar' | 'area' | 'band';
  /** Lower bound of a `band` (`data` is the upper one). */
  low?: (number | null)[];
  /** Colour of one point or bar, when it differs from the series. */
  colors?: (string | undefined)[];
  dashed?: boolean;
  gradient?: boolean;
  dots?: boolean;
  showLabel?: boolean;
  /** Text of each label, instead of the formatted value. */
  labels?: (string | undefined)[];
  /** Joins points with straight segments (default), a smooth curve or steps. */
  curve?: 'linear' | 'smooth' | 'step';
  /** Index in `axes`. */
  axis?: number;
  legend?: boolean;
  width?: number;
  opacity?: number;
}
export interface Axis { side?: 'left' | 'right'; min?: number; max?: number; title?: string; format?: (value: number) => string }

export interface DecorateContext { svg: SVGSVGElement; width: number; height: number; theme: Theme }
interface Base {
  title?: string; description?: string; format?: (value: number, series?: string) => string;
  /** Text written above the drawing (part of the exports). */
  heading?: string; subheading?: string;
  /** Pixels kept free above the drawing, for a header added by `decorate`. */
  insetTop?: number;
  /** Pixels kept free under the drawing (a watermark). */
  insetBottom?: number;
  /** Adds your own elements (header, watermark); they are part of the exports. */
  decorate?: (context: DecorateContext) => void;
}
export interface XYSpec extends Base {
  type: 'xy'; categories: string[]; series: Series[]; axes?: Axis[]; horizontal?: boolean; stacked?: boolean;
  /** Horizontal bars: first category at the top (default) or at the bottom. */
  reverse?: boolean;
  totals?: boolean; legend?: boolean; labelWidth?: number; grid?: boolean | 'dashed';
  /** Title under the horizontal axis. */
  xTitle?: string;
  /** Angle of the category labels (vertical charts), for example -45. */
  labelAngle?: number;
  lines?: ReferenceLine[]; annotations?: Annotation[];
  /** No axis, no legend, no margin: a sparkline. */
  bare?: boolean;
}
export interface PieSpec extends Base { type: 'pie'; data: { name: string; value: number; color?: string }[]; inner?: number; outer?: number; labels?: boolean; /** Outside label of a slice. */ label?: (name: string, value: number, percent: number) => string; /** Text in the hole of a donut. */ center?: string }
export interface RadarSpec extends Base { type: 'radar'; indicators: { name: string; max?: number }[]; series: { name: string; values: number[]; color?: string }[] }
export interface GaugeSpec extends Base { type: 'gauge'; value: number; max?: number; color?: string; /** Mark on the arc. */ target?: number; name?: string; unit?: string }
export interface ScatterPoint { x: number; y: number; r?: number; label: string; color?: string; id?: string; /** Radius in pixels, instead of `r`. */ radius?: number; group?: string; [key: string]: unknown }
export interface ScatterSpec extends Base {
  type: 'scatter'; data: ScatterPoint[]; color?: string; xLabel?: string; yLabel?: string;
  /** Names written above the points. */
  labels?: boolean; legend?: boolean; lines?: ReferenceLine[]; annotations?: Annotation[];
  tooltip?: (point: ScatterPoint, format: (v: number) => string) => string[];
  onPointClick?: (point: ScatterPoint) => void;
}
export interface Item { name: string; value: number; color?: string; label?: string; id?: string }
interface Clickable { onItemClick?: (id: string) => void }
export interface TreemapSpec extends Base, Clickable { type: 'treemap'; data: Item[] }
export interface PackSpec extends Base, Clickable { type: 'pack'; data: Item[] }
export interface FunnelSpec extends Base, Clickable { type: 'funnel'; data: Item[]; /** Share of the first step (default) or of the previous one. */ rate?: 'first' | 'previous' | false }
/** Steps from a start to an end; `total: true` restarts from zero. */
export interface WaterfallSpec extends Base { type: 'waterfall'; data: { name: string; value: number; total?: boolean; color?: string }[]; up?: string; down?: string; totalColor?: string }
export interface HeatmapSpec extends Base { type: 'heatmap'; columns: string[]; rows: string[]; values: (number | null)[][]; color?: string; /** Values written in the cells. */ labels?: boolean; labelWidth?: number }
export interface TimelineBar { row: string; start: number; end: number; color?: string; label?: string; detail?: string }
export interface TimelineMark { row: string; at: number; color?: string; label?: string; id?: string }
/** Dates are timestamps in milliseconds. */
export interface TimelineSpec extends Base, Clickable { type: 'timeline'; rows: string[]; bars?: TimelineBar[]; marks?: TimelineMark[]; now?: number; nowLabel?: string; labelWidth?: number; formatDate?: (t: number) => string }
export interface TreeNode { name: string; color?: string; children?: TreeNode[] }
/** Radial tree; a click on a node folds or unfolds it. */
export interface TreeSpec extends Base { type: 'tree'; root: TreeNode; /** Deepest level whose names are written. */ labelDepth?: number }
export type ChartSpec = XYSpec | PieSpec | RadarSpec | GaugeSpec | ScatterSpec | TreemapSpec | PackSpec | FunnelSpec | WaterfallSpec | HeatmapSpec | TimelineSpec | TreeSpec;

export interface RenderOptions { theme?: Partial<Theme>; locale?: string; animate?: boolean }
export interface ChartHandle {
  readonly svg: SVGSVGElement;
  /** Draws another spec, with its entrance animation. */
  update(spec: ChartSpec, options?: RenderOptions): void;
  /** Draws new data at once, without animation (live charts). */
  refresh(spec: ChartSpec): void;
  resize(): void;
  /** Colour of a series or of an item, by name. Kept until `resetColors`. */
  setColor(name: string, color: string): void;
  setPalette(colors: string[]): void;
  resetColors(): void;
  /** Adds a note or a line (`xy` and `scatter`). Kept until `clearNotes`. */
  addAnnotation(note: Annotation): void;
  addLine(line: ReferenceLine): void;
  clearNotes(): void;
  destroy(): void;
  /** Standalone SVG document. */
  toSVG(): string;
  toPNG(scale?: number, background?: string): Promise<Blob>;
}

Live data ​

ts
export type LiveStatus = 'connecting' | 'live' | 'paused' | 'retrying' | 'stopped';
/** Where data comes from. `start` is called again after an error; it returns how to stop. */
export interface Source<T> { start(push: (data: T) => void, fail: (error: unknown) => void, signal: AbortSignal): void }
export interface PollOptions<T> { every: number; init?: RequestInit; parse?: (response: Response) => Promise<T> }
/** Calls `url` (or your function) every `every` milliseconds. The next call starts after the previous answer. */
export function poll<T = unknown>(url: string | ((signal: AbortSignal) => Promise<T>), { every, init, parse }: PollOptions<T>): Source<T> { … }
/** Server-sent events: one JSON message per event. */
export function events<T = unknown>(url: string, { event = 'message', withCredentials = false, parse = JSON.parse as (text: string) => T } = {}): Source<T> { … }
/** WebSocket; `hello` is sent on opening (a subscription, for example). */
export function socket<T = unknown>(url: string, { hello, protocols, parse = JSON.parse as (text: string) => T }: { hello?: unknown; protocols?: string[]; parse?: (text: string) => T } = {}): Source<T> { … }
export interface LiveOptions<T> { … }
export interface Live { stop(): void; pause(): void; resume(): void; readonly status: LiveStatus }
/** Feeds a chart from a source. Retries with growing waits, and draws the last message only. */
export function live<T>(chart: ChartHandle, initial: ChartSpec, source: Source<T>, o: LiveOptions<T>): Live { … }
/** Adds one point to an `xy` spec and keeps the last `keep` ones. `values` is by series name; a missing one is a gap. */
export function append(spec: XYSpec, category: string, values: Record<string, number | null>, keep = 60): XYSpec { … }

MIT License