Skip to main content

Line

The Line component is used to display line charts. You can specify the type of data that the line is being trended over with the scaleType prop. You can add Trendlines and MetricRanges as children to show trends in your data or include a ranged area of data in addition to the line. It's also possible to define tooltips and on-click popovers for the line using the ChartTooltip and ChartPopover components respectively as children.

Examples

Basic Line Chart

<Chart data={data}>
<Axis position="bottom" labelFormat="time" ticks baseline />
<Axis position="left" grid title="Users" />
<Line metric="users" color="event" />
<Legend position="bottom" />
</Chart>

Line chart example Line chart example

Line with Tooltip and Click Handler

<Chart data={data}>
<Axis position='bottom' labelFormat='time' ticks baseline />
<Axis position='left' grid title="Users" />
<Line
metric="users"
color="browser"
onClick={(event, data) => console.log('Clicked:', data)}
>
<ChartTooltip>
{(datum) => (
<div>
<div>{datum.date}</div>
<div>Event: {datum.event}</div>
<div>Users: {Number(datum.users).toLocaleString()}</div>
</div>
)}
</ChartTooltip>
</Line>
<Legend position='bottom'/>
</Chart>

Line tooltip chart example Line tooltip chart example

Line with Trendline and MetricRange

<Chart data={data}>
<Axis position="bottom" labelFormat="time" ticks baseline />
<Axis position="left" grid title="Users" />
<Line metric="users" color="event">
<Trendline method="quadratic" />
<MetricRange upperMetric="upperBound" lowerMetric="lowerBound" />
</Line>
</Chart>

Line chart with metric range and trendline example Line chart with metric range and trendline example

Props

nametypedefaultdescription
childrenChartTooltip | ChartPopover | MetricRange | TrendlineOptional elements that can be rendered within the chart. Use these to add tooltips, popovers, metric ranges, or trendlines to your line chart.
colorstring'series'The key in the data that defines what color that line will be. This is not a color value itself but rather the key in the data that will map to the colors scale.
For example: A line chart that has a different line and color for each operating system, color would be set to the name of the key in the data that defines which operating system it is (color="operatingSystem").
dimensionstring'datetime'The key in the data that the metric is trended against. This is the x-axis for a standard line chart.
lineTypestring | {value: LineType | number[]}If a string is provided, this string is the key in the data that lines will be grouped into series by. Each unique value for this key in the provided data will map to a line type from the lineTypes scale.
If an object is provided with format {value: 'solid' | 'dashed' | 'dotted' | number[]}, this will set the line type for all lines.
metricstring'value'The key in the data that is used for the value of the data point.
namestringLine name. Useful for if you need to traverse the chart object to find this line.
onClick(event: MouseEvent, data: any) => voidCallback function that will be executed when a point or section of the line is clicked. Receives the mouse event and the data point as arguments.
paddingnumberSets the chart area padding. This is a ratio from 0 to 1 for categorical scales (point) and a pixel value for continuous scales (time, linear)
scaleType'linear' | 'time' | 'point''time'Type of data that the line is trended over. If using 'time', the dimension data must be in milliseconds since Jan 1, 1970 UTC (epoch time).
If you are plotting this line along with other marks that are ordinal (ex. bar), then you must use a 'point' scale for the two to line up correctly.
staticPointstringKey in the data that if it exists and its value is true, a visible point will be shown on the line for that data item.

Best Practices

  1. Scale Type Selection

    • Use scaleType="time" for time-series data
    • Use scaleType="linear" for continuous numerical data
    • Use scaleType="point" when working with categorical data or when combining with ordinal marks like bars
  2. Interactivity

    • Add tooltips using ChartTooltip for better data exploration
    • Use onClick handlers for interactive features like drilling down into data
    • Consider using ChartPopover for displaying detailed information on click
  3. Data Analysis

    • Use Trendline to show data trends
    • Add MetricRange to display confidence intervals or bounds
    • Use staticPoint to highlight specific data points of interest

Accessibility

The Line component follows accessibility best practices:

  • Uses semantic SVG elements for better screen reader support
  • Supports keyboard navigation when interactive elements are added
  • Color combinations should meet WCAG contrast requirements