Everything is exported from @chartist-js/terminal. Types from chartist link to the Chartist API reference, where Rendering covers the scene and the renderers.
function renderToTerminal(scene: SceneNode, options?: TerminalRenderOptions): stringRenders a scene as text for a terminal: lines, points and areas in braille dots, bars in block characters, slices filled, the grid in dim box-drawing characters, labels as text, all colored by series with ANSI escape codes. Lay the scene out with terminalSize, so its px map to cells.
Parameters
sceneSceneNodeoptions?TerminalRenderOptions={}
Example
import { lineChartConfig, toScene } from 'chartist';
import { renderToTerminal, terminalSize } from '@chartist-js/terminal';
const scene = toScene(
lineChartConfig({ labels: ['Mon', 'Tue', 'Wed'], series: [[5, 2, 4]] }),
terminalSize(60, 15)
);
console.log(renderToTerminal(scene, { color: '256' }));function lineChart(data: LineChartData, options?: TerminalLineChartOptions): stringRenders a line chart for the terminal, from the data and options of Chartist's lineChart, at columns × rows cells: lines and areas in braille dots, colored by series.
Parameters
dataLineChartDataoptions?TerminalLineChartOptions={}
Example
import { lineChart } from '@chartist-js/terminal';
console.log(
lineChart(
{ labels: ['Mon', 'Tue', 'Wed', 'Thu'], series: [[12, 9, 7, 8], [2, 1, 3.5, 7]] },
{ columns: 60, rows: 15, showArea: true, color: '256' }
)
);function barChart(data: BarChartData, options?: TerminalBarChartOptions): stringRenders a bar chart for the terminal, from the data and options of Chartist's barChart (stacked, horizontal and the others), at columns × rows cells: bars in block characters, colored by series.
Parameters
dataBarChartDataoptions?TerminalBarChartOptions={}
Example
import { barChart } from '@chartist-js/terminal';
console.log(
barChart(
{ labels: ['Q1', 'Q2', 'Q3'], series: [[5, 4, 3], [3, 2, 9]] },
{ stackBars: true, columns: 50, rows: 12 }
)
);function pieChart(data: PieChartData, options?: TerminalPieChartOptions): stringRenders a pie or donut chart for the terminal, from the data and options of Chartist's pieChart, at columns × rows cells: slices filled with braille dots, each label on its slice.
Parameters
dataPieChartDataoptions?TerminalPieChartOptions={}
Example
import { pieChart } from '@chartist-js/terminal';
console.log(
pieChart(
{ labels: ['Bananas', 'Apples', 'Kiwis'], series: [20, 10, 30] },
{ donut: true, columns: 40, rows: 16 }
)
);function detectColorMode(stream: ColorStream, env: Readonly<Record<string, string | undefined>>): ColorModePicks the color mode for an output stream from environment variables, the way most CLI tools do:
- none when
NO_COLORis set,TERMisdumbor the stream isn't a terminal; FORCE_COLORoverrides that:0none,116 colors,2256 colors,324-bit;- otherwise 24-bit when
COLORTERMistruecoloror24bit, else 256 colors.
It reads only what it's given, so it works outside Node.js too.
Parameters
streamColorStreamThe output, usually
process.stdoutenvReadonly<Record<string, string | undefined>>The environment variables, usually
process.env
Example
import { detectColorMode, lineChart } from '@chartist-js/terminal';
const color = detectColorMode(process.stdout, process.env);
console.log(lineChart({ series: [[1, 3, 2]] }, { color }));const CELL_WIDTH: 8The width in px a terminal cell stands for in a scene: a glyph is half as wide as high.
const CELL_HEIGHT: 16The height in px a terminal cell stands for in a scene: one line.
const measureCells: MeasureTextMeasures text in terminal cells: each character is one cell wide and one cell high, so labels are laid out on the character grid.
function terminalSize(columns: number, rows: number): SceneSizeThe size to lay out a chart at for columns × rows terminal cells, for toScene(): in px, with text measured in cells and SVG text labels.
Parameters
columnsnumberrowsnumber
function renderToHtml(scene: SceneNode): stringRenders a scene as HTML text art: the terminal output in a pre element (class ct-terminal), each run of cells in a span with classes instead of colors: ct-series-a to ct-series-z, ct-bg-series-a for the series behind a cell, and the kind (ct-grid, ct-label, ct-area, ct-bar, ct-line, ct-point, ct-slice). Load @chartist-js/terminal/styles.css to color it; it reads Chartist's --ct-series-a and other custom properties, so themes apply, and draws braille, blocks and grid lines with a font of its own that fills each cell. Each character is an i element: one cell. Lay the scene out with terminalSize.
Parameters
sceneSceneNode
Example
import { pieChartConfig, toScene } from 'chartist';
import { renderToHtml, terminalSize } from '@chartist-js/terminal';
import '@chartist-js/terminal/styles.css';
const scene = toScene(
pieChartConfig({ labels: ['A', 'B'], series: [3, 2] }),
terminalSize(30, 12)
);
document.querySelector('#chart')!.innerHTML = renderToHtml(scene);function legend(data: { readonly labels?: readonly unknown[]; readonly series?: readonly unknown[] }, options?: LegendOptions): stringMakes a legend for a chart's data: a colored marker and the name of each named series (series objects with a name), in series colors. For pie data, whose series are values, the names are the labels. Series without a name are left out but keep their color. Returns an empty string when nothing has a name.
Parameters
data{ readonly labels?: readonly unknown[]; readonly series?: readonly unknown[] }options?LegendOptions={}
Example
import { legend, lineChart } from '@chartist-js/terminal';
const data = {
labels: ['Mon', 'Tue', 'Wed'],
series: [
{ name: 'Visitors', data: [12, 9, 7] },
{ name: 'Sign-ups', data: [2, 1, 3] }
]
};
console.log(lineChart(data, { columns: 60, rows: 12 }));
console.log(legend(data, { columns: 60 }));interface TerminalRenderOptionsOptions of renderToTerminal.
Properties
readonly color?ColorModeHow colors are written;
nonewrites plain text.readonly palette?readonly string[]Series colors as hex, by series letter (
afirst); Chartist's default series colors otherwise. Series past the end start over.
interface TerminalChartOptionsThe terminal options of the chart functions, next to the 1.x options.
Properties
readonly columns?numberThe width in terminal columns.
readonly rows?numberThe height in terminal rows.
readonly color?ColorModeHow text output is colored; HTML output uses classes instead.
readonly palette?readonly string[]Series colors as hex for text output, by series letter (
afirst).readonly format?'html' | 'text'textfor terminals, orhtmlforrenderToHtmlmarkup.
type TerminalLineChartOptions = Omit<LineChartPresetOptions, 'width' | 'height'> & TerminalChartOptionsOptions of the terminal lineChart: the options of Chartist's lineChart and the terminal options.
Properties
readonly columns?numberThe width in terminal columns.
readonly rows?numberThe height in terminal rows.
readonly color?ColorModeHow text output is colored; HTML output uses classes instead.
readonly palette?readonly string[]Series colors as hex for text output, by series letter (
afirst).readonly format?'html' | 'text'textfor terminals, orhtmlforrenderToHtmlmarkup.
type TerminalBarChartOptions = Omit<BarChartPresetOptions, 'width' | 'height'> & TerminalChartOptionsOptions of the terminal barChart: the options of Chartist's barChart and the terminal options.
Properties
readonly columns?numberThe width in terminal columns.
readonly rows?numberThe height in terminal rows.
readonly color?ColorModeHow text output is colored; HTML output uses classes instead.
readonly palette?readonly string[]Series colors as hex for text output, by series letter (
afirst).readonly format?'html' | 'text'textfor terminals, orhtmlforrenderToHtmlmarkup.
type TerminalPieChartOptions = Omit<PieChartPresetOptions, 'width' | 'height'> & TerminalChartOptionsOptions of the terminal pieChart: the options of Chartist's pieChart and the terminal options.
Properties
readonly columns?numberThe width in terminal columns.
readonly rows?numberThe height in terminal rows.
readonly color?ColorModeHow text output is colored; HTML output uses classes instead.
readonly palette?readonly string[]Series colors as hex for text output, by series letter (
afirst).readonly format?'html' | 'text'textfor terminals, orhtmlforrenderToHtmlmarkup.
type ColorMode = 'truecolor' | '256' | '16' | 'none'How text output is colored: 24-bit colors, the 256-color palette, the 16 standard colors, or none (plain text without escape codes).
interface ColorStreamWhat detectColorMode reads from an output stream.
Properties
readonly isTTY?booleanWhether the stream is a terminal, as
process.stdout.isTTY.
interface LegendOptionsOptions of legend.
Properties
readonly color?ColorModeHow text output is colored; HTML output uses classes instead.
readonly palette?readonly string[]Series colors as hex for text output, by series letter (
afirst).readonly columns?numberThe width in columns; entries that don't fit go on the next line.
readonly format?'html' | 'text'textfor terminals, orhtmlfor markup likerenderToHtml's.