Sparkline

A sparkline is a small chart with no axes or labels that shows a trend at a glance. Put one beside a number, such as in a stat card or a table row.

Usage

Code
import { Sparkline } from '@leverui/components';
Code
<Sparkline
  data={[{ value: 10 }, { value: 16 }, { value: 19 }, { value: 15 }]}
  aria-label="Values over time"
/>

Examples

Basic

Render the compact area chart.

Stock

Pair the Amazon closing price and trend with a compact area chart.

AMZN
Amazon Inc.
$189.46
30.28%

Line Chart

Keep only the line stroke and omit area fill.

Gradient

Fade the area fill below a smoothed line.

Interaction

Show a changing download count beside the hovered chart point.

Weekly downloads

345,000

Reference

Label the minimum and maximum against a wider line chart.

1019

Stat

Fill the base of a statistic card with a short visitor trend.

Unique visitors
192.1k

Props

Data

PropDefaultTypeDescription
data—readonly SparklineData[]Points to draw. Required.

The scale starts at zero. A value below zero is drawn at zero.

Look

PropDefaultTypeDescription
typearea'area' | 'line' | 'bar'Kind of chart.
colorteal.solidstringColor token such as teal.solid, or a CSS color.
gradientfalsebooleanFade the area fill toward the bottom.
fillOpacity0.2numberArea fill opacity, 0 to 1.
smoothfalsebooleanRound the line through the points.
referencefalsebooleanDashed lines at the lowest and highest values.
unstyledfalsebooleanTurn off default styles. Shapes stay.

With gradient, fillOpacity defaults to 0.6.

reference also prints the two values, the lowest just outside the left edge and the highest just outside the right. Leave room for them.

Size

PropDefaultTypeDescription
width112number | stringWidth in pixels, or a CSS width such as 100%.
height48numberHeight, in pixels.
inset5numberSpace between the edge and the plot, in pixels.

With a CSS width such as width="100%", the chart follows its container.

Set inset={0} to reach the edges of the container, as in a statistic card. A negative inset, or one that leaves no room for the plot, throws an error.

Interaction

PropDefaultTypeDescription
activeIndex—number | nullMark this point with a line and a dot.
onActiveIndexChange—(index: number | null) => voidRuns as the pointer moves over the chart.

onActiveIndexChange gets the index of the point nearest the pointer, or null when the pointer leaves.

Code
const [active, setActive] = createSignal<number | null>(null);

<Sparkline
  data={data}
  activeIndex={active()}
  onActiveIndexChange={setActive}
  aria-label="Weekly downloads"
/>;

class, className, style and ref go to the <svg> element.

Data points

FieldTypeDescription
valuenumberNumber to draw. Required.
fillstringColor token or CSS color for this bar. Bar charts only.
Code
<Sparkline
  type="bar"
  data={[{ value: 10 }, { value: 16 }, { value: 19, fill: 'red.solid' }]}
  aria-label="Values over time"
/>

Styling

Code
<Sparkline class="visitors-sparkline" data={data} />
Code
.visitors-sparkline {
  --leverui-sparkline-line-width: 1.5;
  --leverui-sparkline-reference-color: #a6c9c0;
}

Variables

VariableDefaultDescription
--leverui-sparkline-width112pxChart width.
--leverui-sparkline-height48pxChart height.
--leverui-sparkline-colorSolid tealLine, fill and bar color.
--leverui-sparkline-area-opacity0.2Area fill opacity.
--leverui-sparkline-line-width2Line thickness.
--leverui-sparkline-reference-colorEmphasized border colorReference line color.
--leverui-sparkline-label-colorMuted text colorReference value color.
--leverui-sparkline-active-point-strokeBackground colorOutline color of the active dot.

The color, width, height and fillOpacity props set the matching variable, so a prop wins over a variable set in CSS.

Classes

Root: .leverui-sparkline.

Parts, each prefixed with .leverui-sparkline__: area, line, bar, reference-line, reference-label, active-guide, active-point, gradient-start, gradient-end.