function smilAnimations(): LiveAnimationsAnimates the changes of an update with SMIL, timed by the chart's CSS: each updated node's changed numbers and paths move from their previous value to their new one over --ct-update-duration (a CSS time, 300 ms by default; 0s turns the animations off) with the easing in --ct-update-easing (a CSS easing, ease-out by default). The attributes hold their new values from the start, and an update during an animation continues from where each element is drawn.
It animates updates (update(), setOptions()) and changes of the matching responsive options, not resizes, and nothing while the visitor's system asks for reduced motion. Lines and areas follow their values' keys, and pie slices turn around the pie; layers and plugins can give keyframes of their own.
Returns
The animations, for liveDomRenderer({ animations })
Example
import { lineChart } from 'chartist';
import { liveDomRenderer, smilAnimations } from 'chartist/live';
// 300 ms with ease-out, unless the CSS sets --ct-update-duration
// and --ct-update-easing.
const chart = lineChart(
'#chart',
{ labels: ['Mon', 'Tue', 'Wed'], series: [[5, 2, 4]] },
{ renderer: liveDomRenderer({ animations: smilAnimations() }) }
);
chart.update({ data: { labels: ['Mon', 'Tue', 'Wed'], series: [[3, 6, 1]] } });function diffScenes(previous: SceneNode, next: SceneNode, options?: DiffOptions): SceneChange[]The changes between two scenes, matched by what each node stands for: add, update (with the previous node) and remove, in the new scene's document order, with the remove entries after their former parent's kept children. Every node of an added or removed subtree has its own entry. The live renderer patches its SVG with them; other renderers that change their output in place can use them too.
Nodes are matched among the children of matched parents: values by their layer, series and key (see key), series by layer and index, grid lines and axis labels by axis and tick, nodes with their own key by it, and the rest by element name and first class, in order.
Parameters
previousSceneNodeThe previous scene
nextSceneNodeThe new scene
options?DiffOptions={}How values are identified, and the data's labels
Returns
The changes, in the new scene's document order
Example
import { lineChartConfig, toScene } from 'chartist';
import { diffScenes } from 'chartist/live';
const size = { width: 600, height: 300 };
const labels = ['Mo', 'Tu', 'We'];
const before = toScene(lineChartConfig({ labels, series: [[1, 2, 3]] }), size);
const after = toScene(lineChartConfig({ labels, series: [[1, 5, 3]] }), size);
for (const { op, node } of diffScenes(before, after, { labels })) {
console.log(op, node.role, node.key);
}function liveDomRenderer(options?: LiveDomRendererOptions): Renderer<DomRenderOutput>Creates a DOM renderer that keeps a chart's SVG and changes it in place on every later render, instead of drawing a new one: elements that stand for the same thing as before stay, with only their changed attributes, classes and text set, elements for new nodes are created and elements of nodes that are gone are removed. What listeners added to kept elements stays, and mount gets the changes of the render.
Its first render, and every render without the previous one (1.x charts), draws a new SVG as domRenderer does. A chart whose container holds markup from renderForHydration() takes that markup over instead, keeping the server's elements, unless adopt is false. The renderer keeps no state of its own, so one instance can serve many charts.
Parameters
options?LiveDomRendererOptions={}How values are identified (
key), update animations, such assmilAnimations(), and whether to take server markup over
Returns
The renderer, for renderer in chart() and the presets
Example
import { lineChart } from 'chartist';
import { liveDomRenderer } from 'chartist/live';
const labels = ['Mo', 'Tu', 'We', 'Th', 'Fr'];
const values = [3, 5, 4, 6, 5];
const chart = lineChart(
'#chart',
{ labels, series: [values] },
// Points and labels follow their day as the window slides.
{ renderer: liveDomRenderer({ key: 'label' }) }
);
let day = 0;
setInterval(() => {
labels.push(`Day ${++day}`);
labels.shift();
values.push(Math.round(Math.random() * 10));
values.shift();
chart.update({ labels: [...labels], series: [[...values]] });
}, 1000);interface ValueKeyContextWhat ValueKey functions get about a value: where it is, the value itself, the label at its position and its meta.
Properties
readonly seriesIndexnumberThe series' index in the chart's data.
readonly valueIndexnumberThe value's index in its series (for pie data, 0).
readonly valueunknownThe value, as the layer drew it.
readonly labelunknownThe label at the value's position:
labels[valueIndex], or the slice's label in pie data.readonly metaunknownThe value's meta, if any.
type ValueKey = 'index' | 'label' | ((value: ValueKeyContext) => string | number)What identifies a value across updates: 'index' (the default) its position in its series, 'label' the label at its position, or a function of the value, such as ({ meta }) => meta.id.
interface DiffOptionsThe options of diffScenes: how values are identified, and the labels for key: 'label'.
Properties
readonly key?ValueKeyWhat identifies a value across updates;
'index'by default.readonly labels?readonly unknown[]The new scene's labels, for
key: 'label'.readonly previousLabels?readonly unknown[]The previous scene's labels, for
key: 'label';labelsif omitted.
interface LiveAnimationContextWhat the live renderer hands its LiveAnimations after patching the SVG of an update.
Properties
readonly changesreadonly SceneChange[]The changes the render made, in document order.
readonly elementOf(node: SceneNode) => Element | undefinedThe element of a node of the new scene.
readonly updateRenderUpdateThe previous render, with the cause and the keyframes lookup.
readonly containerElementThe chart's container.
readonly sceneSceneNodeThe new scene.
readonly measureStyle?MeasureStyleReads computed styles in the container, with the chart's root classes.
readonly valueKey(context: Omit<ValueKeyContext, 'label'>, scene: 'previous' | 'next') => stringThe key that identifies a value across updates, as the renderer's
keyoption says: for the value atvalueIndexof seriesseriesIndex, with its value and meta, in the previous scene or the new one (labels differ between them).
interface LiveAnimationsUpdate animations for the live renderer, such as smilAnimations: called after the SVG of an update was patched, it starts the animations and returns their timing, or nothing when the render doesn't animate.
Properties
animate(context: LiveAnimationContext) => UpdateAnimation | undefinedStarts the update animations of a render.
interface LiveDomRendererOptionsThe options of liveDomRenderer.
Properties
readonly key?ValueKeyWhat identifies a value across updates;
'index'by default.readonly animations?LiveAnimationsUpdate animations, such as
smilAnimations(); none by default.readonly adopt?booleanTakes over markup from
renderForHydration()in the chart's container and keeps its elements;trueby default. Withfalse, the chart draws a new svg over the server markup.