Line Chart

Shows a trend over time as one or more lines.

Usage

Code
import { LineChart } from '@leverui/components';
Code
<LineChart
  data={[
    { month: 'January', sales: 10 },
    { month: 'February', sales: 95 },
  ]}
  xKey="month"
  series={[{ name: 'sales', color: 'teal.solid', strokeWidth: 2 }]}
  showYAxis
  aria-label="Monthly sales"
/>

Examples

Basic

Draw the single teal sales line.

Multiple

Plot Mac and Linux as independent lines with a legend.

linuxmac

No Dots

Omit point markers from the three operating-system lines.

linuxmacwindows

Start End Tick

Show only the first and last category ticks with a descriptive axis label.

Value Formatter

Format the numeric axis as compact US-dollar revenue.

Dashed

Dash the Windows line and vertical grid while leaving Mac solid.

Nulls

Connect the sales samples on either side of missing months.

Stroke Width

Emphasize a smoothed line using the four-pixel stroke.

Value Domain

Crop the value scale to make close sales values easier to inspect.

Composition

Pair a small three-series chart with the stat totals.

Customers by channel

Facebook Ads
325
Organic
387
Google Ads
327

Series Label

Use descriptive legend labels while retaining the original data keys.

Linux salesMac sales

Types

Compare the eight line interpolation modes on the same source data.

<LineChart curve="linear" />

<LineChart curve="bump" />

<LineChart curve="basis" />

<LineChart curve="step" />

<LineChart curve="stepBefore" />

<LineChart curve="stepAfter" />

<LineChart curve="natural" />

<LineChart curve="monotone" />

Custom Dot

Highlight each sales sample with a larger outlined point.

Gradient

Change stroke color across a naturally curved temperature line.

Point Label

Place bold values to the right of each line point.

400300200278189239349

Custom Tooltip

Replace the standard chart tooltip with the teal customer card.

Legend Interaction

Dim unselected lines while hovering a series label.

linuxmac
Hover on "mac" ↑

Reference Point

Mark the latest current-year sample and the horizontal target.

Last yearThis year

Biaxial

Plot Windows on the left scale and Mac on the right scale.

macwindows

Props

Data

PropDefaultTypeDescription
data—readonly CartesianDatum[]Rows to draw. Required.
xKey—stringField that holds the category. Required.
series—readonly CartesianSeries[]Number fields to draw. Required.

Lines

PropDefaultTypeDescription
curvelinearCartesianCurveLine shape.
connectNullsfalsebooleanDraw the line across missing values.
dotsfalsebooleanDraw a dot at each value.
pointLabelsfalsebooleanPrint each value at its point, with a dot.
pointLabelPositiontop'top' | 'right'Side of the point the value sits on.

curve values: linear, bump, basis, step, stepBefore, stepAfter, natural, monotone.

Thickness, dashes and colors are set per line in Series options.

Axes and grid

PropDefaultTypeDescription
showXAxistruebooleanShow category labels.
showYAxisfalsebooleanShow number labels on the left.
showAxisLinesfalsebooleanDraw axis lines and tick marks.
xAxisLabel—stringTitle below the X-axis.
yAxisLabel—stringTitle beside the Y-axis.
rightYAxisLabel—stringTitle beside the right Y-axis.
xTickFormatter—(value: string) => stringFormat each category label.
yTickFormatter—(value: number) => stringFormat each number label on the left.
xTickValues—readonly string[]Only these categories get a label and a vertical line.
yTickValues—readonly number[]Exact numbers that get a label and a grid line.
xTickAngle0numberRotate category labels, in degrees.
xPadding—{ left?: number; right?: number }Space before the first and after the last category, in px.
yDomain—readonly [number, number]Lowest and highest number on the Y-axis.
rightYDomain—readonly [number, number]Lowest and highest number on the right Y-axis.
showGridtruebooleanShow the grid.
gridHorizontaltruebooleanShow horizontal grid lines.
gridVerticalfalsebooleanShow vertical grid lines.
gridDashedfalsebooleanDash the grid lines.

Without yDomain, the Y-axis fits the data and includes zero. In yDomain, the first number must be lower than the second.

