renderChart — 6.0.0

Build structured charts with named series, native string or Date X values, mixed modes, annotations, and a secondary Y-axis.

Signature

Signature
renderChart<X extends ChartX>(spec: ChartSpec<X>): string

Executable examples

Structured chart

Source
renderChart({
  width: 42,
  height: 10,
  series: [
    { id: 'latency', data: [['Mon', 42], ['Tue', 68], ['Wed', 51]] },
    { id: 'errors', data: [['Mon', 2], ['Tue', 7], ['Wed', 3]], yAxis: 'secondary' },
  ],
  xAxis: { scale: 'band' },
  secondaryYAxis: { domain: [0, 10] },
  annotations: [{ id: 'warning', type: 'span', axis: 'y', from: 60, to: 80 }],
  legend: { position: 'bottom', series: true },
});
Terminal output

  ▲                                          ▲ 
68┤░░░░░░░░░░░░░░░░░░░░┏━━━━━━━━━━━━┓░░░░░░░░│ 
  │░░░░░░░░░░░░░░░░░░░░┃░░░░░░░░░░░░┃░░░░░░░░│ 
  │░░░░░░░░░░░░░░░░░░░░┃░░░░░░░░░░░░┃░░░░░░░░│ 
  │░░░░░░░░░░░░░░░░░░░░┏━━━━━━━━━━━━┓░░░░░░░░├7
  │                    ┃            ┃        │ 
  │                    ┃            ┃        │ 
51┤                    ┃            ┗━       ├3
  │       ━━━━━━━━━━━━━┛                     ├2
  │                    ┃                     │ 
42┤       ━━━━━━━━━━━━━┛                     │ 
  └───────┬─────────────┬────────────┬───────┘ 
        Mon           Tue          Wed         
█ latency                                      
█ errors                                       

ChartSpec

Complete structured chart specification.

type ChartSpec = {
  /** Structured series collection. May be empty; explicit axis domains can define an empty frame. */
  series: readonly ChartSeries<X>[];
  /** Fixed plot width or `auto` for active terminal columns. Defaults to 60. */
  width?: ChartWidth;
  /** Plot height in terminal rows. Defaults to 15 unless derived. */
  height?: number;
  /** Desired physical width-to-height ratio used when height is derived. */
  aspectRatio?: number;
  /** Title rendered above the chart. */
  title?: string;
  /** ANSI color applied to the title. */
  titleColor?: Color;
  /** Line behavior outside explicit domains. Defaults to `discard`. */
  overflow?: Overflow;
  /** Plot-cell backend. Defaults to `ascii`. */
  renderer?: Renderer;
  /** Native X-axis configuration. */
  xAxis?: XAxis<X>;
  /** Primary numeric Y-axis configuration. */
  yAxis?: YAxis;
  /** Independently scaled right-side Y-axis configuration. */
  secondaryYAxis?: YAxis;
  /** Legend placement, entity selection, and ANSI color. */
  legend?: ChartLegend;
  /** Explicit numeric X/Y axis intersection. */
  axisCenter?: MaybePoint;
  /** Named point overlays rendered on the primary Y-axis. */
  points?: readonly ChartOverlayPoint<X>[];
  /** Named reference lines rendered on the primary Y-axis. */
  thresholds?: readonly ChartThreshold<X>[];
  /** Data-coordinate spans, text, arrows, and error bars. */
  annotations?: readonly ChartAnnotation<X>[];
  /** Per-call glyph overrides. */
  symbols?: Symbols;
  /** ANSI color applied to the configured border glyph. */
  borderColor?: Color;
  /** ANSI color applied to background glyphs. */
  backgroundColor?: Color;
  /** Logs out-of-bounds drawing attempts for diagnostics. */
  debugMode?: boolean;
  /** Shared arrangement for multi-series bars. Defaults to `overlap`. */
  barLayout?: BarLayout;
  /** Enables bar endpoint labels or configures their text and color. */
  valueLabels?: ChartValueLabels<X>;
};

series

readonly ChartSeries<X>[]Required

Structured series collection. May be empty; explicit axis domains can define an empty frame.

Related executable examples: Structured chart

width

ChartWidthOptional

Fixed plot width or auto for active terminal columns. Defaults to 60.

Related executable examples: Structured chart

