DocumentationCharts

Angular Sparkline Charts

A sparkline is a small chart with no axes, labels, or grid. It shows just the shape of the data and is sized to fit next to a number in a table row, a dashboard tile, or a sentence. With a few config settings, any Pioneer Charts line or area chart becomes an Angular sparkline.

In A Table

The most common use: one row per metric, with a sparkline showing the trend and a number showing the current value. Hover over or focus on a point to see its week and value.

Last 12 weeks

MetricTrendThis weekChange
Visitors
2,710+49%
Sign-ups
104+68%
Error rate
0.6%-67%
<table>
  @for (metric of metrics; track metric.name) {
    <tr>
      <th scope="row">{{ metric.name }}</th>
      <td><div class="spark"><pcac-line-chart [config]="metric.config" /></div></td>
      <td>{{ metric.values.at(-1) }}</td>
    </tr>
  }
</table>

/*
 * Put the chart in a fixed-width box rather than just setting a width on the cell. An auto-layout
 * table sizes each cell to its content, and the chart sizes itself to its container, so the two
 * would keep resizing each other.
 */
.spark { width: 160px; }

The Config

Five settings turn a line chart into a sparkline:

  • Hide both axes. Setting xAxis.hide and yAxis.hide removes the ticks and labels and gives their space back to the chart. An 8px margin stays on each side so a point at the edge isn't cut off.
  • Turn off the grid. Setting yAxis.showGrid: false removes the horizontal lines a line chart draws by default.
  • Remove the dots. Setting pointRadius: 0 shows only the line. The points are still there: hovering over or focusing on one shows its dot and tooltip, and it can still be clicked.
  • Fit the y axis to the data. By default, the y axis runs from 0 to 100. A series in the thousands would be pushed flat against the top, and a series of small fractions would lie flat along the bottom. Set domainMin and domainMax to the series' lowest and highest values so the line uses the full height. If the distance from zero matters, leave domainMin at 0 instead.
  • Adjust the height for the hidden axes. When both axes are hidden, the chart keeps the space their margins used, so it ends up height + 28px tall. For example, height: 12 makes a 40px sparkline.

Turning off enableEffects removes the hover crosshair, which doesn't have much room at this size. Each point still shows its tooltip on hover and focus. You don't need to set a width: the chart fills its container and redraws when the container is resized. In a table, though, put the chart in a fixed-width box instead of relying on the cell's width, because an auto-sized cell gets its width from its content.

import { PcacLineChartConfig } from '@pioneer-code/pioneer-charts';

function sparkline(name: string, values: number[]): PcacLineChartConfig {
  return {
    ...new PcacLineChartConfig(),
    height: 12,             // plus 28px with both axes hidden: a 40px chart
    pointRadius: 0,         // just the line; a point's dot shows on hover
    enableEffects: false,   // no crosshair; each point still has its tooltip
    xAxis: { hide: true },
    yAxis: {
      hide: true,
      showGrid: false,
      // Fit the line to its own range. The default is 0 to 100.
      domainMin: Math.min(...values),
      domainMax: Math.max(...values),
    },
    data: [{
      key: name, value: null, hide: false,
      data: values.map((value, i) => ({ key: `Week ${i + 1}`, value, hide: false, data: [] })),
    }],
  };
}

In A Stat Tile

The same config on an area chart, made a little taller, fills in the space under the line. It works well under a dashboard's headline number, where the fill gives the tile more visual weight.

Visitors
2,710 +49%
<div class="tile">
  <div class="label">Visitors</div>
  <div class="value">2,710 <span class="change">+49%</span></div>
  <pcac-area-chart [config]="tile" />
</div>

Tips

  • Keep it short. Every point is a keyboard stop and a pointer target. A few dozen points are easy to read and navigate at sparkline size, but a few hundred would be hard to tell apart.
  • Show the number next to it. A sparkline shows the shape, not the value. Pair it with the current number, as in the table above, so the row can be read at a glance.
  • Use the same scale to compare. Each sparkline above is scaled to its own range. That makes each trend easy to see, but two lines at the same height don't mean the same values. To compare rows directly, give every sparkline the same domainMin and domainMax.
  • Accessible by default. Screen readers announce each point by its key and value, and the arrow keys move between points, like on any chart (see Accessibility). The row's label and number give the summary.
  • Colors. A single series uses the theme palette's first color. Set colorOverride to choose another, such as green for a metric going up and red for one going down.