LineChart
Line chart with several series, dots, dashed comparisons and gaps for missing data
Usage
LineChart is imported from its own subpath and needs the optional recharts peer.
npm install rechartsimport {LineChart} from "@peakui-pro/react/line-chart";Two series on one plot, with a straight line between observations (type="linear").
Anatomy
import {LineChart} from "@peakui-pro/react/line-chart";
// Only the root is this package's own; the parts are the charting library's components under
// PeakUI names. The root is not exported from `@peakui-pro/react`, so an app that renders no
// chart never needs the peer installed, not even to render on a server.
export default () => (
<LineChart aria-label="Sessions by month" data={[]}>
<LineChart.Grid />
<LineChart.XAxis />
<LineChart.YAxis />
<LineChart.Line dataKey="sessions" />
<LineChart.Tooltip content={<LineChart.TooltipContent />} />
</LineChart>
);Missing observations
A null field in data is a gap: the line breaks at that category instead of falling to zero. Set
connectNulls on the Line to draw across it instead.
const data = [
{month: "Jun", revenue: 8400},
{month: "Jul", revenue: null}, // no report this month
{month: "Aug", revenue: 9200},
];Accessibility
The plot takes focus and answers the arrow keys, so aria-label names it, not the wrapper. Series
are told apart by colour; when two lines must still read without it, dash one of them with
strokeDasharray (see Dashed Comparison) and give every series a name the tooltip can print.
// Named by a string, or by the element that already carries the metric's title.
<LineChart aria-label="Sessions by month" data={data} />
<LineChart aria-labelledby="sessions-title" data={data} />
// A sparkline that only repeats a figure printed beside it leaves the keyboard layer out.
<LineChart accessibilityLayer={false} data={data} />Examples
Portfolio
The range control swaps data; the chart is the same element throughout. A hidden YAxis with
domain={["dataMin", "dataMax"]} lets the line fill the plot.
KPI With Chart
KPI.Chart renders an area. A line is composed by hand in the right column of KPI.Content, where
it keeps its own box and never pushes the figure.
Stats With Chart
Multi Line Chart Colors
Series colours are the consumer's: var(--chart-1) to var(--chart-5). There is no colour prop.
Sparkline
No axis, no grid, no tooltip, and a height that fits beside a figure.
Custom Tooltip
With Dots
A dot on every observation. The shared stylesheet outlines it in the surface colour and leaves the
fill to you, so pass the series colour in dot. The gap in July is a null field.
Dashed Comparison
strokeDasharray turns the second series into a pattern, which still separates it from the first
when colour is not available.
Customization
Tailwind CSS
<LineChart className="rounded-xl bg-surface p-4" data={data} />Global CSS
@layer components {
.chart {
--chart-axis-size: 11px;
--chart-grid-opacity: 0.4;
}
}Styling Reference
PeakUI follows the BEM methodology, so variants and states are plain class names you can target.
CSS Classes
The LineChart component uses these CSS classes (view source styles):
Base Classes
.chart- The block every cartesian chart carries; axis type, grid, tooltip cursor and dots.line-chart- This chart's own block; gives a series line round joins and ends
Slots
[data-slot="line-chart"]- The root element
CSS Variables
| Variable | Purpose |
|---|---|
--chart-axis-size | Type size of the axis labels; set on .chart |
--chart-grid-opacity | How far the grid recedes behind the data; set on .chart |
--chart-1 … --chart-5 | The series ramp, derived from the theme's accent |
API Reference
LineChart
| Prop | Type | Default | Description |
|---|---|---|---|
data | Record<string, number | string | null>[] | - | Required. One object per category; a null field is a missing observation |
height | number | `${number}%` | 300 | Height of the plot |
width | number | `${number}%` | '100%' | Width of the plot |
margin | {top?: number; right?: number; bottom?: number; left?: number} | {top: 8, right: 8, bottom: 0, left: 0} | Space between the plot and the root's edges |
accessibilityLayer | boolean | true | Whether the plot is focusable and answers the arrow keys |
children | ReactNode | - | The chart's parts |
aria-label | string | - | Names the plot, which is what takes focus — not the wrapping <div> |
aria-labelledby | string | - | Names the plot by another element's id |
className | string | - | Additional classes |
...rest | ComponentProps<'div'> | - | Native <div> attributes, on the wrapper |
LineChart.Line
The charting library's Line, re-exported. See the Recharts Line API.
LineChart.XAxis
The charting library's XAxis, re-exported. See the Recharts XAxis API.
LineChart.YAxis
The charting library's YAxis, re-exported. See the Recharts YAxis API.
LineChart.Grid
The charting library's CartesianGrid, re-exported. See the Recharts CartesianGrid API.
LineChart.Tooltip
The charting library's Tooltip, re-exported. See the Recharts Tooltip API.
LineChart.TooltipContent
ChartTooltip.Content, for the content prop of LineChart.Tooltip. Its props are on the ChartTooltip page.