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 Trendline
s 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 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 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>
Props
name | type | default | description |
---|---|---|---|
children | ChartTooltip | ChartPopover | MetricRange | Trendline | – | Optional elements that can be rendered within the chart. Use these to add tooltips, popovers, metric ranges, or trendlines to your line chart. |
color | string | '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"). |
dimension | string | 'datetime' | The key in the data that the metric is trended against. This is the x-axis for a standard line chart. |
lineType | string | {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. |
metric | string | 'value' | The key in the data that is used for the value of the data point. |
name | string | – | Line name. Useful for if you need to traverse the chart object to find this line. |
onClick | (event: MouseEvent, data: any) => void | – | Callback 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. |
padding | number | – | Sets 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. |
staticPoint | string | – | Key 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
-
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
- Use
-
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
- Add tooltips using
-
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
- Use
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