function cartesianAxes(input: readonly AxisDefinition[] | AxesConfig, extra?: Pick<AxesConfig, 'reverseData' | 'normalize'>): CartesianAxesCreates the axes for the axes option of chart: a list of axes, each with an id, a side and a type, several per side if needed. Axis types are v2 factories (stepAxis, autoScaleAxis, fixedScaleAxis) or 1.x axis classes; the other fields are the 1.x axis options (offset, low, high, ticks, …).
Layers pick their axes by id, and each axis scales to the series of the layers drawn on it. Offsets default to 30 px for X axes and 40 px for Y axes, and axes on the same side stack outward in list order. Unknown or duplicate ids throw.
The 1.x-style { x, y } form also works, with the same defaults: X at the bottom unless its position is 'start', Y on the left unless it's 'end'.
Parameters
inputreadonly AxisDefinition[] | AxesConfigThe axes: a list of
AxisDefinitions, or the 1.x{ x, y }form.extra?Pick<AxesConfig, 'reverseData' | 'normalize'>={}reverseDataandnormalize, for the list form (the{ x, y }form takes them inline).
Example
import {
autoScaleAxis,
axisGrid,
axisLabels,
bar,
cartesianAxes,
chart,
line,
stepAxis
} from 'chartist';
const months = ['Jan', 'Feb', 'Mar', 'Apr'];
chart('#chart', {
data: {
labels: months,
series: [
{ name: 'eur', data: [120, 180, 150, 210] },
{ name: 'usd', data: [130, 195, 160, 230] }
]
},
axes: cartesianAxes([
{ id: 'month', side: 'bottom', type: stepAxis(), ticks: months },
// Both on the left: 'eur' next to the chart, 'usd' outside it.
{ id: 'eur', side: 'left', type: autoScaleAxis(), low: 0 },
{ id: 'usd', side: 'left', type: autoScaleAxis(), low: 0, offset: 50 }
]),
layers: [
axisGrid({ axes: ['month', 'eur'] }),
bar({ series: ['eur'], y: 'eur' }),
line({ series: ['usd'], y: 'usd', align: 'center' }),
axisLabels()
],
className: 'ct-chart-bar'
});function stepAxis(): AxisFactoryCreates a step axis: evenly spaced ticks, one per step, that place values by their index rather than by their value. It's the label axis of line and bar charts. Same rules as 1.x StepAxis.
Its ticks are the data's labels, or the axis's ticks when you give them. With stretch, the last tick sits at the axis end.
Example
import {
autoScaleAxis,
axisGrid,
axisLabels,
cartesianAxes,
chart,
line,
stepAxis
} from 'chartist';
const months = ['Jan', 'Feb', 'Mar', 'Apr'];
chart('#chart', {
data: { labels: months, series: [[4, 6, 5, 9]] },
axes: cartesianAxes([
// The months as ticks; stretch: the first and last at the edges.
{ id: 'month', side: 'bottom', type: stepAxis(), stretch: true },
{ id: 'value', side: 'left', type: autoScaleAxis() }
]),
layers: [axisGrid(), line(), axisLabels()]
});function autoScaleAxis(): AxisFactoryCreates an auto-scaled axis: a scale fitted to the data, with round ticks at least scaleMinSpace px apart. Set low, high or referenceValue to widen it, and onlyInteger for whole-number steps. Same rules as 1.x AutoScaleAxis.
It's the value axis of line and bar charts. As an X axis, it places { x, y } values at their real distances.
Example
import {
autoScaleAxis,
axisGrid,
axisLabels,
cartesianAxes,
chart,
line
} from 'chartist';
chart('#chart', {
data: {
series: [
[
{ x: 1, y: 2 },
{ x: 2.5, y: 5 },
{ x: 7, y: 3 },
{ x: 10, y: 8 }
]
]
},
axes: cartesianAxes([
{ id: 'x', side: 'bottom', type: autoScaleAxis(), onlyInteger: true },
{ id: 'y', side: 'left', type: autoScaleAxis(), low: 0 }
]),
layers: [axisGrid(), line(), axisLabels()]
});function fixedScaleAxis(): AxisFactoryCreates a fixed-scale axis: a scale from low to high (by default the smallest and largest value) with the ticks you give. Without ticks, divisor splits the range into equal parts with a tick at each end, so divisor: 4 makes 5 ticks from low to high (1.x left out the one at high).
Example
import {
axisGrid,
axisLabels,
cartesianAxes,
chart,
fixedScaleAxis,
line,
stepAxis
} from 'chartist';
const teams = ['Red', 'Blue', 'Green'];
chart('#chart', {
data: { labels: teams, series: [[64, 81, 47]] },
axes: cartesianAxes([
{ id: 'team', side: 'bottom', type: stepAxis(), ticks: teams },
// A percentage scale with three grid lines.
{
id: 'percent',
side: 'left',
type: fixedScaleAxis(),
ticks: [0, 50, 100],
low: 0,
high: 100
}
]),
layers: [axisGrid(), line({ showLine: false }), axisLabels()]
});type AxisDimension = 'x' | 'y'Which way an axis runs: x (horizontal) or y (vertical).
type AxisSide = 'top' | 'bottom' | 'left' | 'right'Where an axis sits: top/bottom are X axes, left/right Y axes.
interface AxisContextWhat an AxisFactory gets to create an axis: its direction, the data, the chart area and the axis options.
Properties
readonly dimensionAxisDimensionWhich way the axis runs:
xory, from its side.readonly seriesNormalizedSeries[]Normalized multi-value series (
{ x, y }values): the series bound to the axis, or all of them.readonly labelsreadonly Label[]Normalized labels, padded to the longest series.
readonly areaAreaThe chart area in px, with the offsets of all axes reserved.
readonly optionsAxisOptionsThe options of the axis definition, without
id,sideandtype.readonly extent?AxisExtentThe range the layers drawn on the axis need beyond their values, such as the sums of stacked bars (see
Layer.extent). Scale axes include it in the range they fit to the data; their ownlowandhighstill win.
interface AxisExtentA range of values on an axis, from low to high: what a layer reports in Layer.extent.
Properties
readonly lownumberThe lowest value.
readonly highnumberThe highest value.
interface ScaleAxisA positioned axis: ticks, range and projection into px along the axis. An AxisFactory creates it once the chart area is known; layers read it to place what they draw.
Properties
readonly dimensionAxisDimensionWhich way the axis runs:
xory.readonly kindstringThe axis type, e.g.
step,autoScale,fixedScale.readonly stretch?booleanStep axes: whether the last tick sits at the axis end.
readonly step?numberStep axes: the distance between two ticks in px.
readonly ticksreadonly Label[]The values at which grid lines and labels are drawn: labels on a step axis, numbers on a scale.
readonly range?{ readonly min: number; readonly max: number }The values at the axis start (
min) and end (max). Step axes have none.readonly lengthnumberAxis length in px.
project(value: string | number | Date | { x: number; y: number } | { x: number } | { y: number } | undefined, index?: number, series?: NormalizedSeries) => numberPlaces a value on the axis: the distance in px from the axis start (left, or bottom for y). Step axes place by
indexinstead.
interface AxisFactoryAn axis type: creates the axis once the chart area and data are known. stepAxis, autoScaleAxis and fixedScaleAxis return one; write your own for a scale they don't cover. It goes in the type of an axis.
Properties
readonly kindstringThe axis type, e.g.
step. The axis it creates has the samekind; the line and bar layers center onstepaxes.create(context: AxisContext) => ScaleAxisCreates the axis for a chart area and data. Runs on every layout, so after resizes too.
type LegacyAxisClass = (...args: any[]) => unknownA 1.x axis class (or a custom subclass of the 1.x Axis).
interface AxisSetup extends Omit<AxisOptions, 'type'>An axis's setup: an axis type plus the 1.x axis options (offset, low, high, ticks, showGrid, …).
Properties
low?numberThe lowest value this axis shows, instead of the data's lowest.
high?numberThe highest value this axis shows, instead of the data's highest.
referenceValue?numberA value this axis always includes, such as 0.
offset?numberRoom in px for the axis labels, between the drawing area and the chart's edge.
position?'end' | 'start'Where the labels go:
start(left or top) orend(right or bottom).labelOffset?{ x: number; y: number }Moves the labels by
xandypx from where they'd go.showLabel?booleanDraws the axis labels.
showGrid?booleanDraws the grid lines of the axis.
labelInterpolationFnc?(value: Label, index: number) => Label | null | undefinedTurns each label (and its index) into the text to show. Return
nullorundefinedto leave a label out.scaleMinSpace?numberThe minimum space in px between two grid lines of a scale: the scale takes fewer, larger steps to keep it.
onlyInteger?booleanOnly whole numbers on the scale.
stretch?booleanStep axis: spreads the ticks over the whole axis, so the last one lands on its end (line charts with
fullWidthset it).divisor?numberFixed scale axis without
ticks: the number of equal steps between its low and high, with a tick at each end.highLow?{ high: number; low: number }The value range of a scale as
{ high, low }, instead of the one computed from the data.readonly typeLegacyAxisClass | AxisFactoryA v2 axis factory, or a 1.x axis class.
interface AxisDefinition extends AxisSetupAn axis of a composed chart, in the list form of cartesianAxes: an id that layers bind to, a side, a type and the 1.x axis options.
Properties
low?numberThe lowest value this axis shows, instead of the data's lowest.
high?numberThe highest value this axis shows, instead of the data's highest.
referenceValue?numberA value this axis always includes, such as 0.
offset?numberRoom in px for the axis labels, between the drawing area and the chart's edge.
position?'end' | 'start'Where the labels go:
start(left or top) orend(right or bottom).labelOffset?{ x: number; y: number }Moves the labels by
xandypx from where they'd go.showLabel?booleanDraws the axis labels.
showGrid?booleanDraws the grid lines of the axis.
labelInterpolationFnc?(value: Label, index: number) => Label | null | undefinedTurns each label (and its index) into the text to show. Return
nullorundefinedto leave a label out.scaleMinSpace?numberThe minimum space in px between two grid lines of a scale: the scale takes fewer, larger steps to keep it.
onlyInteger?booleanOnly whole numbers on the scale.
stretch?booleanStep axis: spreads the ticks over the whole axis, so the last one lands on its end (line charts with
fullWidthset it).divisor?numberFixed scale axis without
ticks: the number of equal steps between its low and high, with a tick at each end.highLow?{ high: number; low: number }The value range of a scale as
{ high, low }, instead of the one computed from the data.readonly typeLegacyAxisClass | AxisFactoryA v2 axis factory, or a 1.x axis class.
readonly idstringNames the axis, for the
xandyof layers and theaxesofaxisGridandaxisLabels. Ids must be unique.readonly sideAxisSideWhere the axis sits:
bottomortopmakes an X axis,leftorrighta Y axis. Axes on the same side stack outward in list order.
interface AxesConfigThe config of cartesianAxes: one X and one Y axis in the 1.x form, or a list of axes, plus how the data is read.
Properties
readonly x?AxisSetupThe X axis in the 1.x form: at the bottom, or the top with
position: 'start'. Itsoffsetdefaults to 30 px.readonly y?AxisSetupThe Y axis in the 1.x form: on the left, or the right with
position: 'end'. Itsoffsetdefaults to 40 px.readonly axes?readonly AxisDefinition[]The axis list form; used instead of
x/y.readonly reverseData?booleanReverse labels and series before projecting (1.x
reverseData).readonly normalize?{ readonly multi?: true | AxisDimension; readonly distributed?: boolean }How values are normalized (1.x
normalizeData):multiistruefor{ x, y }values, or the dimension single values go to;distributedturns a flat series into one series per value.
interface PlacedScaleAxis extends ScaleAxisA created axis with its place in the chart: what the axis layers draw, and what a layer finds in axes.all.
Properties
readonly dimensionAxisDimensionWhich way the axis runs:
xory.readonly kindstringThe axis type, e.g.
step,autoScale,fixedScale.readonly stretch?booleanStep axes: whether the last tick sits at the axis end.
readonly step?numberStep axes: the distance between two ticks in px.
readonly ticksreadonly Label[]The values at which grid lines and labels are drawn: labels on a step axis, numbers on a scale.
readonly range?{ readonly min: number; readonly max: number }The values at the axis start (
min) and end (max). Step axes have none.readonly lengthnumberAxis length in px.
project(value: string | number | Date | { x: number; y: number } | { x: number } | { y: number } | undefined, index?: number, series?: NormalizedSeries) => numberPlaces a value on the axis: the distance in px from the axis start (left, or bottom for y). Step axes place by
indexinstead.readonly idstringThe axis id: from its definition, or
xandyin the 1.x form.readonly sideAxisSideThe side the axis sits on.
readonly insetnumberDistance in px from the chart area to this axis's band (stacking).
readonly optionsOmit<AxisSetup, 'type'>The axis options (offset, labels, grid), without the type.
interface ChartAxesThe axes of a laid-out chart.
Properties
readonly x?ScaleAxisThe X axis (for layers: the bound one; for the chart: the first).
readonly y?ScaleAxisThe Y axis (for layers: the bound one; for the chart: the first).
readonly all?readonly PlacedScaleAxis[]All axes of the chart, in declaration order.
interface CartesianAxesAxes for a chart's axes option, created by cartesianAxes. The pipeline asks them for the chart area and, after layout, to create the axes. Charts without axes don't bundle any of this.
Properties
readonly configAxesConfigThe config the axes were created from, with the default offsets filled in.
area(width: number, height: number, padding: ChartPadding) => { width: number; height: number; area: Area }The chart area with the axis offsets reserved.
readonly definitionsreadonly AxisDefinition[]The axis definitions, in declaration order.
bind?(layers: readonly Layer[], data: PreparedData) => LayerBinding[]Each layer's axis ids and series selection, checked against the data. Without it, every layer draws all series on the first X and Y axis.
create(data: PreparedData, area: Area, bindings?: readonly LayerBinding[], layers?: readonly Layer[]) => { axes: ChartAxes; normalized: NormalizedChartData }The axes for the final chart area, and the data as they see it. With
bindings, each axis's range comes only from the series bound to it. Withlayers, it also covers the extents they report (seeLayer.extent).
interface LabelAlignHow an SVG text label sits in its label box, as set in the align option of axisLabels.
Properties
readonly horizontal'end' | 'start' | 'middle'Aligns the text to the box's left (
start), center or right (end).readonly vertical'top' | 'bottom' | 'middle'Aligns the lines of text to the box's top, middle or bottom.