height

numberOptional

Plot height in terminal rows. Defaults to 15 unless derived.

Related executable examples: Structured chart

aspectRatio

numberOptional

Desired physical width-to-height ratio used when height is derived.

Related executable examples: Structured chart

title

stringOptional

Title rendered above the chart.

Related executable examples: Structured chart

titleColor

ColorOptional

ANSI color applied to the title.

Related executable examples: Structured chart

overflow

OverflowOptional

Line behavior outside explicit domains. Defaults to discard.

Related executable examples: Structured chart

renderer

RendererOptional

Plot-cell backend. Defaults to ascii.

Related executable examples: Structured chart

xAxis

XAxis<X>Optional

Native X-axis configuration.

Related executable examples: Structured chart

yAxis

YAxisOptional

Primary numeric Y-axis configuration.

Related executable examples: Structured chart

secondaryYAxis

YAxisOptional

Independently scaled right-side Y-axis configuration.

Related executable examples: Structured chart

legend

ChartLegendOptional

Legend placement, entity selection, and ANSI color.

Related executable examples: Structured chart

axisCenter

MaybePointOptional

Explicit numeric X/Y axis intersection.

Related executable examples: Structured chart

points

readonly ChartOverlayPoint<X>[]Optional

Named point overlays rendered on the primary Y-axis.

Related executable examples: Structured chart

thresholds

readonly ChartThreshold<X>[]Optional

Named reference lines rendered on the primary Y-axis.

Related executable examples: Structured chart

annotations

readonly ChartAnnotation<X>[]Optional

Data-coordinate spans, text, arrows, and error bars.

Related executable examples: Structured chart

symbols

SymbolsOptional

Per-call glyph overrides.

Related executable examples: Structured chart

borderColor

ColorOptional

ANSI color applied to the configured border glyph.

Related executable examples: Structured chart

backgroundColor

ColorOptional

ANSI color applied to background glyphs.

Related executable examples: Structured chart

debugMode

booleanOptional

Logs out-of-bounds drawing attempts for diagnostics.

Related executable examples: Structured chart

barLayout

BarLayoutOptional

Shared arrangement for multi-series bars. Defaults to overlap.

Related executable examples: Structured chart

valueLabels

ChartValueLabels<X>Optional

Enables bar endpoint labels or configures their text and color.

Related executable examples: Structured chart

ChartSeries

One independently styled series in a structured chart.

type ChartSeries = {
  /** Unique series identifier used by legends and callback context. */
  id: string;
  /** Human-readable series label. Defaults to `id`. */
  name?: string;
  /** Native-X/numeric-Y values; `null` creates a continuity gap. */
  data: readonly ChartDatum<X>[];
  /** Series rendering primitive. Defaults to `line`. */
  mode?: GraphMode;
  /** Line-segment interpolation. Defaults to `step`. */
  interpolation?: Interpolation;
  /** Static ANSI series color. */
  color?: Color;
  /** Dynamic per-cell series-color strategy. */
  coloring?: Coloring;
  /** Fills the region between a line and the X-axis baseline. */
  fillArea?: boolean;
  /** Custom glyph callback for occupied series cells. */
  lineFormatter?: (args: LineFormatterArgs) => CustomSymbol | CustomSymbol[];
  /** Y-axis assignment. Defaults to `primary`. */
  yAxis?: 'primary' | 'secondary';
};

id

stringRequired

Unique series identifier used by legends and callback context.

Related executable examples: Structured chart

name

stringOptional

Human-readable series label. Defaults to id.

Related executable examples: Structured chart

data

readonly ChartDatum<X>[]Required

Native-X/numeric-Y values; null creates a continuity gap.

Related executable examples: Structured chart

mode

GraphModeOptional

Series rendering primitive. Defaults to line.

Related executable examples: Structured chart

interpolation

InterpolationOptional

Line-segment interpolation. Defaults to step.

Related executable examples: Structured chart

color

ColorOptional

Static ANSI series color.

Related executable examples: Structured chart

coloring

ColoringOptional

Dynamic per-cell series-color strategy.

Related executable examples: Structured chart

fillArea

booleanOptional

Fills the region between a line and the X-axis baseline.

Related executable examples: Structured chart

lineFormatter

