namespace InterpolationThe line interpolations: functions that turn a series' points into the SvgPath of its line, for the lineSmooth option of line charts. Each one is a factory: call it with its options, such as Interpolation.cardinal({ tension: 0.2 }), and pass the result. Write your own as a LineInterpolation.
Members
CardinalInterpolationOptionsThe options of
Interpolation.cardinal.cardinal(options?: CardinalInterpolationOptions): (pathCoordinates: number[], valueData: SegmentData[]) => SvgPathSmooths a line with a cardinal (Catmull-Rom) spline: round curves that go through every point. When values change quickly, the curve can swing past them;
Interpolation.monotoneCubic(the default forlineSmooth: true) doesn't.With two points or fewer, the line is straight, as with
Interpolation.none.MonotoneCubicInterpolationOptionsThe options of
Interpolation.monotoneCubic.monotoneCubic(options?: MonotoneCubicInterpolationOptions): (pathCoordinates: number[], valueData: SegmentData[]) => SvgPathSmooths a line with a monotone cubic spline: a smooth curve through every point that never swings above or below the values around it. It's what
lineSmooth: trueuses.The x values have to keep one direction, increasing or decreasing: where an x value repeats or turns back, the line breaks. With two points or fewer, the line is straight, as with
Interpolation.none.NoneInterpolationOptionsThe options of
Interpolation.none.none(options?: NoneInterpolationOptions): (pathCoordinates: number[], valueData: SegmentData[]) => SvgPathDraws a line as straight segments from point to point, without smoothing. It's what
lineSmooth: falseuses.SimpleInteractionOptionsThe options of
Interpolation.simple.simple(options?: SimpleInteractionOptions): (pathCoordinates: number[], valueData: SegmentData[]) => SvgPathSmooths a line with curves whose handles are horizontal: it goes through every point and never swings above or below them, but flows less than
Interpolation.cardinal.divisorsets how round it is.StepInterpolationOptionsThe options of
Interpolation.step.step(options?: StepInterpolationOptions): (pathCoordinates: number[], valueData: SegmentData[]) => SvgPathDraws a line in steps: horizontal and vertical segments instead of diagonals or curves. Each step adds a corner to the path, and with
showPointa point is drawn there too.
const easings: { easeInSine: number[]; easeOutSine: number[]; easeInOutSine: number[]; easeInQuad: number[]; easeOutQuad: number[]; easeInOutQuad: number[]; easeInCubic: number[]; easeOutCubic: number[]; easeInOutCubic: number[]; easeInQuart: number[]; easeOutQuart: number[]; easeInOutQuart: number[]; easeInQuint: number[]; easeOutQuint: number[]; easeInOutQuint: number[]; easeInExpo: number[]; easeOutExpo: number[]; easeInOutExpo: number[]; easeInCirc: number[]; easeOutCirc: number[]; easeInOutCirc: number[]; easeInBack: number[]; easeOutBack: number[]; easeInOutBack: number[] }The named easing curves for Svg.animate, from easeInSine to easeInOutBack: each the four numbers of a cubic Bézier curve. Name one as an animation's easing, or pass four numbers of your own, such as easing: [0.25, 0.1, 0.25, 1].
Example
import { lineChart, findAll, Svg, type ChartPlugin } from 'chartist';
// Points drop in from the top and bounce a little at the end.
const dropIn: ChartPlugin = {
name: 'drop-in',
install(chart) {
chart.on('mount', ({ scene, elementOf }) => {
for (const point of findAll(scene, { role: 'point' })) {
const element = elementOf(point);
const { y } = point.ctx;
if (element) {
new Svg(element).animate({
y1: { dur: 600, from: 0, to: y, easing: 'easeOutBack' },
y2: { dur: 600, from: 0, to: y, easing: 'easeOutBack' }
});
}
}
});
}
};
lineChart('#chart', { series: [[5, 2, 4, 3]] }, { plugins: [dropIn] });class Svgnew Svg(name: string | Element, attributes?: Attributes, className?: string, parent?: Svg, insertFirst?: boolean)Wraps an SVG element for chaining: set attributes and classes, add child elements, and animate attributes with SMIL. Wrap an element of a rendered chart (from elementOf in a mount listener), or create a new one by name.
Members
readonly Easing{ easeInSine: number[]; easeOutSine: number[]; easeInOutSine: number[]; easeInQuad: number[]; easeOutQuad: number[]; easeInOutQuad: number[]; easeInCubic: number[]; easeOutCubic: number[]; easeInOutCubic: number[]; easeInQuart: number[]; easeOutQuart: number[]; easeInOutQuart: number[]; easeInQuint: number[]; easeOutQuint: number[]; easeInOutQuint: number[]; easeInExpo: number[]; easeOutExpo: number[]; easeInOutExpo: number[]; easeInCirc: number[]; easeOutCirc: number[]; easeInOutCirc: number[]; easeInBack: number[]; easeOutBack: number[]; easeInOutBack: number[] }The easing curves for
Svg.animate, as 1.x had them on the class.attr(attributes: string, ns?: string) => string | nullSet attributes on the current SVG element of the wrapper you're currently working on.
elem(name: string, attributes?: Attributes, className?: string, insertFirst?: boolean) => SvgCreate a new SVG element whose wrapper object will be selected for further operations. This way you can also create nested groups easily.
querySelector(selector: string) => Svg | nullFind the first child SVG element of the current element that matches a CSS selector. The returned object is a Svg wrapper.
querySelectorAll(selector: string) => SvgListFinds all descendants of the element that match a CSS selector.
getNode<T extends Element = Element>() => TReturns the underlying SVG node for the current element.
foreignObject(content: string | Node, attributes?: Attributes, className?: string, insertFirst?: boolean) => SvgThis method creates a foreignObject (see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/foreignObject) that allows to embed HTML content into a SVG graphic. With the help of foreignObjects you can enable the usage of regular HTML elements inside of SVG where they are subject for SVG positioning and transformation but the Browser will use the HTML rendering capabilities for the containing DOM.
replace(newElement: Svg) => SvgThis method will replace the element with a new element that can be created outside of the current DOM.
append(element: Svg, insertFirst?: boolean) => SvgThis method will append an element to the current element as a child.
classes() => string[]Returns an array of class names that are attached to the current wrapper element. This method can not be chained further.
addClass(names: string) => SvgAdds one or a space separated list of classes to the current element and ensures the classes are only existing once.
removeClass(names: string) => SvgRemoves one or a space separated list of classes from the current element.
removeAllClasses() => SvgRemoves all classes from the current element.
height() => numberGet element height using
clientHeightwidth() => numberGet element width using
clientWidthanimate(animations: Record<string, AnimationDefinition | AnimationDefinition[]>, guided?: boolean, eventEmitter?: EventEmitter) => SvgAnimates attributes of the element with SMIL: adds an
<animate>element per attribute. EachAnimationDefinitionholds SMIL attributes (usuallybegin,dur,fromandto, see https://www.w3.org/TR/SVG11/animate.html), plus an optionaleasing: a name fromeasingsor four numbers of a cubic Bézier curve.An animations object could look like this:
ts element.animate({ opacity: { dur: 1000, from: 0, to: 1 }, x1: { dur: '1000ms', from: 100, to: 200, easing: 'easeOutQuart' }, y1: { dur: '2s', from: 0, to: 100 } });durandbegingiven as numbers are milliseconds.**Guided mode** (the default) makes one-off animations behave: the attribute is set to
fromright away, even before a delayed start; the animation starts afterbeginms (a timeout, not the document's timeline); and when it ends, the attribute keepstoand the<animate>element is removed. Passguided: falseto write the<animate>elements as given. An attribute with an array of definitions never runs guided.
Example
import { barChart, findAll, Svg, type ChartPlugin } from 'chartist';
// Bars grow from the axis, animated from a plugin's mount listener.
const growBars: ChartPlugin = {
name: 'grow-bars',
install(chart) {
chart.on('mount', ({ scene, elementOf }) => {
for (const bar of findAll(scene, { role: 'bar' })) {
const element = elementOf(bar);
if (element) {
new Svg(element).animate({
y2: {
begin: bar.ctx.valueIndex * 120,
dur: 700,
from: bar.attrs.y1,
to: bar.attrs.y2,
easing: 'easeOutQuint'
}
});
}
}
});
}
};
barChart('#chart', { series: [[5, 4, 3, 7]] }, { plugins: [growBars] });class SvgListnew SvgList(nodeList: ArrayLike<Element>)A list of Svg wrappers that calls an Svg method on all of them at once, such as addClass or animate. Each method returns the list, for chaining. Svg.querySelectorAll returns one.
Members
attr(...args: [attributes: Attributes]) => SvgListSets attributes on every element, as
Svg.attrdoes.elem(...args: [name: string, attributes?: Attributes, className?: string, insertFirst: boolean]) => SvgListAdds a child element to every element, as
Svg.elemdoes.getNode(...args: []) => SvgListCalls
Svg.getNodeon every element; returns the list, not the nodes.foreignObject(...args: [content: string | Node, attributes?: Attributes, className?: string, insertFirst: boolean]) => SvgListAdds a
foreignObjectwith the content to every element, asSvg.foreignObjectdoes.remove(...args: []) => SvgListRemoves every element from its parent, as
Svg.removedoes.addClass(...args: [names: string]) => SvgListAdds classes to every element, as
Svg.addClassdoes.removeClass(...args: [names: string]) => SvgListRemoves classes from every element, as
Svg.removeClassdoes.removeAllClasses(...args: []) => SvgListRemoves all classes from every element, as
Svg.removeAllClassesdoes.animate(...args: [animations: Record<string, AnimationDefinition | AnimationDefinition[]>, guided: boolean, eventEmitter?: EventEmitter]) => SvgListAnimates attributes of every element with SMIL, as
Svg.animatedoes.
class SvgPathnew SvgPath(close?: boolean, options?: SvgPathOptions)Builds and changes the d of an SVG path: move, line, curve and arc commands, with a cursor where new commands go. Lines, areas and slices are SvgPaths, and interpolations such as Interpolation.cardinal return one. Each command can carry the data of its point, which the chart reads back for its points.
Members
join(paths: SvgPath[], close?: boolean, options?: SvgPathOptions) => SvgPathJoins paths into a new path, in the order given. The new path gets copies of the commands, and its cursor is at the end.
pathElementsPathCommand[]The path's commands, in order: each with its letter, its coordinates and the data of its point.
position(pos: number) => thisGets or sets the current position (cursor) inside of the path. You can move around the cursor freely but limited to 0 or the count of existing elements. All modifications with element functions will insert new elements at the position of this cursor.
move(x: number, y: number, relative?: boolean, data?: SegmentData) => SvgPathUse this function to add a new move SVG path element.
line(x: number, y: number, relative?: boolean, data?: SegmentData) => SvgPathUse this function to add a new line SVG path element.
curve(x1: number, y1: number, x2: number, y2: number, x: number, y: number, relative?: boolean, data?: SegmentData) => SvgPathUse this function to add a new curve SVG path element.
arc(rx: number, ry: number, xAr: number, lAf: number, sf: number, x: number, y: number, relative?: boolean, data?: SegmentData) => SvgPathUse this function to add a new non-bezier curve SVG path element.
parse(path: string) => SvgPathParses the
dof an SVG path and inserts its commands at the cursor. A closingZat the end is dropped: thecloseof the path object decides that.stringify() => stringWrites the path as a string for the
dattribute of apathelement. Numbers are rounded to theaccuracyoption's decimals (to integers with0, not at all withfalse), and a closed path ends withZ.scale(x: number, y: number) => SvgPathScales all elements in the current SVG path object. There is an individual parameter for each coordinate. Scaling will also be done for control points of curves and the radii of arcs, affecting the given coordinate; an arc's rotation and flags stay as they are.
translate(x: number, y: number) => SvgPathTranslates all elements in the current SVG path object. The translation is relative and there is an individual parameter for each coordinate. Translation will also be done for control points of curves, affecting the given coordinate; an arc's radii, rotation and flags stay as they are.
transform(transformFnc: (cmd: PathCommand<T>, param: keyof T, cmdIndex: number, paramIndex: number, cmds: PathCommand<T>[]) => number | void) => SvgPathThis function will run over all existing path elements and then loop over their attributes. The callback function will be called for every path element attribute that exists in the current path. The method signature of the callback function looks like this:
javascript function(pathElement, paramName, pathElementIndex, paramIndex, pathElements)If something else than undefined is returned by the callback function, this value will be used to replace the old value. This allows you to build custom transformations of path objects that can't be achieved using the basic transformation functions scale and translate.clone(close?: boolean) => SvgPathThis function clones a whole path object with all its properties. This is a deep clone and path element objects will also be cloned.
splitByCommand(command: string) => SvgPath[]Splits the path into new paths, each starting at the given command, such as
'M'to get the separate pieces of a line with holes. The letter matches absolute and relative commands. The pieces get copies of the commands and the path's options, with their cursor at the end; the last piece is closed if the path is.
Example
import { SvgPath } from 'chartist';
const path = new SvgPath()
.move(0, 100)
.line(50, 20)
.curve(70, 0, 90, 0, 100, 40);
path.stringify(); // 'M0,100L50,20C70,0,90,0,100,40'
// 10px to the right, at half the height
path.translate(10, 0).scale(1, 0.5);interface BasePathParamsThe end point every path command has.
type MoveParams = BasePathParamsThe parameters of a move command (M): the point to move to.
type LineParams = BasePathParamsThe parameters of a line command (L): the point to draw to.
interface CurveParams extends BasePathParamsThe parameters of a cubic Bézier curve command (C).
interface ArcParams extends BasePathParamsThe parameters of an elliptical arc command (A), as in SVG.
Properties
xnumberThe x of the command's end point.
ynumberThe y of the command's end point.
rxnumberThe x radius of the ellipse.
rynumberThe y radius of the ellipse.
xArnumberThe rotation of the ellipse's x axis, in degrees.
lAfnumberThe large arc flag:
1takes the longer way around,0the shorter.sfnumberThe sweep flag:
1draws the arc clockwise,0counterclockwise.
type PathParams = MoveParams | LineParams | CurveParams | ArcParamsThe parameters of any path command.
type PathCommand<T extends PathParams = PathParams> = { … } & TA command of an SvgPath: its letter, its parameters and the data of its point.
Properties
commandstringThe command letter:
M,L,CorA, or from a parsed path alsoH,V,S,Q,TorZ(lowercase when relative).data?SegmentDataThe value of the chart point the command draws to, if any.
interface SvgPathOptionsThe options of an SvgPath.
Properties
accuracy?number | falseThe decimals numbers are rounded to when the path is stringified:
0rounds to integers,falsedoesn't round.
type Attributes = Record<string, number | string | undefined | null>SVG attributes by name, for Svg.attr. undefined values are skipped; names with a prefix such as ct:value use its namespace.
interface AnimationDefinitionOne SMIL animation of an attribute, for Svg.animate: the attributes of its <animate> element, plus an easing.
Properties
id?stringThe
idof the<animate>element, to start other animations relative to it.easing?number[] | 'easeInSine' | 'easeOutSine' | 'easeInOutSine' | 'easeInQuad' | 'easeOutQuad' | 'easeInOutQuad' | 'easeInCubic' | 'easeOutCubic' | 'easeInOutCubic' | 'easeInQuart' | 'easeOutQuart' | 'easeInOutQuart' | 'easeInQuint' | 'easeOutQuint' | 'easeInOutQuint' | 'easeInExpo' | 'easeOutExpo' | 'easeInOutExpo' | 'easeInCirc' | 'easeOutCirc' | 'easeInOutCirc' | 'easeInBack' | 'easeOutBack' | 'easeInOutBack'The easing: a name from
easingsor four numbers of a cubic Bézier curve. SetscalcMode,keySplinesandkeyTimesfor you.calcMode?'discrete' | 'linear' | 'paced' | 'spline'How values are interpolated (SMIL
calcMode); aneasingsets'spline'.restart?'always' | 'whenNotActive' | 'never'Whether the animation can restart while it's running (SMIL
restart).repeatCount?number | 'indefinite'How many times the animation repeats, or
'indefinite'.repeatDur?stringHow long the animation repeats in total, or
'indefinite'.keySplines?stringThe Bézier control points of a
'spline'animation (SMILkeySplines); aneasingsets them.keyTimes?stringThe times of the key values, from
0to1(SMILkeyTimes); aneasingsets them.fill?string'freeze'keeps the last value after the animation,'remove'resets it. Guided mode sets'freeze'.min?string | numberThe minimum active duration (SMIL
min).max?string | numberThe maximum active duration (SMIL
max).begin?string | numberWhen the animation starts: ms as a number, or a SMIL time such as
'1s'.end?string | numberWhen the animation ends (SMIL
end).durstring | numberHow long the animation runs: ms as a number, or a SMIL time such as
'2s'.fromstring | numberThe value the attribute starts at.
tostring | numberThe value the attribute ends at.
interface AnimationEventThe payload of the animationBegin and animationEnd events that Svg.animate emits when it gets an event emitter.
Properties
animateElementThe
<animate>element.paramsAnimationDefinitionThe animation's definition, as given.