Generate Sparklines With Sparkle MacSparkline
Published on Nov 23, 2026, filed under tools, development, design. (Share this post, e.g., on Mastodon.)
After using a hand-rolled script for sparklines on meiert.com (example), I wanted to bring them to dxsrc as well (example—on larger viewport sizes). To do that cleanly, I built a helper: Sparkle MacSparkline (source).
Sparkle MacSparkline turns a series of numbers into a small, word-sized SVG sparkline, like this one charting posts per year:
The output is compact, has no dependencies, follows the surrounding text color (and with it dark mode), and describes itself to assistive technology (“rising from 4 to a peak of 87 in 2025, then falling to 79”).
You can use it, too, whether to generate sparklines by hand or programmatically. On the command line:
npx sparklemacsparkline --stdout 4,42,50,34,13In your code:
import { sparkline } from 'sparklemacsparkline';
const svg = sparkline([4, 42, 50, 34, 13], { title: 'Posts per year' });There are options for size, colors, labels, scaling, and more.
Example: Generating Sparklines in Eleventy
As sparklines get generated in memory, a static site generator like Eleventy can create them during the build, without files or caching. First, install Sparkle MacSparkline as a dev dependency:
npm i -D sparklemacsparklineOn meiert.com, a collection then counts posts per tag and year, and turns every series into a sparkline:
import { sparkline } from 'sparklemacsparkline';
eleventyConfig.addCollection('sparklines', collectionApi => {
const counts = countByTagAndYear(collectionApi.getFilteredByTag('posts')); // Tag → posts per year
return Object.fromEntries([...counts].map(([tag, values]) => [tag, sparkline(values, { decorative: true, width: 160, height: 20 })]));
});(`decorative` hides the chart from assistive technology, as the page already lists what it counts—and as the description would otherwise become part of the heading.)
A template can then print the respective sparkline wherever it’s needed—on meiert.com and dxsrc, in the headings of topic pages:
<h1>{{ title }} {{ collections.sparklines[tag] | safe }}</h1>_ Sparkle MacSparkline is still fresh: If you run into an issue, please report it. Other than that—enjoy!
About Me
I’m Jens (long: Jens Oliver Meiert), an engineering lead, guerrilla philosopher, and indie publisher. I’ve worked as a technical lead and manager at various companies (e.g., Google); I’m an active web and tool developer (code optimization, digital defense), a contributor to web standards (like HTML, CSS, WCAG), and a book author (O’Reilly, dxsrc). You’ll like my work if you value principled, pragmatic problem-solving and advocacy—follow along.
