function node<R extends keyof NodeRoles>(name: string, attributes: Record<string, AttrValue | undefined | null>, className: string, meta: RoleMeta<R>): RoleNode<R>function node(name: string, attributes?: Record<string, AttrValue | null | undefined>, className?: string, meta?: { readonly key?: string; readonly ctx?: NodeContext }): PlainNodeCreates a scene node: an SVG element with attributes and classes, and, for a node with a role, its data context. Use it in compose listeners and custom layers to add or replace elements. With a role in meta, the node is typed as that role's node and the context is checked.
Parameters
Returns
A new node without children
Example
import { lineChart, node } from 'chartist';
// Circles instead of the line chart's points.
lineChart(
'#chart',
{ series: [[1, 3, 2, 4]] },
{
plugins: [
{
name: 'circles',
install(chart) {
chart.on('compose', current => {
if (current.role !== 'point') {
return;
}
const { x, y } = current.ctx;
return node('circle', { cx: x, cy: y, r: 5 }, 'ct-circle', {
role: 'point',
ctx: current.ctx,
key: current.key
});
});
}
}
]
}
);function findAll<R extends keyof NodeRoles>(scene: SceneNode, query: NodeQuery & { readonly role: R }): RoleNode<R>[]function findAll(scene: SceneNode, query?: NodeQuery): SceneNode[]Finds all nodes of a scene that match the query, in document order, the root included. With a role, the nodes are typed as that role's nodes.
Parameters
Example
import { barChart, findAll } from 'chartist';
barChart(
'#chart',
{ labels: ['Q1', 'Q2', 'Q3', 'Q4'], series: [[5, 4, 3, 7]] },
{
plugins: [
{
name: 'log-bars',
install(chart) {
chart.on('scene', ({ scene }) => {
for (const bar of findAll(scene, { role: 'bar' })) {
console.log(bar.key, bar.ctx.value, bar.ctx.y2);
}
});
}
}
]
}
);function mapNodes(scene: SceneNode, mapper: (current: SceneNode) => void | SceneNode | null | undefined): SceneNode | nullMaps a tree bottom-up: children first, then their parent with the mapped children. Return a node to replace, null to drop, or undefined to keep. Subtrees without changes are reused, not copied.
Parameters
sceneSceneNodeThe root of the tree to map
Returns
The mapped tree, or null if the mapper dropped the root
Example
import {
autoScaleAxis,
axisGrid,
cartesianAxes,
line,
mapNodes,
renderToString,
stepAxis,
toScene
} from 'chartist';
const labels = ['Mon', 'Tue', 'Wed'];
const scene = toScene(
{
data: { labels, series: [[1, 3, 2]] },
axes: cartesianAxes([
{ id: 'x', side: 'bottom', type: stepAxis(), ticks: labels },
{ id: 'y', side: 'left', type: autoScaleAxis() }
]),
layers: [axisGrid(), line()]
},
{ width: 600, height: 300 }
);
// No grid lines, and the points marked for the stylesheet.
const mapped = mapNodes(scene, current => {
if (current.role === 'grid') {
return null;
}
if (current.role === 'point') {
return current.addClass('is-marked');
}
});
const markup = mapped ? renderToString(mapped) : '';type AttrValue = string | numberThe value of a node attribute. Numbers are written as they are, e.g. x1="12.5".
interface NodeContextData attached to a node without a role, e.g. by a plugin. Never rendered. Any fields are allowed; the ones below have a meaning to Chartist.
Properties
readonly seriesIndex?numberThe index of the series the node belongs to. With it, the node gets a
keyandfindAllcan find it by series.readonly valueIndex?numberThe index of the value in its series the node stands for.
readonly value?unknownThe value the node stands for.
readonly meta?unknownThe
metaof the value or series, as given in the data.
interface NodeMetaWhat a node means, next to how it looks: its key, role and data context. None of it is rendered.
Properties
readonly key?stringA stable id, e.g.
0/1/3(layer, series, value).readonly role?keyof NodeRolesWhat the node draws, e.g.
pointorbar: a key ofNodeRoles.readonly ctx?objectThe node's data: what its role declares, or anything for plain nodes.
interface SceneNodeFieldsThe fields every node has, whatever its role.
Properties
readonly namestringSVG element name, e.g.
g,path,line.readonly attrsReadonly<Record<string, AttrValue>>The element's attributes, without
class. Names with act:prefix (ct:value,ct:meta) are written in the Chartist namespace.readonly classesreadonly string[]The element's class names, each once, in the order they were added.
readonly childrenreadonly SceneNode[]The child nodes, in document order.
readonly content?stringText content, set with
text().readonly key?stringA stable id that survives updates. The pipeline keys data nodes that have none:
layer/series/valuefor a value's node (e.g.0/1/3),layer/seriesfor a series' node.
interface SceneNodeMethodsThe builder methods of every node. Each returns a changed copy of the same type and leaves the node it's called on as it was.
Properties
attr(attributes: Record<string, AttrValue | undefined | null>) => thisSets attributes;
undefinedornullremoves one.addClass(names: string) => thisAdds space-separated class names that aren't present yet.
removeClass(names: string) => thisRemoves space-separated class names.
append(child: SceneNode, insertFirst?: boolean) => thisAdds a child at the end, or at the start with
insertFirst.text(content: string) => thisSets the text content.
empty() => thisRemoves all children and text content.
type RoleNode<R extends NodeRole> = SceneNodeFields & SceneNodeMethods & { … }A node of a role, such as RoleNode<'point'>: its context and attributes are the ones NodeRoles declares for that role. findAll with a role returns these.
Properties
readonly namestringSVG element name, e.g.
g,path,line.readonly attrsReadonly<Record<string, AttrValue>> & NodeRoles[R]['attrs']The element's attributes, typed as the built-in layers set them;
attr()can change them.readonly classesreadonly string[]The element's class names, each once, in the order they were added.
readonly childrenreadonly SceneNode[]The child nodes, in document order.
readonly content?stringText content, set with
text().readonly key?stringA stable id that survives updates. The pipeline keys data nodes that have none:
layer/series/valuefor a value's node (e.g.0/1/3),layer/seriesfor a series' node.attr(attributes: Record<string, AttrValue | undefined | null>) => thisSets attributes;
undefinedornullremoves one.addClass(names: string) => thisAdds space-separated class names that aren't present yet.
removeClass(names: string) => thisRemoves space-separated class names.
append(child: SceneNode, insertFirst?: boolean) => thisAdds a child at the end, or at the start with
insertFirst.text(content: string) => thisSets the text content.
empty() => thisRemoves all children and text content.
readonly roleRWhat the node draws, e.g.
point.readonly ctxNodeRoles[R]['ctx'] & { readonly seriesIndex?: number; readonly valueIndex?: number }The node's data, as its role declares it.
seriesIndexandvalueIndexread asnumber | undefinedon any node, declared or not.
type PlainNode = SceneNodeFields & SceneNodeMethods & { … }A node without a role: groups, decorations, anything a plugin adds.
Properties
readonly namestringSVG element name, e.g.
g,path,line.readonly attrsReadonly<Record<string, AttrValue>>The element's attributes, without
class. Names with act:prefix (ct:value,ct:meta) are written in the Chartist namespace.readonly classesreadonly string[]The element's class names, each once, in the order they were added.
readonly childrenreadonly SceneNode[]The child nodes, in document order.
readonly content?stringText content, set with
text().readonly key?stringA stable id that survives updates. The pipeline keys data nodes that have none:
layer/series/valuefor a value's node (e.g.0/1/3),layer/seriesfor a series' node.attr(attributes: Record<string, AttrValue | undefined | null>) => thisSets attributes;
undefinedornullremoves one.addClass(names: string) => thisAdds space-separated class names that aren't present yet.
removeClass(names: string) => thisRemoves space-separated class names.
append(child: SceneNode, insertFirst?: boolean) => thisAdds a child at the end, or at the start with
insertFirst.text(content: string) => thisSets the text content.
empty() => thisRemoves all children and text content.
readonly role?undefinedAlways absent: plain nodes have no role.
readonly ctx?NodeContextData you attach to the node, if any.
type SceneNode = { [R in NodeRole]: RoleNode<R> }[NodeRole] | PlainNodeA scene node: a node of one of the roles in NodeRoles, or a plain one. Check role to narrow it, e.g. if (node.role === 'point') node.ctx.x.
Nodes are immutable: the builder methods (SceneNodeMethods) return a new node. The compose and scene phase events pass nodes to your listeners, and the renderers turn them into SVG.
type RoleMeta<R extends NodeRole> = { … } & object extends NodeRoles[R]['ctx'] ? { … } : { … }What node takes as meta for a node of role R: the role, a key, and the role's context (required when the role declares fields).
Properties
readonly roleRThe node's role, e.g.
point.readonly key?stringA stable id; the pipeline sets one for data nodes that have none.
interface PointA position in the chart's SVG, in px from its top left corner.
Properties
readonly xnumberPx from the left edge of the SVG.
readonly ynumberPx from the top edge of the SVG.
interface NoSeriesThe context of nodes that belong to no series, such as grid lines. It says so, so ctx.seriesIndex reads as number | undefined on any node.
Properties
readonly seriesIndex?undefinedAlways absent: the node belongs to no series.
readonly valueIndex?undefinedAlways absent: the node stands for no value.
interface SeriesContextWhat every node of a series knows about its series: series groups, points, lines, areas, bars and slices.
Properties
readonly seriesunknownThe series as given in the data (array, or object with
data).readonly seriesIndexnumberThe series' index in the chart's data, across layers.
readonly metaunknownThe series'
meta, if it's a series object with one.
interface SeriesGroupContext extends SeriesContextThe context of a series node: the g element that holds one series' lines, areas, points, bars or slice. It knows its series, but no value.
Properties
readonly seriesunknownThe series as given in the data (array, or object with
data).readonly seriesIndexnumberThe series' index in the chart's data, across layers.
readonly metaunknownThe series'
meta, if it's a series object with one.readonly valueIndex?undefinedAlways absent: the group stands for the whole series.
interface ValueContextWhat a node of one value knows about it: its index and its value.
Properties
readonly valueIndexnumberThe value's index in its series, from 0.
readonly valueNormalizedSeriesPrimitiveValueThe normalized value: a number,
{ x, y }numbers, or a hole.
interface PointContext extends SeriesContext, ValueContext, PointThe context of a point node: one value of a line chart's series, and where it's drawn. x and y are the point's position in px in the SVG. Line values are normalized to { x, y } (or a hole), so the value is ctx.value.y.
Properties
readonly xnumberPx from the left edge of the SVG.
readonly ynumberPx from the top edge of the SVG.
readonly seriesunknownThe series as given in the data (array, or object with
data).readonly seriesIndexnumberThe series' index in the chart's data, across layers.
readonly metaunknownThe series'
meta, if it's a series object with one.readonly valueIndexnumberThe value's index in its series, from 0.
readonly valueNormalizedSeriesPrimitiveValueThe normalized value: a number,
{ x, y }numbers, or a hole.
interface BarContext extends SeriesContext, ValueContextThe context of a bar node: one value of a bar chart's series, and the line it's drawn as, from the base (x1, y1) to the value (x2, y2), in px in the SVG. A stacked bar starts where the one below it ends. Bars are clipped to the chart area.
Properties
readonly seriesunknownThe series as given in the data (array, or object with
data).readonly seriesIndexnumberThe series' index in the chart's data, across layers.
readonly metaunknownThe series'
meta, if it's a series object with one.readonly valueIndexnumberThe value's index in its series, from 0.
readonly valueNormalizedSeriesPrimitiveValueThe normalized value: a number,
{ x, y }numbers, or a hole.readonly x1numberThe X of the bar's base, in px.
readonly y1numberThe Y of the bar's base, in px.
readonly x2numberThe X of the bar's end (the value), in px.
readonly y2numberThe Y of the bar's end (the value), in px.
interface PathContext extends SeriesContextThe context of a line or area node: the path of a whole series of a line chart, with all its values.
Properties
readonly seriesunknownThe series as given in the data (array, or object with
data).readonly seriesIndexnumberThe series' index in the chart's data, across layers.
readonly metaunknownThe series'
meta, if it's a series object with one.readonly valueIndex?undefinedAlways absent: the path stands for the whole series.
readonly valuesNormalizedSeriesAll values of the series.
readonly pathSvgPathThe path as drawn (a copy, safe to change).
interface SliceContext extends SeriesContextThe context of a slice node: a pie or donut slice, which is one series with one value, and its geometry in px and degrees.
Properties
readonly seriesunknownThe series as given in the data (array, or object with
data).readonly seriesIndexnumberThe series' index in the chart's data, across layers.
readonly metaunknownThe series'
meta, if it's a series object with one.readonly valuenumberThe slice's value.
readonly valueIndex0Always 0: a slice's series has one value.
readonly indexnumberSame as
seriesIndex, as in 1.x draw events.readonly totalDataSumnumberThe sum of all slices (or the
totaloption).readonly pathSvgPathThe slice's outline as drawn (a copy, safe to change).
readonly centerPointThe center of the pie, in px in the SVG.
readonly radiusnumberThe pie's radius in px (for a donut, to the middle of its ring).
readonly startAnglenumberWhere the slice starts, in degrees clockwise from the top.
readonly endAnglenumberWhere the slice ends, in degrees clockwise from the top.
interface GridContext extends NoSeriesThe context of a grid node: the grid line of an axis at one tick, across the chart area. Its ends are in px in the SVG.
Properties
readonly seriesIndex?undefinedAlways absent: the node belongs to no series.
readonly valueIndex?undefinedAlways absent: the node stands for no value.
readonly axisScaleAxisThe axis the line belongs to;
axis.dimensionisxory.readonly indexnumberThe tick's index.
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.
interface AxisLabelContext extends NoSeriesThe context of an axis label node: the label of an axis at one tick, and the box it's laid out in, in px in the SVG.
Properties
readonly seriesIndex?undefinedAlways absent: the node belongs to no series.
readonly valueIndex?undefinedAlways absent: the node stands for no value.
readonly kind'axis'axisfor axis labels; check it to tell them from pie labels.readonly axisScaleAxisThe axis the label belongs to;
axis.dimensionisxory.readonly indexnumberThe tick's index.
readonly textLabelThe label after
labelInterpolationFnc.readonly xnumberThe left edge of the label's box, in px.
readonly ynumberThe top edge of the label's box, in px.
readonly widthnumberThe width of the label's box in px. On the X axis, the space to the next tick.
readonly heightnumberThe height of the label's box in px. On the Y axis, the space to the next tick.
interface PieLabelContext extends SeriesContext, PointThe context of a pie label node: a slice's label on a pie or donut. x and y are where the label is anchored, in px in the SVG.
Properties
readonly xnumberPx from the left edge of the SVG.
readonly ynumberPx from the top edge of the SVG.
readonly seriesunknownThe series as given in the data (array, or object with
data).readonly seriesIndexnumberThe series' index in the chart's data, across layers.
readonly metaunknownThe series'
meta, if it's a series object with one.readonly kind'pie'piefor slice labels; check it to tell them from axis labels.readonly valueIndex0Always 0, as for the slice.
readonly indexnumberSame as
seriesIndex, as in 1.x draw events.readonly textstringThe label as drawn.
interface LineAttrsThe attributes of a line element: points, bars and grid lines. A point is a line of 0.01 px, which the CSS draws as a dot with a round cap.
Properties
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 ct:value?string | numberThe value as written to the output, for points and bars.
readonly ct:meta?stringThe value's
meta, serialized, for points and bars that have one.
interface PathAttrsThe attributes of a path element: lines, areas and slices.
Properties
readonly dstringThe path data, as in SVG's
dattribute.readonly ct:value?string | numberThe value as written to the output, for slices.
readonly ct:meta?stringThe series'
meta, serialized, for slices that have one.
interface SeriesAttrsThe attributes of a series' g element.
Properties
readonly ct:series-name?stringThe series'
name, if it has one.readonly ct:meta?stringThe series'
meta, serialized, if it has one (line and bar charts).
interface RectAttrsThe attributes of a rect element: the grid background.
Properties
readonly xnumberThe left edge, in px.
readonly ynumberThe top edge, in px.
readonly widthnumberThe width in px.
readonly heightnumberThe height in px.
interface NodeRolesThe roles of scene nodes, and for each its data context (ctx) and attributes (attrs). Checking node.role narrows a SceneNode to its role, so ctx has that role's fields.
The built-in layers and axes use the roles below; the class names are their defaults. A plugin that adds nodes with a role of its own declares it here with TypeScript's module augmentation; node and findAll then accept it, and its compose listeners see it.
Properties
point{ ctx: PointContext; attrs: LineAttrs }A value of a line chart: a
lineof 0.01 px (classct-point).bar{ ctx: BarContext; attrs: LineAttrs }A value of a bar chart: a
line(classct-bar).line{ ctx: PathContext; attrs: PathAttrs }A series' line in a line chart: a
path(classct-line).area{ ctx: PathContext; attrs: PathAttrs }A series' filled area in a line chart: a
path(classct-area).slice{ ctx: SliceContext; attrs: PathAttrs }A pie or donut slice: a
path(classct-slice-pieorct-slice-donut).series{ ctx: SeriesGroupContext; attrs: SeriesAttrs }The
gelement that holds a series' nodes (classct-series).grid{ ctx: GridContext; attrs: LineAttrs }A grid line of an axis at a tick: a
line(classct-grid).gridBackground{ ctx: NoSeries; attrs: RectAttrs }The
rectbehind the chart area (classct-grid-background).label{ ctx: AxisLabelContext | PieLabelContext; attrs: object }An axis label (a
foreignObjectwith aspan, or SVGtext) or a pie label (SVGtext). Checkctx.kindto tell them apart.
Example
import { node, type ChartPlugin } from 'chartist';
declare module 'chartist' {
interface NodeRoles {
marker: {
ctx: { readonly seriesIndex: number; readonly label: string };
attrs: { readonly cx: number; readonly cy: number };
};
}
}
// A marker on the last point of each series.
export const markers: ChartPlugin = {
name: 'markers',
install(chart) {
chart.on('compose', current => {
if (current.role !== 'series') {
return;
}
const last = current.children.at(-1);
if (last?.role !== 'point') {
return;
}
const { x, y, seriesIndex } = last.ctx;
return current.append(
node('circle', { cx: x, cy: y, r: 6 }, 'ct-marker', {
role: 'marker',
ctx: { seriesIndex, label: 'last' }
})
);
});
}
};type NodeRole = keyof NodeRolesA role name, such as point or bar: a key of NodeRoles.
interface NodeQueryWhat findAll looks for: nodes by role, key and data context fields. A node matches when it matches every field given; an empty query matches every node.
Properties
readonly role?keyof NodeRolesOnly nodes of this role, e.g.
bar.readonly key?stringOnly the node with this key, e.g.
0/1/3(layer, series, value).readonly seriesIndex?numberOnly nodes of the series with this index (
ctx.seriesIndex).readonly valueIndex?numberOnly nodes of the value with this index (
ctx.valueIndex).