Chartist

Search

Find a guide, an API entry or an example.

Try

SVG Path animation

Lines and areas of three series grow from the bottom, one series after the other.

src/main.ts
import 'chartist/styles.css';
import { lineChart, easings, findAll, Svg, type ChartPlugin } from 'chartist';

// Lines and areas grow from the bottom with SMIL, one series after the other.
const growPaths: ChartPlugin = {
  name: 'grow-paths',
  install(chart) {
    let height = 0;
    chart.on('layout', ({ area }) => {
      height = area.y1 - area.y2;
    });
    chart.on('mount', ({ scene, elementOf }) => {
      const shapes = [
        ...findAll(scene, { role: 'line' }),
        ...findAll(scene, { role: 'area' })
      ];
      for (const shape of shapes) {
        const element = elementOf(shape);
        if (!element) {
          continue;
        }
        const path = shape.ctx.path;
        new Svg(element).animate({
          d: {
            begin: 2000 * shape.ctx.seriesIndex,
            dur: 2000,
            from: path.clone().scale(1, 0).translate(0, height).stringify(),
            to: path.clone().stringify(),
            easing: easings.easeOutQuint
          }
        });
      }
    });
  }
};

lineChart(
  '#chart',
  {
    labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
    series: [
      [1, 5, 2, 5, 4, 3],
      [2, 3, 4, 8, 1, 2],
      [5, 4, 3, 2, 1, 0.5]
    ]
  },
  {
    plugins: [growPaths],
    low: 0,
    showArea: true,
    showPoint: false,
    fullWidth: true
  }
);
Live
Open in the playgroundView as MarkdownSwitch to 1.x above the code to see the same chart written with the Chartist 1.x classes.