v22.18.0 release notes

Sparkline

Sparkline is a word-sized chart in pure SVG for table cells, cards and KPIs: a line, an area, bars with negative values, wins and losses or a bullet chart, with markers, a reference line, a tooltip, keyboard navigation and a text summary for screen readers. It is rendered on the server and has no dependency.

View as .md
import { Sparkline } from '@exhaustive-ui/core/sparkline';

Sparkline draws its data, an array of numbers, as a small chart the size of a word, in pure SVG. It is 6em wide and 1.5em high by default, so it follows the font size of the text around it; width and height take any CSS length or a number of pixels, and the chart stretches to any width, 100% included. It is rendered on the server.

Weekly downloads 2,410 this week.

Larger text grows with it.

type picks the chart. line is the default and area fills below the line, down to zero when the values cross it. bar draws a bar per value from the zero line, the negative values below it in their own color. winloss only shows the sign of each value, a bar up for a win and down for a loss, whatever its size. bullet compares the last value, the bar, to a target given by referenceLine, on qualitative bands set by ranges.

The scale runs from the smallest to the largest value, from 0 for bars and bullet charts; min and max fix it, for example to compare the rows of a table on the same scale. curve set to smooth draws a monotone curve, that never goes above or below the values. A null value leaves a gap in the line.

  • Line
  • Smooth line with a gap
  • Area
  • Bar
  • Win-loss
  • Bullet

In a table, a sparkline shows a trend per row next to the figures. It needs no observer, no canvas and no timer: each one is a few SVG paths computed once per data change, and listens to the pointer only while the tooltip is on. The table below has 100 rows, one line and one bar chart each, and min and max are left to scale every row on its own.

SymbolPriceLast 30 daysDaily change
EX00151.73
EX00237.86
EX00329.99
EX00446.12
EX00544.25
EX00630.39
EX00770.52
EX00832.65
EX00948.78
EX01046.91

In a KPI card, a full width area chart gives the figure its history. showMarkers set to last points at the current value and the tooltip shows any other one on hover.

  • Revenue$48,200+12% this month
  • Active users12,940+4% this month
  • Churn2.1%-0.6 points this month

showMarkers puts a dot on some points of a line or an area: last, minmax for the lowest and the highest values, in their own colors, or all. The dots keep their size and stay round whatever the width and the height of the chart.

referenceLine draws a dashed line at a value, a goal for example, or at a statistic of the data: average, median, min or max. The scale includes it, so it stays in view. In a bullet chart it is the target.

  • Last value
  • Lowest and highest
  • All points
  • Average
  • Goal of 20

The chart follows the theme: the line, the area and the positive bars use the sparkline.color design token, the primary color by default, and the negative bars and the losses sparkline.negative.color. color and negativeColor override them on one chart with any CSS color, design token variables included. strokeWidth sets the width of the line in pixels.

  • Theme colors
  • Custom colors
  • Thick green line

With tooltip, hovering or touching the chart shows the value of the nearest point, with its label when data holds { value, label } objects. valueFormatter formats the values, here as currency, and a template named tooltip replaces the content.

With focusable, the chart is in the tab order: it shows the last point when it gets the focus, and the left and right arrow keys, Home and End move between the points, announced to screen readers. onPointChange emits the active point, here to show it in the heading.

Monthly revenue: move over the chart

Screen Reader

The SVG has the img role and a text summary of the data as accessible name, for example "12 values from 3 to 18, last 15, up 25% from first", "12 results, 8 wins, 4 losses" or "value 68, target 80". ariaLabel names the chart and comes first: "Revenue: 12 values from 3 to 18...". ariaLabelledBy refers to a visible title instead, and the summary becomes the description. summaryFormatter writes another summary from the statistics of the data, and every text can be translated through labels; locale or valueFormatter format the numbers.

With focusable, the chart is in the tab order and lists its keys in aria-keyshortcuts. Each point reached with the keyboard is announced by a polite live region, with its label, its value and its position, "Mar: 15, 3 of 12". The tooltip is hidden from assistive technologies, which get the same text from the live region. Without focusable, the chart is a plain image with its summary and adds no live region.

The lines, bars and markers keep a 3:1 contrast with the background in the light and dark color schemes. Colors are never the only cue: negative bars are also below the zero line, losses below the middle.

<p-sparkline [data]="revenue" ariaLabel="Revenue" />

<h3 id="visits-title">Visits</h3>
<p-sparkline [data]="visits" ariaLabelledBy="visits-title" [focusable]="true" [tooltip]="true" />

<p-sparkline [data]="sales" [labels]="{ range: '{count} valeurs de {min} à {max}', last: 'dernière {last}', up: 'en hausse de {change}', down: 'en baisse de {change}' }" locale="fr-FR" />

Keyboard Support

The chart is focusable only with focusable. A bullet chart shows its value and target on focus and has no points to move between.

KeyFunction
tabMoves focus to the chart and activates its last point.
right arrowMoves to the next point, skipping the gaps.
left arrowMoves to the previous point.
homeMoves to the first point.
endMoves to the last point.
escapeHides the tooltip and the active point.