Sparkline
Shrink a line or area to a word-sized trend with display "sparkline", which strips the title, axes, legend and watermark so it fits beside a number.
A sparkline is a trend with everything else removed: no title, axes, legend or source line. It sits next to a number you render yourself, such as a KPI card or a table cell, and answers whether the number is going up or down.
Use a full line chart when the reader needs to read values or dates off the axes. A sparkline shows no axes, so pair it with the figures it summarizes. For sparkline cells inside tables, see data table.
A line sparkline
Set display: "sparkline" on a line or area spec. The engine strips chrome, axes, legend and watermark, so the chart fills whatever box you give it. Give it a fixed height of 36 to 56 pixels. Without stroke, the line is colored by its trend: the theme’s positive color when it rises, negative when it falls. The first demo sets stroke to use an accent color instead.
{
"mark": {
"type": "line",
"stroke": "#0891b2"
},
"data": [
{
"t": 0,
"value": 1965
},
{
"t": 1,
"value": 1970.2
},
{
"t": 2,
"value": 1979.27
},
{
"t": 3,
"value": 1991.22
},
{
"t": 4,
"value": 1980.98
},
{
"t": 5,
"value": 1989.94
},
// … 34 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0891b2"
},
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0891b2"
},
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0891b2"
},
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0891b2"
},
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
An area sparkline
Swap the mark to "area" for a filled version. The channels are the same: a position field on x and the value on y. With no stroke, the trend color drives both the line and the fill.
{
"mark": "area",
"data": [
{
"t": 0,
"value": 4180
},
{
"t": 1,
"value": 4195.53
},
{
"t": 2,
"value": 4186.73
},
{
"t": 3,
"value": 4194.96
},
{
"t": 4,
"value": 4184.79
},
{
"t": 5,
"value": 4170.74
},
// … 34 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
The last-point dot
A sparkline marks its last point by default. point: "first" moves the dot and point: false removes it, as here.
{
"mark": {
"type": "line",
"stroke": "#0891b2",
"point": false
},
"data": [
{
"t": 0,
"value": 33420
},
{
"t": 1,
"value": 33618.38
},
{
"t": 2,
"value": 33771.75
},
{
"t": 3,
"value": 33840.18
},
{
"t": 4,
"value": 33725.16
},
{
"t": 5,
"value": 33680
},
// … 34 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0891b2",
"point": false
},
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0891b2",
"point": false
},
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0891b2",
"point": false
},
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0891b2",
"point": false
},
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
A taller box
A sparkline has no size of its own. It fills the box it is mounted in, so a taller box gives the same series more vertical room. Pick the height from the layout the sparkline sits in.
{
"mark": "area",
"data": [
{
"t": 0,
"value": 7510
},
{
"t": 1,
"value": 7525.12
},
{
"t": 2,
"value": 7510.51
},
{
"t": 3,
"value": 7475.79
},
{
"t": 4,
"value": 7447.86
},
{
"t": 5,
"value": 7452.3
},
// … 34 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "t",
"type": "ordinal"
},
"y": {
"field": "value",
"type": "quantitative"
}
},
"display": "sparkline"
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Fields that matter
| Field | What it does |
|---|---|
display |
"sparkline" strips chrome, axes, legend and watermark. The default is "full". |
mark |
"line" or "area". An object sets stroke and point. |
mark.point |
The last point is marked by default. "first" moves the dot to the first point and false removes it. |
encoding.x |
The position of each reading. An ordinal field is enough when you only need the order. |
encoding.y |
The value that sets the height of the line. |
Related
- Line for a trend with axes.
- Area for a filled trend with axes.
- Dashboard tiles for sparklines inside KPI cards.
- Data table for sparkline columns in a table.
- Marks reference lists the channels and options each mark type takes.