Chartist

Search

Find a guide, an API entry or an example.

Try

Scales and axes

Axes that turn values into positions: step, auto-scaled and fixed, placed on any side.

cartesianAxes

experimentalSource
function cartesianAxes(input: readonly AxisDefinition[] | AxesConfig, extra?: Pick<AxesConfig, 'reverseData' | 'normalize'>): CartesianAxes

Creates 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

input readonly AxisDefinition[] | AxesConfig

The axes: a list of AxisDefinitions, or the 1.x { x, y } form.

extra? Pick<AxesConfig, 'reverseData' | 'normalize'> = {}

reverseData and normalize, 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'
});

stepAxis

experimentalSource
function stepAxis(): AxisFactory

Creates 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()]
});

autoScaleAxis

experimentalSource
function autoScaleAxis(): AxisFactory

Creates 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()]
});

fixedScaleAxis

experimentalSource
function fixedScaleAxis(): AxisFactory

Creates 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()]
});

AxisDimension

Source
type AxisDimension = 'x' | 'y'

Which way an axis runs: x (horizontal) or y (vertical).

AxisSide

Source
type AxisSide = 'top' | 'bottom' | 'left' | 'right'

Where an axis sits: top/bottom are X axes, left/right Y axes.

AxisContext

Source
interface AxisContext

What an AxisFactory gets to create an axis: its direction, the data, the chart area and the axis options.

Properties

readonly dimension AxisDimension

Which way the axis runs: x or y, from its side.

readonly series NormalizedSeries[]

Normalized multi-value series ({ x, y } values): the series bound to the axis, or all of them.

readonly labels readonly Label[]

Normalized labels, padded to the longest series.

readonly area Area

The chart area in px, with the offsets of all axes reserved.

readonly options AxisOptions

The options of the axis definition, without id, side and type.

readonly extent? AxisExtent

The 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 own low and high still win.

AxisExtent

experimentalSource
interface AxisExtent

A range of values on an axis, from low to high: what a layer reports in Layer.extent.

Properties

readonly low number

The lowest value.

readonly high number

The highest value.

ScaleAxis

experimentalSource
interface ScaleAxis

A 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 dimension AxisDimension

Which way the axis runs: x or y.

readonly kind string

The axis type, e.g. step, autoScale, fixedScale.

readonly stretch? boolean

Step axes: whether the last tick sits at the axis end.

readonly step? number

Step axes: the distance between two ticks in px.

readonly ticks readonly 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 length number

Axis length in px.

project (value: string | number | Date | { x: number; y: number } | { x: number } | { y: number } | undefined, index?: number, series?: NormalizedSeries) => number

Places a value on the axis: the distance in px from the axis start (left, or bottom for y). Step axes place by index instead.

AxisFactory

experimentalSource
interface AxisFactory

An 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 kind string

The axis type, e.g. step. The axis it creates has the same kind; the line and bar layers center on step axes.

create (context: AxisContext) => ScaleAxis

Creates the axis for a chart area and data. Runs on every layout, so after resizes too.

LegacyAxisClass

Source
type LegacyAxisClass = (...args: any[]) => unknown

A 1.x axis class (or a custom subclass of the 1.x Axis).

AxisSetup

Source
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? number

The lowest value this axis shows, instead of the data's lowest.

high? number

The highest value this axis shows, instead of the data's highest.

referenceValue? number

A value this axis always includes, such as 0.

offset? number

Room 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) or end (right or bottom).

labelOffset? { x: number; y: number }

Moves the labels by x and y px from where they'd go.

showLabel? boolean

Draws the axis labels.

showGrid? boolean

Draws the grid lines of the axis.

labelInterpolationFnc? (value: Label, index: number) => Label | null | undefined

Turns each label (and its index) into the text to show. Return null or undefined to leave a label out.

scaleMinSpace? number

The minimum space in px between two grid lines of a scale: the scale takes fewer, larger steps to keep it.

onlyInteger? boolean

Only whole numbers on the scale.

ticks? Label[]

The ticks: the labels of a step axis, or the values of a fixed scale axis.

stretch? boolean

