Skip to main content

LineChart

Every example on this page is live, drawn by the library's Storybook, where you can also change its props and see the result.

A line chart for displaying numerical data as a connected series of points. Supports simple Y-value arrays, explicit X,Y pairs and dates, one series or several, with optional axes, grid, dots, labels and a legend.

The chart fills the box it is given and keeps filling it in a widget's preview at any size: the lines stretch, while text, ticks, dots and dashes keep their size.

Import​

import { LineChart } from "~lib/charts/line-chart";

Props​

NameTypeRequiredDefaultDescription
dataCartesianPoint[] | Record<string, CartesianPoint[]>YesN/AThe points to plot (at least 2), or several named series. See Point Formats below
axis{ x?: XAxisConfig, y?: YAxisConfig }NoN/AAxis display configuration. See Axis Configuration below
widthnumber | stringNo300Chart width, in pixels or any CSS length
heightnumber | stringNo300Chart height, in pixels or any CSS length
smoothbooleanNofalseDraw smooth curves through the points
dotsbooleanNofalseMark every point with a dot
stackedbooleanNofalseStack each series on the ones before it
direction"horizontal" | "vertical"No"horizontal""vertical" turns the chart on its side: values run across and categories up the left
legendboolean | { position: "top" | "top-left" | "top-right" | "bottom" | "bottom-left" | "bottom-right" }NoN/AName each series, below the chart unless placed. Only shown for two or more series
dataLabel(point, index, seriesKey) => ReactNodeNoN/AA label to show by each point; return null to skip one
dataIcon(point, index, seriesKey) => IconName | nullNoN/AAn icon to set above a point's label, or on its own; return null to skip one

Each series of several is told apart by its dash: solid, dashed, dotted, then dash-dot.

Point Formats​

The data prop accepts any of the following CartesianPoint formats:

FormatDescriptionExample
number[]Y values only. X is auto-indexed starting at 0[100, 200, 400, 300]
[number, number][]Explicit [x, y] tuples[[10, 100], [20, 200], [30, 400]]
{ x: number; y: number }[]Object notation[{ x: 10, y: 100 }, { x: 20, y: 200 }]
[Date, number][]Date on X axis (enables date tick formatting)[[new Date("2024-01-01"), 120], ...]
[Date, number, T][]Date with arbitrary metadata (accessible via dataLabel)[[new Date("2024-01-01"), 120, { emoji: "☀️" }]]

Axis Configuration​

y configures the value axis and x the category axis, wherever direction puts them.

NameTypeAxisRequiredDefaultDescription
visiblebooleanBothNofalseWhether to show the axis, its ticks and their labels
namestringBothNoN/ALabel displayed along the axis (requires visible: true)
gridbooleanBothNofalseDraw dotted grid lines at the axis's ticks
minnumberyNoN/AWhere the value axis starts, instead of the lowest value
maxnumberyNoN/AWhere the value axis ends, when above the highest value
type"date"xNoN/ATreat x values as dates (epoch milliseconds)
dateFormatIntl.DateTimeFormatOptionsxNoN/AHow date ticks are written, instead of the automatic format
timeZonestringxNoN/AThe time zone date ticks fall on whole hours and days of

Date ticks fall on whole minutes, days, Mondays, months or years, and are labelled at the granularity of their spacing unless dateFormat is given.

Ticks fall on the clock of the time zone in timeZone, and are written in it too, so a chart of a day's prices ticks at midnight, 6, noon and 6 in the place it is about, wherever the frame happens to be drawn. Pass the frame's own zone from useTimeZone(). Without timeZone, the zone in dateFormat.timeZone is used, and without either, the clock of whatever machine draws the widget.

Open With Time Zone in Storybook

Icons on Points​

dataIcon names an Icon for a point, set above its label at a size that reads on its own, so a forecast can show each hour's sky on the line itself rather than in a list beside it. Return null for a point that should have none. The icon and its label move together: above the point, or below it near the top of the chart.

<LineChart
data={hours.map((hour) => [new Date(hour.time), hour.temperature, hour.sky])}
dots
dataLabel={(point) => `${point[1]}°`}
dataIcon={(point) => point[2]}
/>
Open Hourly Forecast in Storybook

Examples​

Simple Line​

A basic line chart with Y values:

Open With Points in Storybook

Negative Values​

The chart auto-scales to accommodate negative values:

Open With Negative Points in Storybook

With Both Axes​

Display X and Y axis lines and tick marks:

Open With Both Axis in Storybook

X,Y Coordinate Pairs with Axes​

Use explicit [x, y] pairs for precise positioning:

Open With XY Points And Axis in Storybook

Irregular X Spacing​

Points do not need to be evenly spaced along the X axis:

Open With Irregular XY Points And Axis in Storybook

With Axis Labels​

Use the name property on each axis config to display a label along the axis:

Open With Axis Labels in Storybook

See Also​