Skip to content

chart ​

Draws horizontal bar charts for terminal output. barChart(...) returns a layout node. Render it with render from std::ui/layout, so a chart nests inside box / row / column. Pass the series and categories directly or build them up in a trailing block. Each series gets a distinct color and fill symbol when you omit them, so charts stay readable even with color disabled. Negative values draw left of a zero baseline. Stacked bars must be uniform-sign.

ts
import { barChart } from "std::ui/chart"
import { render } from "std::ui/layout"

const c = barChart(
  title: "Revenue by quarter",
  mode: "stacked",
  keys: [{ name: "web", color: "blue" }, { name: "app", color: "green" }],
  data: [
    { label: "Q1", values: [120, 80] },
    { label: "Q2", values: [98, 90] },
  ],
)
print(render(c))

The block form builds the same chart imperatively:

ts
const c = barChart(title: "Revenue", mode: "stacked") as ch {
  ch.key("web", color: "blue")
  ch.key("app", color: "green")
  ch.bar("Q1", 120, 80)
  ch.bar("Q2", 98, 90)
}

Types ​

BarKey ​

A series. color/symbol auto-assign if omitted.

ts
/** A series. `color`/`symbol` auto-assign if omitted. */
export type BarKey = {
  name: string;
  color?: string;
  symbol?: string
}

(source)

Bar ​

One category. values aligns to keys by index.

ts
/** One category. `values` aligns to `keys` by index. */
export type Bar = {
  label: string;
  values: number[]
}

(source)

BarMode ​

ts
export type BarMode = "stacked" | "grouped"

(source)

ChartBuilder ​

Methods inside a barChart trailing as c { ... } block.

ts
/** Methods inside a `barChart` trailing `as c { ... }` block. */
export type ChartBuilder = {
  key: any;
  bar: any
}

(source)

Functions ​

barChart ​

ts
barChart(
  title: string = "",
  mode: BarMode = "grouped",
  keys: BarKey[] = null,
  data: Bar[] = null,
  showValues: boolean = true,
  legend: boolean = true,
  max: number = 0,
  barChar: string = "█",
  width: Width = null,
  block: (ChartBuilder) -> void = null,
): LayoutNode

Build a horizontal bar chart to display with std::ui/layout render. Stacked values within each category must all have the same sign.

Parameters:

NameTypeDefaultDescription
titlestring""
modeBarMode"grouped"Grouped draws a bar per series; stacked combines them per category
keysBarKey[]nullSeries definitions. A color is a name like "blue" or a hex string like "#cc7a4a"; omitted colors and symbols are assigned automatically
dataBar[]nullCategories with values aligned to keys by index
showValuesbooleantrueShow the numeric value beside each bar
legendbooleantrueShow the series names and symbols
maxnumber0Positive axis maximum; longer bars saturate at it, and 0 derives the maximum from data
barCharstring"█"Default fill symbol for the first or only series
widthWidthnullColumns, a percentage of the parent such as "50%", or "full"
block(ChartBuilder) => voidnull

Returns: LayoutNode

(source)