Step axis: spreads the ticks over the whole axis, so the last one lands on its end (line charts with fullWidth set it).

divisor? number

Fixed 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 type LegacyAxisClass | AxisFactory

A v2 axis factory, or a 1.x axis class.

AxisDefinition

experimentalSource
interface AxisDefinition extends AxisSetup

An 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? number

The lowest value this axis shows, instead of the data's lowest.

high? number

The highest value this axis shows, instead of the data's highest.

referenceValue? number

A value this axis always includes, such as 0.

offset? number

Room 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) or end (right or bottom).

labelOffset? { x: number; y: number }

Moves the labels by x and y px from where they'd go.

showLabel? boolean

Draws the axis labels.

showGrid? boolean

Draws the grid lines of the axis.

labelInterpolationFnc? (value: Label, index: number) => Label | null | undefined

Turns each label (and its index) into the text to show. Return null or undefined to leave a label out.

scaleMinSpace? number

The minimum space in px between two grid lines of a scale: the scale takes fewer, larger steps to keep it.

onlyInteger? boolean

Only whole numbers on the scale.

ticks? Label[]

The ticks: the labels of a step axis, or the values of a fixed scale axis.

stretch? boolean

Step axis: spreads the ticks over the whole axis, so the last one lands on its end (line charts with fullWidth set it).

divisor? number

Fixed 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 type LegacyAxisClass | AxisFactory

A v2 axis factory, or a 1.x axis class.

readonly id string

Names the axis, for the x and y of layers and the axes of axisGrid and axisLabels. Ids must be unique.

readonly side AxisSide

Where the axis sits: bottom or top makes an X axis, left or right a Y axis. Axes on the same side stack outward in list order.

AxesConfig

Source
interface AxesConfig

The 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? AxisSetup

The X axis in the 1.x form: at the bottom, or the top with position: 'start'. Its offset defaults to 30 px.

readonly y? AxisSetup

The Y axis in the 1.x form: on the left, or the right with position: 'end'. Its offset defaults to 40 px.

readonly axes? readonly AxisDefinition[]

The axis list form; used instead of x/y.

readonly reverseData? boolean

Reverse labels and series before projecting (1.x reverseData).

readonly normalize? { readonly multi?: true | AxisDimension; readonly distributed?: boolean }

How values are normalized (1.x normalizeData): multi is true for { x, y } values, or the dimension single values go to; distributed turns a flat series into one series per value.

PlacedScaleAxis

Source
interface PlacedScaleAxis extends ScaleAxis

A created axis with its place in the chart: what the axis layers draw, and what a layer finds in axes.all.

Properties

readonly dimension AxisDimension

Which way the axis runs: x or y.

readonly kind string

The axis type, e.g. step, autoScale, fixedScale.

readonly stretch? boolean

Step axes: whether the last tick sits at the axis end.

readonly step? number

Step axes: the distance between two ticks in px.

readonly ticks readonly 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 length number

Axis length in px.

project (value: string | number | Date | { x: number; y: number } | { x: number } | { y: number } | undefined, index?: number, series?: NormalizedSeries) => number

Places a value on the axis: the distance in px from the axis start (left, or bottom for y). Step axes place by index instead.

readonly id string

The axis id: from its definition, or x and y in the 1.x form.

readonly side AxisSide

The side the axis sits on.

readonly inset number

Distance in px from the chart area to this axis's band (stacking).

readonly options Omit<AxisSetup, 'type'>

The axis options (offset, labels, grid), without the type.

ChartAxes

Source
interface ChartAxes

The axes of a laid-out chart.

Properties

readonly x? ScaleAxis

The X axis (for layers: the bound one; for the chart: the first).

readonly y? ScaleAxis

The Y axis (for layers: the bound one; for the chart: the first).

readonly all? readonly PlacedScaleAxis[]

All axes of the chart, in declaration order.

CartesianAxes

experimentalSource
interface CartesianAxes

Axes 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 config AxesConfig

The 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 definitions readonly 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. With layers, it also covers the extents they report (see Layer.extent).

LabelAlign

experimentalSource
interface LabelAlign

How 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.