(args: LineFormatterArgs) => CustomSymbol | CustomSymbol[]Optional

Custom glyph callback for occupied series cells.

Related executable examples: Structured chart

yAxis

'primary' | 'secondary'Optional

Y-axis assignment. Defaults to primary.

Related executable examples: Structured chart

ChartLegend

Legend configuration and optional entity selection by id.

type ChartLegend = {
  /** Placement relative to the plot. */
  position: LegendPosition;
  /** `true` shows all series; an ID array selects and orders entries. */
  series?: boolean | readonly string[];
  /** `true` shows all points; an ID array selects and orders entries. */
  points?: boolean | readonly string[];
  /** `true` shows all thresholds; an ID array selects and orders entries. */
  thresholds?: boolean | readonly string[];
  /** ANSI color applied to legend labels. */
  color?: Color;
};

position

LegendPositionRequired

Placement relative to the plot.

Related executable examples: Structured chart

series

boolean | readonly string[]Optional

true shows all series; an ID array selects and orders entries.

Related executable examples: Structured chart

points

boolean | readonly string[]Optional

true shows all points; an ID array selects and orders entries.

Related executable examples: Structured chart

thresholds

boolean | readonly string[]Optional

true shows all thresholds; an ID array selects and orders entries.

Related executable examples: Structured chart

color

ColorOptional

ANSI color applied to legend labels.

Related executable examples: Structured chart

ChartOverlayPoint

Named overlay point for a structured chart.

type ChartOverlayPoint = Omit<GraphPoint, 'x'> & {
  /** Unique point identifier used by legends. */
  id: string;
  /** Human-readable point label. Defaults to `id`. */
  name?: string;
  /** Native X coordinate. */
  x: X;
};

y

numberRequired

Numeric Y coordinate.

Related executable examples: Structured chart

color

ColorOptional

ANSI color applied to the point glyph.

Related executable examples: Structured chart

id

stringRequired

Unique point identifier used by legends.

Related executable examples: Structured chart

name

stringOptional

Human-readable point label. Defaults to id.

Related executable examples: Structured chart

ChartThreshold

Named threshold for a structured chart.

type ChartThreshold = Omit<Threshold, 'x'> & {
  /** Unique threshold identifier used by legends. */
  id: string;
  /** Human-readable threshold label. Defaults to `id`. */
  name?: string;
  /** Optional native X coordinate for a vertical reference line. */
  x?: X;
};

y

numberOptional

Y coordinate for a horizontal reference line.

Related executable examples: Structured chart

color

ColorOptional

ANSI color applied to the reference line.

Related executable examples: Structured chart

id

stringRequired

Unique threshold identifier used by legends.

Related executable examples: Structured chart

name

stringOptional

Human-readable threshold label. Defaults to id.

Related executable examples: Structured chart

x

XOptional

Optional native X coordinate for a vertical reference line.

Related executable examples: Structured chart

XAxis

Structured X-axis configuration.

type XAxis = {
  /** Scale kind. Defaults to the kind inferred from X values. */
  scale?: XScale;
  /** Explicit category order or continuous scale extent. */
  domain?: XAxisDomain<X>;
  /** Exact native tick values or an exact tick count of at least two. */
  ticks?: number | readonly X[];
  /** Axis label rendered outside tick labels. */
  label?: string;
  /** Formatter called with native X values. */
  formatter?: AxisFormatter<X>;
  /** Hides the axis line, ticks, labels, and axis label. */
  hidden?: boolean;
  /** Hides tick markers and labels while retaining the axis line. */
  hideTicks?: boolean;
  /** Handling for labels that do not fit the available width. */
  labelCollision?: LabelCollisionPolicy;
  /** ANSI color applied to the axis line, ticks, tick labels, and label. */
  color?: Color;
};

scale

XScaleOptional

Scale kind. Defaults to the kind inferred from X values.

Related executable examples: Structured chart

domain

XAxisDomain<X>Optional

Explicit category order or continuous scale extent.

Related executable examples: Structured chart

ticks

number | readonly X[]Optional

Exact native tick values or an exact tick count of at least two.

Related executable examples: Structured chart

label

stringOptional

Axis label rendered outside tick labels.

Related executable examples: Structured chart

formatter

AxisFormatter<X>Optional

Formatter called with native X values.

