Animated HTML Charts: Bar, Line, Pie and Sparkline With Springs
Version 1.1 of motion-components adds a second new category next to Icons: Charts. It has three elements.
All three follow one idea: the data is HTML, and every change in the data is a spring. You do not call a draw function. You write the numbers into an attribute, and when the numbers change, the chart moves to them.
A chart in one line
This is a complete page. Save it as
The bars grow one after another when the chart scrolls into view. Hover a bar to see its value.
Three ways to give a chart its data
| Way | Use it when | Example |
|---|---|---|
| Attributes | You have a few numbers. | |
| A table | Your data is already a table, or a server or CMS renders it. | |
| The | The data comes from JavaScript, an API or a framework. |
The table is the most interesting one. Wrap any HTML table, and the chart reads it. The first column holds the labels, and every other column is one series:
The table stays in the page for screen readers. If your code changes a cell, the chart updates by itself.
Every change is a spring
Most chart libraries redraw or fade when the data changes. Here, each bar and each point springs from where it is to its new value:
If the data changes again in the middle of an animation, the chart does not jump to the end first. It turns around from its current position. This makes live data look calm, even when it updates every second.
| Moment | What moves |
|---|---|
| Entrance | Bars grow one after another, lines draw in, pies sweep in clockwise. |
| New data | Bars, points and slices spring to their new values. |
| Hover | The tooltip springs from point to point. A hovered pie slice springs outward. |
Bar and line charts
Pie and donut charts
A pie works best with a few slices. When there are more than six, the extra slices are combined into one slice called “Other”. To compare values that are close together, a bar chart is easier to read.
Sparklines
Put it next to
Colors
The eight series colors are chosen to stay apart for readers with color blindness, in light mode and in dark mode. Each mode has its own set of colors, not an automatic inversion. Override them with
Text, axes and gridlines use the text color, so the chart fits any theme.
Accessibility
| What | How |
|---|---|
| Screen readers | They read the data as a table: your own table, or one the chart generates. |
| Keyboard | Press Tab to focus the chart, then the arrow keys to move between points. Each point is read out. |
| Name | Give every chart a |
| Reduced motion | The chart shows its final state at once and changes values without animation. |
Recipes
The Chart Recipes page has six patterns you can copy, each with a live demo:
- A stat tile with a number and a trend.
- A week, month and year switcher.
- A chart that follows a form.
- Data loaded from a JSON file.
- A budget donut with a month select.
- A live line chart.
Get started
Read the motion-chart, motion-pie and motion-sparkline docs. Every code example on those pages works when you paste it into a page.