Number labels round to whole numbers unless you pass yTickFormatter. Set yDomain with yTickValues to put labels at exact numbers.

The right Y-axis shows when a series sets yAxis to right. Its labels are always whole numbers.

Each xPadding value must be zero or more.

More in Axis and Cartesian Grid.

Legend and tooltip

PropDefaultTypeDescription
showLegendfalsebooleanShow a legend.
legendPlacementbottom'bottom' | 'top-right'Legend position.
legendReversefalsebooleanReverse the legend order.
legendInteraction—'hover'Dim the other series on legend hover or focus.
tooltiptruebooleanShow values for the category under the pointer.
tooltipContent—(row, series) => JSX.ElementRender your own tooltip.

Reference marks

PropDefaultTypeDescription
referenceLines—object[]Vertical lines at named categories.
referenceValue—objectHorizontal line at one number.
referencePoint—objectMarker at one category and number.
MarkFields (? is optional)
referenceLinesx, label?, color?
referenceValuey, label?, color?, strokeDasharray?
referencePointx, y, color?, radius?

x is a category name. y is a number on the left Y-axis. color defaults to red, radius to 6.

Size

PropDefaultTypeDescription
height—numberFixed height, in pixels.
maxHeight384numberTallest the chart grows when height is not set.
plotMargin—objectSpace around the plot, in pixels.

Without height, the chart keeps a 4:3 shape up to maxHeight. maxHeight must be more than zero.

plotMargin takes top, right, bottom, left, each zero or more. Example: plotMargin={{ top: 40, left: 40, right: 40 }}.

Other attributes and event handlers go to the root element.

Series options

FieldTypeDescription
namestringNumber field in your data. Required.
colorstringColor token such as teal.solid, or a CSS color. Required.
labelstringName in the legend and tooltip. Default name.
strokestringLine color only. Dots and legend keep color.
strokeWidthnumberLine thickness. Default 1.
strokeDasharraystringDash pattern, such as 4 4.
strokeGradientStops{ offset: string; color: string }[]Colors the line blends through, top to bottom. Replaces stroke.
dotRadiusnumberDot size. Default 3.
dotFillstringDot fill color. Default color.
dotStrokestringDot outline color. Default page background.
dotStrokeWidthnumberDot outline thickness. Default 1.
activeDotRadiusnumberSize of the dot under the pointer. Default dotRadius.
activeDotStrokestringOutline color of the dot under the pointer.
yAxis'left' | 'right'Y-axis the series uses. Default left.
Code
<LineChart
  data={data}
  xKey="month"
  series={[
    {
      name: 'sales',
      color: 'teal.solid',
      strokeGradientStops: [
        { offset: '0%', color: 'teal.solid' },
        { offset: '100%', color: 'purple.solid' },
      ],
    },
  ]}
/>

Keyboard

The chart takes focus when the tooltip is on.

KeyAction
ArrowLeft ArrowRightPrevious or next category.
EscapeClose the tooltip.

Styling

Code
<LineChart class="sales-line" data={data} xKey="month" series={series} />
Code
.sales-line {
  --leverui-cartesian-chart-legend-gap: 1rem;
}

.sales-line .leverui-cartesian-chart__pointLabel {
  font-size: 0.875rem;
}

Variables

VariableDefaultDescription
--leverui-cartesian-chart-min-width280pxNarrowest the chart gets.
--leverui-cartesian-chart-legend-gap12pxSpace between legend items.
--leverui-cartesian-chart-tooltip-bgPanel colorTooltip background.
--leverui-chart-axis-line-colorBorder colorAxis line and tick mark color.

Grid variables are in Cartesian Grid.

Classes

Root: .leverui-line-chart and .leverui-cartesian-chart__root.

Parts, each prefixed with .leverui-cartesian-chart__: svg, axes, rightAxis, axisLine, axisTitle, gridLine, series, line, dot, pointLabel, referenceLine, referenceValue, referenceValueLabel, referencePoint, referenceLabel, cursor, legend, legendItem, legendSwatch, tooltip, tooltipItem, tooltipSwatch.