function axisGrid(options?: AxisLayerOptions & { readonly background?: boolean }): LayerCreates a layer that draws the grid lines of the named axes (default: all) in g.ct-grids, one line.ct-grid per tick. List it first, so the series draw over it.
Parameters
options?AxisLayerOptions & { readonly background?: boolean }={}The axes to draw and their classes;
backgroundadds arect.ct-grid-backgroundover the chart area, behind the lines.
Example
import {
autoScaleAxis,
axisGrid,
axisLabels,
cartesianAxes,
chart,
line,
stepAxis
} from 'chartist';
const hours = ['6am', '9am', '12pm', '3pm', '6pm'];
chart('#chart', {
data: { labels: hours, series: [[12, 17, 23, 21, 15]] },
axes: cartesianAxes([
{ id: 'time', side: 'bottom', type: stepAxis(), ticks: hours },
{ id: 'celsius', side: 'left', type: autoScaleAxis() }
]),
// Horizontal lines only, on a background rect.
layers: [
axisGrid({ axes: ['celsius'], background: true }),
line(),
axisLabels()
]
});function axisLabels(options?: AxisLabelsOptions): LayerCreates a layer that draws the labels of the named axes (default: all) in g.ct-labels, each on its axis's side. Axes on the same side stack outward, each in its own offset. List it last, so the labels draw over the series.
Labels are HTML in a foreignObject (as in 1.x) or SVG text: the chart's labels option decides, unless the layer sets its own.
Parameters
options?AxisLabelsOptions={}
Example
import {
autoScaleAxis,
axisGrid,
axisLabels,
cartesianAxes,
chart,
line,
stepAxis
} from 'chartist';
const weeks = ['W1', 'W2', 'W3', 'W4'];
chart('#chart', {
data: { labels: weeks, series: [[3, 5, 4, 8]] },
axes: cartesianAxes([
{ id: 'week', side: 'bottom', type: stepAxis(), ticks: weeks },
{ id: 'km', side: 'left', type: autoScaleAxis(), low: 0 }
]),
layers: [
axisGrid(),
line(),
// SVG text instead of HTML, e.g. to export the SVG.
axisLabels({ labels: 'svg' })
]
});function bar(options?: BarLayerOptions): LayerCreates a layer that draws series as bars, in the markup of the 1.x bar chart (g.ct-series with a line.ct-bar per value). The label axis is X (Y with horizontalBars), the value axis the other one; both come from cartesianAxes.
Bars grow from zero on the value axis, so keep zero on it: low: 0, or referenceValue: 0. On a step axis they sit in the middle of each step.
Parameters
options?BarLayerOptions={}
Example
import {
autoScaleAxis,
axisGrid,
axisLabels,
bar,
cartesianAxes,
chart,
stepAxis
} from 'chartist';
const quarters = ['Q1', 'Q2', 'Q3', 'Q4'];
chart('#chart', {
data: {
labels: quarters,
series: [
[800, 1200, 1400, 1300],
[200, 400, 500, 300]
]
},
axes: cartesianAxes([
{ id: 'quarter', side: 'bottom', type: stepAxis(), ticks: quarters },
{ id: 'sales', side: 'left', type: autoScaleAxis(), low: 0 }
]),
layers: [axisGrid(), bar({ seriesBarDistance: 12 }), axisLabels()],
className: 'ct-chart-bar'
});function line(options?: LineLayerOptions): LayerCreates a layer that draws series as lines, with points and optional areas, in the markup of the 1.x line chart (g.ct-series with path.ct-line, line.ct-point and path.ct-area). It needs an X and a Y axis, from cartesianAxes.
Parameters
options?LineLayerOptions={}
Example
import {
autoScaleAxis,
axisGrid,
axisLabels,
cartesianAxes,
chart,
line,
stepAxis
} from 'chartist';
const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
chart('#chart', {
data: {
labels: days,
series: [
{ name: 'visits', data: [12, 9, 7, 8, 5] },
{ name: 'sales', data: [2, 1, 3.5, 7, 3] }
]
},
axes: cartesianAxes([
{ id: 'day', side: 'bottom', type: stepAxis(), ticks: days },
{ id: 'count', side: 'left', type: autoScaleAxis(), low: 0 }
]),
layers: [
axisGrid(),
line({ showArea: true, overrides: { sales: { showArea: false } } }),
axisLabels()
]
});function determineAnchorPosition(center: Dot, label: Dot, direction: LabelDirection): AnchorPositionThe 1.x label anchor: start, middle or end by side and direction.
Parameters
centerDotlabelDotdirectionLabelDirection
function pie(options?: PieLayerOptions): LayerCreates a layer that draws a flat series as pie or donut slices with labels, in the markup of the 1.x pie chart (a g.ct-series per slice, then a group of text.ct-label). It draws in polar coordinates, so the chart needs coordinates: 'polar' and no axes. With several series, it draws one slice per series, from each series' first value.
Parameters
options?PieLayerOptions={}
Example
import { chart, pie } from 'chartist';
chart('#chart', {
data: {
labels: ['Rent', 'Food', 'Fun'],
series: [50, 30, 20]
},
coordinates: 'polar',
layers: [pie({ donut: true, donutWidth: 30 })],
className: 'ct-chart-donut'
});interface AxisLayerClassNames extends AxisClassNamesThe classes axisGrid and axisLabels give what they draw. The defaults are the 1.x classes, so 1.x CSS applies.
Properties
readonly gridGroupstringThe group of all grid lines (
ct-grids).readonly labelGroupstringThe group of all labels (
ct-labels).readonly gridBackgroundstringThe rect behind the grid, with
background: true(ct-grid-background).readonly gridstringEach grid line (
ct-grid).readonly labelstringEach label (
ct-label).readonly horizontalstringGrid lines and labels of an X axis (
ct-horizontal).readonly verticalstringGrid lines and labels of a Y axis (
ct-vertical).readonly startstringLabels of an axis at the top or left (
ct-start).readonly endstringLabels of an axis at the bottom or right (
ct-end).
interface AxisLayerOptionsThe options of axisGrid and axisLabels.
Properties
readonly axes?readonly string[]Axis ids to draw; default: all axes, in declaration order. An unknown id throws.
readonly classNames?Partial<AxisLayerClassNames>Replaces some of the classes; the others keep their 1.x names.
interface AxisLabelsOptions extends AxisLayerOptionsThe options of axisLabels: those of every axis layer, plus how the labels are drawn.
Properties
readonly axes?readonly string[]Axis ids to draw; default: all axes, in declaration order. An unknown id throws.
readonly classNames?Partial<AxisLayerClassNames>Replaces some of the classes; the others keep their 1.x names.
readonly labels?LabelModeHTML (
foreignObject) or SVGtext; default: the chart's mode.readonly align?Partial<LabelAlign>Aligns SVG text labels in their box, on every axis of the layer; default: as the 1.x CSS aligns HTML labels. HTML labels ignore it (align them with CSS).
interface BarLayerOptionsThe options of the bar layer. Without them, it draws the bars of each label side by side, 15 px apart, on the first X and Y axis.
Properties
readonly series?readonly (string | number)[]Series to draw, 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 horizontalBars?booleanValues on the X axis, labels on the Y axis.
readonly stackBars?booleanStacks the bars of a label on each other;
stackModesays how. The value axis grows to fit the stacks (seeLayer.extent).readonly stackMode?boolean | 'accumulate' | 'accumulate-relative' | 'overlap'How stacked bars stack:
accumulate(default) adds each value to the ones before,accumulate-relativestacks positive values up and negative values down from zero, andoverlap(ortrue) draws every bar from zero, on top of each other.readonly seriesBarDistance?numberDistance between the bars of different series, in px.
readonly distributeSeries?booleanOne series per value of a flat series (1.x
distributeSeries).readonly reverseData?booleanDraws the series in reverse order (1.x
reverseData). Can't be combined withseries.readonly classNames?{ readonly series?: string; readonly bar?: string }Replaces the classes of what the layer draws:
series(ct-series) andbar(ct-bar).
interface LineSeriesOptionsThe line options you can set per series: in the overrides of line, or the series option of lineChart.
Properties
readonly lineSmooth?boolean | LineInterpolationHow a line runs between its points:
truesmooths it withInterpolation.monotoneCubic,falsedraws straight segments (Interpolation.none), or pass an interpolation such asInterpolation.cardinal(),Interpolation.step()or your own.readonly showPoint?booleanDraws a point at each value.
readonly showLine?booleanDraws the lines.
readonly showArea?booleanFills the area between each line and
areaBase.readonly areaBase?numberThe value the areas close at, usually 0. It's kept within the Y axis range.
interface LineLayerOptions extends LineSeriesOptionsThe options of the line layer. Without them, it draws every series as a smooth line with points and no area, on the first X and Y axis.
Properties
readonly lineSmooth?boolean | LineInterpolationHow a line runs between its points:
truesmooths it withInterpolation.monotoneCubic,falsedraws straight segments (Interpolation.none), or pass an interpolation such asInterpolation.cardinal(),Interpolation.step()or your own.readonly showPoint?booleanDraws a point at each value.
readonly showLine?booleanDraws the lines.
readonly showArea?booleanFills the area between each line and
areaBase.readonly areaBase?numberThe value the areas close at, usually 0. It's kept within the Y axis range.
readonly series?readonly (string | number)[]Series to draw, 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?'center' | 'start'Point position on a step axis:
startof each step (1.x) or itscenter, where bars are centered.readonly overrides?Readonly<Record<string, LineSeriesOptions>>Option overrides per series name (the 1.x
seriesoption).readonly reverseData?booleanDraws the series in reverse order (1.x
reverseData). Can't be combined withseries.readonly classNames?{ readonly series?: string; readonly line?: string; readonly point?: string; readonly area?: string }Replaces the classes of what the layer draws:
series(ct-series),line(ct-line),point(ct-point) andarea(ct-area).
interface PieLayerOptionsThe options of the pie layer. Without them, it draws a filled pie from the top, with a label halfway out on each slice.
Properties
readonly startAngle?numberWhere the first slice starts, in degrees clockwise from the top.
readonly total?numberFixed total; slices are relative to it (gauges). Defaults to the sum.
readonly donut?booleanDraws a donut: rings instead of filled slices.
readonly donutWidth?string | numberDonut ring width in px, or a share of the radius (
'25%'). Without it, the stylesheet sets the width (--ct-donut-width, 60px by default), and in the browser the ring is laid out for the width the stylesheet gives a slice. Without a DOM (toScene(),renderToString()), it's laid out for 60 px, so setdonutWidththere if the page's CSS draws another width.readonly showLabel?booleanDraws a label for each slice; on by default.
readonly labelOffset?numberMoves the labels away from the center (positive) or toward it (negative), in px.
readonly labelPosition?'center' | 'inside' | 'outside'Where labels go:
inside(the default: halfway out on a pie, on the middle of a donut's ring),outside(at the outer edge) orcenter(in the middle; move them withlabelOffset).readonly labelInterpolationFnc?(value: Label, index: number) => Label | null | undefinedFormats a slice's label: its entry in
labels, or else its value.readonly labelDirection?LabelDirectionWhich way labels anchor:
neutral(the default),explode(away from the center, for labels outside the pie) orimplode(toward it).readonly ignoreEmptyValues?booleanLeaves out the slices and labels of zero values.
readonly preventOverlappingLabelOffset?numberShifts labels that would overlap another one by this many px, up and left, judging the overlap by the length of the drawn label text.
readonly classNames?{ readonly series?: string; readonly slicePie?: string; readonly sliceDonut?: string; readonly label?: string }Replaces the classes of what the layer draws:
series(ct-series),slicePie(ct-slice-pie),sliceDonut(ct-slice-donut) andlabel(ct-label).
type Coordinates = 'cartesian' | 'polar'The coordinate system a chart draws in: cartesian places values on X and Y axes (lines, bars), polar around a center (pies and donuts).
interface LayerContextWhat a layer's compose gets: the data it draws, the area to draw in, the axes and ways to measure text and styles.
Properties
readonly dataPreparedDataThe prepared data; only the layer's series when it selects some.
readonly areaAreaThe drawing area, in px in the SVG.
readonly paddingChartPaddingThe chart's padding, in px.
readonly layerIndexnumberThe layer's index in the chart's
layers; part of its nodes' keys.readonly coordinatesCoordinatesThe chart's coordinate system.
readonly measureTextMeasureTextMeasures text with the renderer's metrics (in the browser, with the page's fonts and the chart's classes), for layers that lay out labels.
readonly measureStyle?MeasureStyleReads computed styles with the renderer (in the browser, with the page's CSS and the chart's classes), for layout that depends on the stylesheet, such as a donut's ring width. Absent without a DOM.
readonly axesChartAxesAxes created after
layout, when the chart has axes.readonly normalized?NormalizedChartDataThe data as the axes see it, when the chart has axes.
readonly seriesIndexes?readonly number[]Chart-wide index of each series in
data(a layer may select a subset of the series). Absent when the layer draws all series.readonly labelsLabelModeHow label layers draw labels: chart option, else the renderer's.
interface ExtentContextWhat a layer's extent gets: its series as the axes see them, and the ids of the axes it draws on.
Properties
readonly normalizedNormalizedChartDataThe layer's series, normalized as for the axes (
{ x, y }values or holes); only the layer's series when it selects some.readonly axes{ readonly x?: string; readonly y?: string }The ids of the X and Y axis the layer draws on.
interface LayerA layer renders series into the chart area. Built-in and custom layers use the same contract.
Properties
readonly typestringName used in error messages, e.g.
line.readonly chartType?stringThe kind of chart the layer draws, when
typedoesn't say it: achartwithoutclassNamegetsct-chart-barif a layer is abar, elsect-chart-pieorct-chart-donutfor apieordonut, elsect-chart-line. Without it,typecounts.readonly coordinatesreadonly Coordinates[]Coordinate systems the layer can draw in.
readonly axes?{ readonly x?: string; readonly y?: string }Axis ids to draw on; default: the first X and the first Y axis.
readonly series?readonly (string | number)[]Series to draw, by index or name; default: all series.
extent?(context: ExtentContext) => Readonly<Record<string, AxisExtent>>Reports the range of values the layer needs on its axes beyond the values themselves, by axis id, such as the sums of stacked bars. Scale axes include it in the range they fit to the data (an axis's own
lowandhighstill win); on an axis several layers report a range for, the axis covers them all. Runs before the axes are created, on every layout.compose(context: LayerContext) => readonly SceneNode[]Draws the layer: returns its top-level nodes, usually one
seriesgroup per series. The pipeline keys their data nodes and passes every node with a role through thecomposelisteners.