Demo: https://bn-l.github.io/sparkline-svelte/
A lightweight and customizable Sparkline component for Svelte 5, based on fnando/sparkline (used on npm to show a chart for weekly downloads), with various improvements and updates. Works on touchscreens, tested on all browsers.
This library creates a small responsive line chart (apparently called "sparkline" for whatever reason)--without axis labels--for quick data visualization. It is ideal for displaying trends and patterns in data in a compact form--making it good for use in dashboards, reports, etc. The SVG output scales to fit its container for responsiveness on different screen sizes.
Changes to the data prop or any option will update the SVG reactively (with svelte 5 managing the SVG dom updates). Performance should be optimal as it just atomically updates the necessary svg paths.
Install the package via npm:
npm install sparkline-svelte
<script>
import { Sparkline } from "sparkline-svelte";
</script>
<Sparkline data={[5, 10, 15, 10, 5]} />
The Sparkline
component accepts the following props (only the options.lineColor
prop
is needed to change all the colors automatically. The other options can be mostly ignored):
data
(required)number[] | { label: string; value: number }[]
label
and value
properties, representing the data points to plot. By supplying a label
, you can provide any label for the values, and they will be displayed on the tooltip as label: ${value}
when hovering over the sparkline.options
(optional)Object
lineColor
string
#FF476F
fillColor
, cursorColor
, tooltipFillColor
, tooltipTextColor
) based on the provided lineColor
.fillColor
string
lineColor
cursorColor
string
lineColor
strokeWidth
number
6
spotRadius
number
2
interactive
boolean
false
true
.showTooltip
boolean
true
tooltipTextColor
string
tooltipFillColor
tooltipFillColor
string
fillColor
tooltipFontSize
string
"0.875rem"
cursorWidth
number
2
svgClass
string
""
toolTipClass
string
"tooltip-class"
cursorData
(optional, bindable)DataPoint | null
x
, y
, value
, index
, and label
properties.interface DataPoint {
x: number;
y: number;
value: number;
index: number;
label?: string;
}
You can bind to cursorData
to get the current data point under the cursor:
<script>
import { Sparkline } from "sparkline-svelte";
let data = [5, 10, 15, 10, 5];
let cursorData = $state(null);
</script>
<Sparkline {data} options={{ interactive: true }} bind:cursorData />
{#if cursorData}
<p>Current Value: {cursorData.value}</p>
<p>Index: {cursorData.index}</p>
<p>X: {cursorData.x}</p>
<p>Y: {cursorData.y}</p>
{#if cursorData.label}
<p>Label: {cursorData.label}</p>
{/if}
{/if}
<script>
import { Sparkline } from "sparkline-svelte";
let data = [
{ label: "Jan", value: 10 },
{ label: "Feb", value: 15 },
{ label: "Mar", value: 12 },
{ label: "Apr", value: 20 },
{ label: "May", value: 18 }
];
</script>
<Sparkline {data} options={{ lineColor: "#27ae60", interactive: true }} />
In this example, by supplying a label
for each data point, the tooltip will display the label and value as label: ${value}
when hovering over the sparkline.
<script>
import { Sparkline } from "sparkline-svelte";
let data = $state([]);
// Simulate data updates
let interval;
$effect(() => {
interval = setInterval(() => {
let value = Math.floor(Math.random() * 100);
data.push(value);
// Keep only the last 10 data points
if (data.length > 10) {
data.shift();
}
}, 1000);
return () => clearInterval(interval);
});
</script>
<Sparkline {data} options={{ lineColor: "#e74c3c", interactive: true }} />
The graph will reactively update as the data prop is updated.