function chart(target: string | Element, config: ChartConfig): ChartHandleCreates a chart from data, axes and layers in a container, and renders it right away. The presets (lineChart, barChart, pieChart) are built on it; use it for what they don't cover, like bars and a line on two Y axes.
The chart fills its container unless the config sets a size, and renders again when the container resizes, web fonts finish loading or the matching responsive entries change. Listeners in config.on and plugins receive the events of the first render too. While the page is printed, the SVG gets a viewBox of its layout size (unless the config sets one), so the drawing scales into the box it gets in the print.
Without className, the SVG gets the chart type class of its layers, so the stylesheet's chart type rules apply: ct-chart-bar if a layer draws bars, else ct-chart-pie or ct-chart-donut for a pie layer, else ct-chart-line. A className replaces it.
Parameters
targetstring | ElementThe container element, or a CSS selector for it.
configChartConfigThe data, axes, layers and the rest of the chart.
Returns
A handle to update the chart, listen to it or remove it.
Example
import {
autoScaleAxis,
axisGrid,
axisLabels,
bar,
cartesianAxes,
chart,
line,
stepAxis
} from 'chartist';
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
chart('#chart', {
data: {
labels: months,
series: [
{ name: 'rain', data: [64, 58, 70, 82, 104, 120] },
{ name: 'temperature', data: [1, 3, 8, 12, 16, 20] }
]
},
axes: cartesianAxes([
{ id: 'month', side: 'bottom', type: stepAxis(), ticks: months },
{ id: 'mm', side: 'left', type: autoScaleAxis(), low: 0 },
{ id: 'celsius', side: 'right', type: autoScaleAxis(), low: 0 }
]),
layers: [
axisGrid({ axes: ['month', 'mm'] }),
bar({ series: ['rain'], x: 'month', y: 'mm' }),
line({ series: ['temperature'], y: 'celsius', align: 'center' }),
axisLabels()
]
});interface ChartConfig extends PipelineOptionsThe config of chart: the data, the layers that draw it and the axes they draw on, plus size, padding, plugins and listeners.
Without axes, layers draw into the whole chart area; the pie layer needs coordinates: 'polar'. toScene takes the same config.
Properties
readonly plugins?readonly PluginEntry[]Plugins to install before the first render, in order: plugins, or plugins with their options from
withOptions.readonly on?PhaseListenersPhase listeners by phase name (or
'*'for all phases). They're registered before the first render, so they see it too.readonly renderer?Renderer<RenderOutput>Turns the scene into output; the DOM renderer by default.
readonly responsive?ResponsiveSet<Partial<Omit<ChartConfig, 'plugins' | 'responsive' | 'on' | 'renderer'>>>Overrides that apply while their condition matches, created with
responsive([...]): entries of a media query string or acontainer()condition, and a partial config. The chart renders again when the matching entries change.readonly dataChartDataThe labels and series to draw.
readonly layersreadonly Layer[]What to draw, in this order (later layers on top), e.g.
[axisGrid(), line()].readonly coordinates?CoordinatesThe coordinate system the layers draw in:
cartesian(the default) for axes,polarforpie().readonly padding?number | Partial<ChartPadding>Space in px between the chart's edge and the drawing area: one number for all sides, or some sides (the others keep the 1.x defaults).
readonly axes?CartesianAxesAxes (see
cartesianAxes); their offsets are reserved around the area.readonly width?string | numberThe chart's width: px as a number, or a CSS length such as
100%or20rem. Without it,chart()fills the container's width.readonly height?string | numberThe chart's height: px as a number, or a CSS length such as
300px. Without it,chart()fills the container's height.readonly viewBox?{ readonly width: number; readonly height: number }Fixed drawing coordinates; sets the layout size and the root
viewBox.readonly className?stringClass names for the root svg, e.g.
ct-chart-linefor the 1.x line chart CSS.readonly labels?LabelModeDraws labels as HTML in
foreignObject(1.x) or as SVGtext. The default depends on the renderer:htmlfor the DOM,svgintoScene().readonly preset?LinePresetState | BarPresetState | PiePresetStateThe preset that created the chart, if any (read by plugins).
type ChartOverrides = Partial<Omit<ChartConfig, 'responsive' | 'plugins' | 'on' | 'renderer'>>The part of a ChartConfig that responsive entries can override: everything but responsive, plugins, on and renderer. The matching entries merge over the config in order, shallowly.
Properties
readonly data?ChartDataThe labels and series to draw.
readonly layers?readonly Layer[]What to draw, in this order (later layers on top), e.g.
[axisGrid(), line()].readonly coordinates?CoordinatesThe coordinate system the layers draw in:
cartesian(the default) for axes,polarforpie().readonly padding?number | Partial<ChartPadding>Space in px between the chart's edge and the drawing area: one number for all sides, or some sides (the others keep the 1.x defaults).
readonly axes?CartesianAxesAxes (see
cartesianAxes); their offsets are reserved around the area.readonly width?string | numberThe chart's width: px as a number, or a CSS length such as
100%or20rem. Without it,chart()fills the container's width.readonly height?string | numberThe chart's height: px as a number, or a CSS length such as
300px. Without it,chart()fills the container's height.readonly viewBox?{ readonly width: number; readonly height: number }Fixed drawing coordinates; sets the layout size and the root
viewBox.readonly className?stringClass names for the root svg, e.g.
ct-chart-linefor the 1.x line chart CSS.readonly labels?LabelModeDraws labels as HTML in
foreignObject(1.x) or as SVGtext. The default depends on the renderer:htmlfor the DOM,svgintoScene().readonly preset?LinePresetState | BarPresetState | PiePresetStateThe preset that created the chart, if any (read by plugins).
interface ChartHandleThe handle chart returns: update or remove the chart, and listen to its events. Plugins get the same handle in install. The methods other than destroy and emit return the handle, so calls chain.
Properties
readonly container?ElementThe element the chart renders into (undefined in
toScene).update(config: Partial<Omit<ChartConfig, 'plugins' | 'on'>>) => thisShallow-merges the changes into the config and renders again. Plugins and listeners stay;
pluginsandoncan't change.detach() => thisStops reacting to container size changes (and media queries in presets). The output and the listeners stay.
destroy() => voidDetaches, removes the output and clears all listeners.
emit<K extends CustomEventName>(name: K, payload: ChartEvents[K]) => voidEmits a custom event (declared in
ChartEvents), e.g. from a plugin. Phase names are reserved: emitting one throws.on<K extends keyof PhaseEvents>(name: K, listener: PhaseListener<K>) => thisListens to a phase event, which fires on every render. Listeners of
resolvetoscenemay return a new payload to change what's drawn;composelisteners may returnnullto drop a node.off<K extends keyof PhaseEvents>(name: K, listener?: PhaseListener<K>) => thisRemoves a phase listener, whoever added it. Without a listener, removes the listeners of the phase added through
onand the config'son; plugins keep theirs.