Related executable examples: Structured chart

hidden

booleanOptional

Hides the axis line, ticks, labels, and axis label.

Related executable examples: Structured chart

hideTicks

booleanOptional

Hides tick markers and labels while retaining the axis line.

Related executable examples: Structured chart

labelCollision

LabelCollisionPolicyOptional

Handling for labels that do not fit the available width.

Related executable examples: Structured chart

color

ColorOptional

ANSI color applied to the axis line, ticks, tick labels, and label.

Related executable examples: Structured chart

YAxis

Numeric Y-axis configuration.

type YAxis = {
  /** Explicit numeric extent as `[minimum, maximum]`. */
  domain?: readonly [number, number];
  /** Exact numeric tick values or an exact tick count of at least two. */
  ticks?: number | readonly number[];
  /** Axis label rendered outside tick labels. */
  label?: string;
  /** Formatter called for each numeric Y-axis tick. */
  formatter?: Formatter;
  /** Hides the axis line, ticks, labels, and axis label. */
  hidden?: boolean;
  /** Hides tick markers and labels while retaining the axis line. */
  hideTicks?: boolean;
  /** Reserves enough width for complete Y-axis tick labels. */
  showTickLabel?: boolean;
  /** ANSI color applied to the axis line, ticks, tick labels, and label. */
  color?: Color;
};

domain

readonly [number, number]Optional

Explicit numeric extent as [minimum, maximum].

Related executable examples: Structured chart

ticks

number | readonly number[]Optional

Exact numeric tick values or an exact tick count of at least two.

Related executable examples: Structured chart

label

stringOptional

Axis label rendered outside tick labels.

Related executable examples: Structured chart

formatter

FormatterOptional

Formatter called for each numeric Y-axis tick.

Related executable examples: Structured chart

hidden

booleanOptional

Hides the axis line, ticks, labels, and axis label.

Related executable examples: Structured chart

hideTicks

booleanOptional

Hides tick markers and labels while retaining the axis line.

Related executable examples: Structured chart

showTickLabel

booleanOptional

Reserves enough width for complete Y-axis tick labels.

Related executable examples: Structured chart

color

ColorOptional

ANSI color applied to the axis line, ticks, tick labels, and label.

Related executable examples: Structured chart

Coloring

Dynamic per-cell series-color strategy.

type Coloring = {
  /** Selects callback-based colors. */
  type: 'conditional';
  /** Returns a color or `undefined` to use the static series fallback. */
  getColor: (context: ColorContext) => Color | undefined;
} | {
  /** Selects equal-band palette coloring. */
  type: 'gradient';
  /** Data-space axis used to select a palette band. */
  by: 'x' | 'y';
  /** Nonempty ordered ANSI color palette. */
  colors: readonly Color[];
  /** Optional finite palette extent. Defaults to the resolved chart domain. */
  domain?: readonly [number, number];
};

type

'conditional' | 'gradient'Required

Selects callback-based colors.

Related executable examples: Structured chart

getColor

(context: ColorContext) => Color | undefinedRequired

Returns a color or undefined to use the static series fallback.

Related executable examples: Structured chart

by

'x' | 'y'Required

Data-space axis used to select a palette band.

Related executable examples: Structured chart

colors

readonly Color[]Required

Nonempty ordered ANSI color palette.

Related executable examples: Structured chart

domain

readonly [number, number]Optional

Optional finite palette extent. Defaults to the resolved chart domain.

Related executable examples: Structured chart

ChartValueLabels

Enables bar endpoint labels or configures their text and ANSI color.

type ChartValueLabels = boolean | {
  /** Formats the rendered value; normalized layouts receive a percentage. */
  formatter?: ValueLabelFormatter<X>;
  /** ANSI color applied to every value label. */
  color?: Color;
};

formatter

ValueLabelFormatter<X>Optional

Formats the rendered value; normalized layouts receive a percentage.

Related executable examples: Structured chart

color

ColorOptional

ANSI color applied to every value label.

Related executable examples: Structured chart

ChartSpanAnnotation

Reference region spanning an inclusive X or Y interval.

