Everything is exported from @chartist-js/essentials. Types from chartist link to the Chartist API reference, where Rendering covers the scene and the renderers.
function axisTitle(options: AxisTitleOptions): LayerNames an axis with a title next to its labels, on any side of the chart. The layer reserves the room the title needs, so the drawing area shrinks and the title never overlaps the labels, whatever font size the stylesheet gives it. One layer draws one title; give each axis its own. Titles on the same side stack outward in the order of layers.
The title is an SVG text with the classes ct-axis-title, ct-horizontal or ct-vertical and ct-start or ct-end, styled with --ct-axis-title-color and --ct-axis-title-size from @chartist-js/essentials/styles.css.
Parameters
optionsAxisTitleOptions
Example
import { lineChart } from 'chartist';
import { axisTitle } from '@chartist-js/essentials';
import 'chartist/styles.css';
import '@chartist-js/essentials/styles.css';
lineChart(
'#chart',
{ labels: ['Mon', 'Tue', 'Wed'], series: [[12, 9, 7]] },
{
layers: {
above: [
axisTitle({ axis: 'x', text: 'Day' }),
axisTitle({ axis: 'y', text: 'Visits' })
]
}
}
);function logAxis(options?: LogAxisOptions): AxisFactoryCreates a logarithmic axis, for values that span several orders of magnitude: each power of the base (1, 10, 100, … for base 10) is the same distance from the next, so growth by a factor looks the same everywhere. It goes in the type of a preset's axisX or axisY, or of an axis of cartesianAxes().
Without low and high, the range runs from the power at or below the smallest value to the power at or above the largest; the axis's own low and high set it exactly. The ticks are the powers, every second or third when they'd be closer than scaleMinSpace; the axis's ticks replace them. Zero and negative values have no place on a log scale: they don't count towards the range and are drawn at the start of the axis, so a bar for zero has no length. Use null for a gap.
Trend lines on a log axis are fitted to the logarithms of the values, so they're straight on the axis: an exponential trend on a log Y axis.
Parameters
options?LogAxisOptions={}The base and whether to draw minor grid lines.
Example
import { lineChart } from 'chartist';
import { logAxis } from '@chartist-js/essentials';
lineChart(
'#chart',
{
labels: ['2019', '2020', '2021', '2022', '2023', '2024'],
series: [[120, 980, 4100, 36000, 210000, 850000]]
},
{
axisY: {
type: logAxis({ minorTicks: true }),
labelTransform: value => value.toLocaleString('en-US')
}
}
);function targetLine(options: TargetLineOptions): LayerCreates a layer that draws a line across the chart area at a constant value on one axis, such as a goal or a limit (line.ct-target-line). On a Y axis the line is horizontal, on an X axis vertical. It can have a label, drawn as SVG text (text.ct-target-label) next to it inside the chart area, and a class, e.g. ct-series-b for a series' color. The axis widens to include the value, unless its own low or high leave it out; a line outside the axis isn't drawn. For several lines, add a layer per line.
Parameters
optionsTargetLineOptions
Example
import { lineChart } from 'chartist';
import { targetLine } from '@chartist-js/essentials';
import 'chartist/styles.css';
import '@chartist-js/essentials/styles.css';
lineChart(
'#chart',
{ labels: ['Jan', 'Feb', 'Mar', 'Apr'], series: [[620, 810, 740, 930]] },
{
low: 0,
layers: {
above: [
targetLine({ value: 700, label: 'Min' }),
targetLine({ value: 1000, label: 'Goal', className: 'ct-series-b' })
]
}
}
);function trendLine(options?: TrendLineOptions): LayerCreates a layer that draws a trend line for each series: the straight line that fits its values best (least squares), from its smallest to its largest X, as a line.ct-trend-line in a series group (g.ct-series.ct-series-a), so it takes the series' color. On a step axis X is the value's index, on a scale axis the value's x. Holes are skipped, and a series with fewer than two values gets no line. The Y axis widens to include the line's ends. On an axis with a transform, such as a log axis, the line is fitted to the transformed values, so it's straight on that axis: an exponential trend on a log Y axis. Add it next to the line() or bar() layer that draws the values.
Parameters
options?TrendLineOptions={}
Example
import { lineChart } from 'chartist';
import { trendLine } from '@chartist-js/essentials';
import 'chartist/styles.css';
import '@chartist-js/essentials/styles.css';
lineChart(
'#chart',
{
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
series: [{ name: 'sales', data: [620, 810, 740, 930, 880, 1020] }]
},
{ layers: { above: [trendLine()] } }
);function valueLabels<R extends ValueLabelRole = ValueLabelRole>(options?: ValueLabelsOptions<R>): LayerCreates a layer that writes each value next to the point or bar that shows it: above points, past the ends of bars, and in the middle of stacked segments. It draws from where the chart's other layers drew, after stacking, offsets and compose listeners, so add it as an extra layer: layers.above on a preset, anywhere in a composed chart.
The text is the value along the value axis, written with Intl.NumberFormat; format gets the same item as the format of @chartist-js/tooltips. Labels that don't fit their bar, or would leave the chart, move to a place where they fit. They're SVG text, so they render on servers and in the terminal, styled with --ct-value-label-color, --ct-value-label-inside-color and --ct-value-label-size from @chartist-js/essentials/styles.css.
Parameters
options?ValueLabelsOptions<R>={}
Example
import { barChart } from 'chartist';
import { valueLabels } from '@chartist-js/essentials';
import 'chartist/styles.css';
import '@chartist-js/essentials/styles.css';
barChart(
'#chart',
{ labels: ['Q1', 'Q2', 'Q3'], series: [[1250, 1830, 1470]] },
{
layers: {
above: [
valueLabels({ format: item => `${item.value} units` })
]
}
}
);interface AxisTitleOptionsOptions of axisTitle.
Properties
readonly axisstringThe id of the axis the title names:
xoryon the presets, the ids you gave incartesianAxes()in composed charts.readonly textstringThe title's text; an empty text draws nothing and takes no room.
readonly align?'start' | 'middle' | 'end'Where along the axis the title sits: at the start (left on X axes, the bottom on Y axes), in the middle or at the end.
readonly flip?booleanTurns titles of left and right axes to read from top to bottom instead of from bottom to top. Titles of top and bottom axes don't change.
readonly gap?numberSpace between the title and the axis labels, in px.
readonly className?stringExtra class names for the title, e.g.
ct-series-bfor a color.readonly classNames?{ readonly title?: string }Replaces the default class:
titleinstead ofct-axis-title.
interface LogAxisOptionsOptions of logAxis.
Properties
readonly base?numberThe base of the logarithm: the ticks are its powers. Any finite number above 1, such as 2 for a scale that doubles; default 10.
readonly minorTicks?booleanDraws minor grid lines between the ticks, without labels: at 2 to 9 times each power for base 10, or at the powers left out when the ticks skip some. Style them with
--ct-grid-minor-*.
interface TargetLineContext extends NoSeriesThe context of a targetLine node: the line of a targetLine() layer, across the chart area at a value on an axis. Its ends are in px in the SVG.
Properties
readonly axisScaleAxisThe axis the value is on;
axis.dimensionisxory.readonly valuenumberThe value the line is drawn at.
readonly label?stringThe line's label, if it has one.
readonly x1numberThe X of the line's start, in px.
readonly y1numberThe Y of the line's start, in px.
readonly x2numberThe X of the line's end, in px.
readonly y2numberThe Y of the line's end, in px.
readonly seriesIndex?undefinedAlways absent: the node belongs to no series.
readonly valueIndex?undefinedAlways absent: the node stands for no value.
interface TargetLabelContext extends NoSeriesThe context of a targetLabel node: the label of a target line, and where it's anchored, in px in the SVG.
Properties
readonly axisScaleAxisThe axis of the line's value.
readonly valuenumberThe value of the line.
readonly textstringThe label as drawn.
readonly xnumberPx from the left edge of the SVG.
readonly ynumberPx from the top edge of the SVG.
readonly seriesIndex?undefinedAlways absent: the node belongs to no series.
readonly valueIndex?undefinedAlways absent: the node stands for no value.
interface TrendLineContextThe context of a trendLine node: the line a trendLine() layer fits through a series' values, y = slope * x + intercept in the axes' values (X is the value's index on a step axis). Its ends are in px in the SVG.
Properties
readonly seriesIndexnumberThe series' index in the chart's data, across layers.
readonly valueIndex?undefinedAlways absent: the line stands for the whole series.
readonly slopenumberHow much the fitted value changes per unit of X. On axes with a transform, such as a log axis, it's in their transformed values: on a base 10 log Y axis, the powers of ten per unit of X.
readonly interceptnumberThe fitted value at X 0, in transformed values like
slope.readonly correlation?numberHow closely the values follow the line: Pearson's correlation coefficient r, from -1 (every value on a falling line) through 0 (no straight trend) to 1 (every value on a rising line). Its square is R², the share of the values' variation the line explains, as spreadsheets show it next to a trendline. It describes the fit, not how reliable it is: with two values it's always 1 or -1. On axes with a transform, it's computed in their transformed values, so it says how straight the values look on the chart. Absent when all values are equal, where it isn't defined.
readonly x1numberThe X of the line's start (the series' smallest X), in px.
readonly y1numberThe Y of the line's start, in px.
readonly x2numberThe X of the line's end (the series' largest X), in px.
readonly y2numberThe Y of the line's end, in px.
interface TargetLineOptionsThe options of the targetLine layer: the line's value, its label and class, and the axis it's on. For several lines, add a layer per line.
Properties
readonly valuenumberThe value the line is drawn at, on the layer's axis. On a step axis, the position by index, as for the axis's ticks.
readonly label?stringThe text drawn next to the line; no label without it.
readonly className?stringExtra classes for the line and its label, e.g.
ct-series-b.readonly axis?stringThe id of the axis the value is on: a Y axis draws a horizontal line, an X axis a vertical one. Default: the first Y axis.
readonly labelPosition?'start' | 'end'Where the label sits along the line: at the
end(right, or top for a vertical line) or thestart(left, or bottom).readonly classNames?{ readonly line?: string; readonly label?: string }Replaces the classes of what the layer draws:
line(ct-target-line) andlabel(ct-target-label).
interface TrendLineOptionsThe options of the trendLine layer. Without them, it draws a trend line for every series, on the first X and Y axis.
Properties
readonly series?readonly (string | number)[]Series to draw a trend line for, by index or name; default: all series.
readonly x?stringThe id of the X axis to draw on; default: the first X axis.
readonly y?stringThe id of the Y axis to draw on; default: the first Y axis.
readonly align?'start' | 'center'Where the line's ends sit on a step axis: at the
startof their steps, as the points ofline(), or at thecenter, where bars are centered.readonly classNames?{ readonly series?: string; readonly line?: string }Replaces the classes of what the layer draws:
series(ct-series) andline(ct-trend-line).
type ValueLabelRole = 'point' | 'bar'The roles whose nodes get labels.
type PointLabelPosition = 'above' | 'below' | 'left' | 'right' | 'center'Where a point's label sits.
type BarLabelPosition = 'end' | 'inside-end' | 'center' | 'inside-base'Where a bar's label sits, from the bar's base to its value: past its end, inside its end, in its middle or inside its base.
type ValueLabelItem<R extends ValueLabelRole = ValueLabelRole> = { [K in R]: { readonly role: K; readonly seriesIndex: number; readonly valueIndex: number; readonly value: number | { readonly x: number; readonly y: number }; readonly meta: unknown; readonly series: unknown; readonly seriesName?: string; readonly label?: string; readonly context: NodeRoles[K]['ctx'] } }[R]The value a label is about: what format gets. It has the fields of the item @chartist-js/tooltips gives its format, under the same names, so a function written for one mostly works for the other. Checking item.role narrows item.context to that role's context.
interface ValueLabelContextThe context of a valueLabel node.
Properties
readonly roleValueLabelRoleThe role of the node the label is for.
readonly seriesIndexnumberThe series' index in the chart's data.
readonly valueIndexnumberThe value's index in its series.
readonly valueunknownThe labeled value, as the node has it.
readonly metaunknownThe value's
meta.readonly seriesunknownThe series as given in the data.
readonly textstringThe label's text.
readonly position'left' | 'right' | 'end' | 'center' | 'above' | 'below' | 'inside-end' | 'inside-base'Where the label ended up, after fitting.
readonly xnumberPx from the left edge of the SVG: where the text is anchored.
readonly ynumberPx from the top edge of the SVG: the first line's baseline.
interface ValueLabelsOptions<R extends ValueLabelRole = ValueLabelRole>Options of valueLabels.
Properties
readonly roles?readonly R[]The nodes that get labels:
point,baror both (the default).readonly series?readonly (string | number)[]The series to label, by index or name; default: all of them.
readonly pointPosition?PointLabelPositionWhere point labels sit; default
above.readonly barPosition?BarLabelPositionWhere bar labels sit; default
end, andcenterfor segments of stacked bars.readonly offset?numberThe gap in px between a point's center, or a bar's end or base, and the label's nearest edge; default 8 for points and 4 for bars.
readonly format?(item: ValueLabelItem<R>) => string | number | null | undefinedThe label's text for a value: a string or a number.
undefinedkeeps the default text,nullor''draw no label, and\nstarts a new line.readonly numberFormat?NumberFormatOptionsIntl.NumberFormatoptions for the default text.readonly locale?string | readonly string[]The locale of the default text; default: the runtime's. Set it when charts render on a server, so the text doesn't depend on its locale.
readonly overflow?'fit' | 'show'fit(the default) moves labels that don't fit their bar or would leave the chart;showdraws every label where its position puts it.readonly labels?'svg' | 'html'SVG
text(the default) or HTML in aforeignObject.readonly classNames?{ readonly group?: string; readonly label?: string; readonly inside?: string }Replace the classes:
group(ct-value-labels),label(ct-value-label) andinside(ct-value-label-inside).