type ChartSpanAnnotation = ChartAnnotationBase & (Readonly<{
  /** Span annotation discriminator. */
  type: 'span';
  /** Fills an X-axis interval across the complete plot height. */
  axis: 'x';
  /** Inclusive native interval start. */
  from: X;
  /** Inclusive native interval end. */
  to: X;
  /** One-column fill glyph. */
  symbol?: string;
}> | Readonly<{
  /** Span annotation discriminator. */
  type: 'span';
  /** Fills a Y-axis interval across the complete plot width. */
  axis: 'y';
  /** Inclusive numeric interval start. */
  from: number;
  /** Inclusive numeric interval end. */
  to: number;
  /** One-column fill glyph. */
  symbol?: string;
}>);

id

stringRequired

Unique annotation identifier.

Related executable examples: Structured chart

color

ColorOptional

ANSI color applied to the complete annotation.

Related executable examples: Structured chart

type

'span'Required

Span annotation discriminator.

Related executable examples: Structured chart

axis

'x' | 'y'Required

Fills an X-axis interval across the complete plot height.

Related executable examples: Structured chart

from

X | numberRequired

Inclusive native interval start.

Related executable examples: Structured chart

to

X | numberRequired

Inclusive native interval end.

Related executable examples: Structured chart

symbol

stringOptional

One-column fill glyph.

Related executable examples: Structured chart

ChartTextAnnotation

Text anchored at one data coordinate.

type ChartTextAnnotation = ChartAnnotationBase & Readonly<{
  /** Text annotation discriminator. */
  type: 'text';
  /** Native X anchor. */
  x: X;
  /** Numeric Y anchor. */
  y: number;
  /** Display-safe terminal text. */
  text: string;
  /** Horizontal placement relative to the anchor. Defaults to `right`. */
  align?: 'left' | 'center' | 'right';
}>;

id

stringRequired

Unique annotation identifier.

Related executable examples: Structured chart

color

ColorOptional

ANSI color applied to the complete annotation.

Related executable examples: Structured chart

type

'text'Required

Text annotation discriminator.

Related executable examples: Structured chart

text

stringRequired

Display-safe terminal text.

Related executable examples: Structured chart

align

'left' | 'center' | 'right'Optional

Horizontal placement relative to the anchor. Defaults to right.

Related executable examples: Structured chart

ChartArrowAnnotation

Arrow connecting two data coordinates.

type ChartArrowAnnotation = ChartAnnotationBase & Readonly<{
  /** Arrow annotation discriminator. */
  type: 'arrow';
  /** Native-X/numeric-Y path start. */
  from: readonly [x: X, y: number];
  /** Native-X/numeric-Y path end and arrowhead position. */
  to: readonly [x: X, y: number];
  /** One-column path glyph. */
  lineSymbol?: string;
}>;

id

stringRequired

Unique annotation identifier.

Related executable examples: Structured chart

color

ColorOptional

ANSI color applied to the complete annotation.

Related executable examples: Structured chart

type

'arrow'Required

Arrow annotation discriminator.

Related executable examples: Structured chart

from

readonly [x: X, y: number]Required

Native-X/numeric-Y path start.

Related executable examples: Structured chart

to

readonly [x: X, y: number]Required

Native-X/numeric-Y path end and arrowhead position.

Related executable examples: Structured chart

lineSymbol

stringOptional

One-column path glyph.

Related executable examples: Structured chart

ChartErrorBarAnnotation

Error whiskers centered on one data coordinate.

type ChartErrorBarAnnotation = ChartAnnotationBase & Readonly<{
  /** Error-bar annotation discriminator. */
  type: 'errorBar';
  /** Native X center. */
  x: X;
  /** Numeric Y center. */
  y: number;
  /** Symmetric or asymmetric Y-axis error magnitude. */
  yError?: ChartErrorMagnitude;
}> & ([X] extends [string] ? Readonly<{
  xError?: never;
}> : Readonly<{
  /** Symmetric or asymmetric X-axis error magnitude. */
  xError?: ChartErrorMagnitude;
}>);

id

stringRequired

Unique annotation identifier.

Related executable examples: Structured chart

color

ColorOptional

ANSI color applied to the complete annotation.

Related executable examples: Structured chart

type

'errorBar'Required

Error-bar annotation discriminator.

Related executable examples: Structured chart

yError

ChartErrorMagnitudeOptional

Symmetric or asymmetric Y-axis error magnitude.

Related executable examples: Structured chart