A React Native line chart for mobile trend data with multi-series, sparkline, and custom tooltip support.
LineChartruns on the UI thread. React renders the parts once; data changes, layout, animations, scrubbing, the crosshair, the dot, the tooltip and the axis labels all update through Reanimated shared values without re-rendering. Rendering a chart requires the optionalreact-native-skiapeer (>=3.0.0).
import { LineChart } from 'heroui-native-pro';<LineChart data={...} xKey="...">
<LineChart.Grid />
<LineChart.XAxis />
<LineChart.YAxis />
<LineChart.Line dataKey="..." />
<LineChart.Crosshair />
<LineChart.Indicator dataKey="..." />
<LineChart.Tooltip dataKey="..." />
</LineChart>data) and the field holding the x value (xKey). Owns the canvas, the press gesture and the animation settings for every part. Set its height with className.dataKey field of each row.Every part is optional. A chart with only LineChart.Line is a sparkline.
<LineChart data={DATA} xKey="month" className="h-48">
<LineChart.Grid />
<LineChart.XAxis />
<LineChart.YAxis />
<LineChart.Line dataKey="revenue" />
</LineChart>Render one LineChart.Line per field. The y-axis fits all of them.
<LineChart data={DATA} xKey="month" className="h-48">
<LineChart.Grid />
<LineChart.YAxis />
<LineChart.Line dataKey="organic" colorClassName="accent-chart-3" />
<LineChart.Line dataKey="paidAds" colorClassName="accent-chart-1" />
</LineChart><LineChart.Line dataKey="actual" curveType="monotoneX" dotRadius={3} />
<LineChart.Line dataKey="target" curveType="natural" dashed />
<LineChart.Line dataKey="forecast" dashed={[2, 4]} />curveType accepts linear (default), natural, monotoneX, basis, bumpX, bumpY, cardinal, cardinal50, catmullRom, catmullRom0, catmullRom100, step, stepAfter and stepBefore.
Formatters run on the UI thread, so they must be worklets. Use the bundled ones or add the 'worklet' directive to your own.
import { formatCompact, formatCurrency } from 'heroui-native-pro';
const formatRevenue = (value: number) => {
'worklet';
return formatCurrency(value, { compact: true });
};
<LineChart.YAxis count={4} format={formatRevenue} />
<LineChart.Tooltip dataKey="revenue" format={formatCompact} />Bundled worklet formatters: formatCompact (15K), formatGroupedNumber (1,234.5), formatCurrency ($1,234.50, or $1.2K with compact) and formatPercent (12.3%).
Pass a new data array and the line glides to it. Point counts may differ between arrays. Always pass a new array: changes made to the same array in place are not detected.
<LineChart
data={series[range]}
xKey="index"
animate={{ type: 'timing', duration: 250 }}
>
<LineChart.Line dataKey="value" curveType="natural" />
</LineChart>animate is true by default (300 ms timing). Pass a spring ({ type: 'spring', damping: 20 }) or false. animation="disable-all" turns off every animation in the chart.
animateIn draws the line from left to right on mount. Change resetKey to replay it.
const [replay, setReplay] = useState(0);
<LineChart.Line dataKey="revenue" animateIn={1200} resetKey={replay} />;Add Crosshair, Indicator and Tooltip. They follow the finger on the UI thread.
<LineChart data={DATA} xKey="month" className="h-48">
<LineChart.Line dataKey="revenue" curveType="monotoneX" />
<LineChart.Crosshair />
<LineChart.Indicator dataKey="revenue" />
<LineChart.Tooltip dataKey="revenue" format={formatRevenue} />
</LineChart>By default the tooltip sits above the plot. Pass followPoint to float it above the pressed point instead (it flips below when there is no room).
To react on the JS thread, use onSelect. It fires only when the pressed row changes, and with null when the press ends.
<LineChart data={DATA} xKey="month" onSelect={(event) => setActive(event?.row ?? null)}>selection={{ activateAfter: 200 }} changes the press delay; selection={false} turns inspection off.
Build the content from ChartTooltipValue and ChartTooltipLabel. They show the pressed row's values; the tooltip's position follows on the UI thread.
import { ChartTooltipLabel, ChartTooltipValue } from 'heroui-native-pro';
<LineChart.Tooltip
followPoint
dataKey="revenue"
className="rounded-xl bg-surface p-2"
>
<ChartTooltipLabel xKey="month" className="text-xs text-muted" />
<ChartTooltipValue
dataKey="revenue"
format={formatRevenue}
className="text-sm text-foreground"
/>
</LineChart.Tooltip>;When format is a worklet (or omitted), ChartTooltipValue, ChartTooltipLabel and the default tooltip write their text from the UI thread, so scrubbing renders no React however many rows the finger crosses.
react-native-animateable-text (optional peer, >=0.19.3, New Architecture): the text is a native Text that sizes itself to every value. This gives the best tooltip performance.TextInput. It keeps the width of the widest value it has shown, so the tooltip does not change width while scrubbing.npm install react-native-animateable-textpnpm add react-native-animateable-textyarn add react-native-animateable-textbun add react-native-animateable-textnpx expo install react-native-animateable-textThe package ships a native view, so rebuild the app after installing it (for Expo, a new development build). Until the native view is in the build, charts keep using the TextInput fallback.
With a plain (non-worklet) format, React renders the text instead. It re-renders when the pressed row changes, at most once per JS frame, so a fast scrub never queues renders.
useChartSelection() returns the press state as shared values (active, index, x) for your own animated views inside the chart.
import { Card } from 'heroui-native';
import { formatCompact, LineChart } from 'heroui-native-pro';
import { View } from 'react-native';
const DATA = [
{ month: 'Jan', organic: 2000, paidAds: 1000 },
{ month: 'Feb', organic: 15000, paidAds: 10000 },
{ month: 'Mar', organic: 8000, paidAds: 12000 },
{ month: 'Apr', organic: 14000, paidAds: 14000 },
{ month: 'May', organic: 15000, paidAds: 8000 },
{ month: 'Jun', organic: 8000, paidAds: 9000 },
];
const formatSessions = (value: number) => {
'worklet';
return formatCompact(value);
};
export default function TrafficChart() {
return (
<View className="flex-1 justify-center px-5">
<Card>
<Card.Header>
<Card.Title>Traffic Source</Card.Title>
</Card.Header>
<Card.Body>
<LineChart data={DATA} xKey="month" className="h-[200px]">
<LineChart.Grid />
<LineChart.XAxis />
<LineChart.YAxis format={formatSessions} />
<LineChart.Line dataKey="organic" curveType="monotoneX" />
<LineChart.Line
dataKey="paidAds"
curveType="monotoneX"
colorClassName="accent-chart-1"
/>
<LineChart.Crosshair />
<LineChart.Indicator dataKey="organic" />
<LineChart.Tooltip dataKey="organic" format={formatSessions} />
</LineChart>
</Card.Body>
</Card>
</View>
);
}| prop | type | default | description |
|---|---|---|---|
data | Row[] | - | Rows to plot. A new array animates; mutating the same array is not detected. |
xKey | keyof Row | - | Field holding the x value: a number, or a category label shown as-is. |
yDomain | { min?: number; max?: number } | fitted | Fixes either end of the y-axis. |
animate | boolean | ChartTransition | true | Data-change animation ({ type: 'timing' | 'spring', ...config }). |
animation | 'disable-all' | - | "disable-all" turns off every animation in the chart. |
selection | { activateAfter?: number } | false | { activateAfter: 100 } | Press-and-drag inspection; false turns it off. |
onSelect | (event: { index: number; row: Row } | null) => void | - | Called on the JS thread when the pressed row changes; null when it ends. |
domainPadding | number | 8 | Pixels between the plot edges and the outermost points. |
className | string | - | Root view class. Set the height here. |
children | ReactNode | - | Chart parts. |
| prop | type | default | description |
|---|---|---|---|
dataKey | string | - | Field of each row to plot. |
curveType | CurveType | 'linear' | Curve through the points. |
colorClassName | string | 'accent-chart-3' | Uniwind color class for the stroke. |
strokeWidth | number | 2 | Stroke width. |
dashed | boolean | number[] | - | Dash pattern, or true for 5-on/5-off. |
dotRadius | number | - | Draws a dot of this radius on every point. |
animateIn | boolean | number | - | Draws the line in on mount; a number sets the duration in ms. |
resetKey | unknown | - | Changing it replays animateIn. |
axis | 'left' | 'right' | 'left' | Y-axis the line is plotted against (see ComposedChart). |
| prop | type | default | description |
|---|---|---|---|
colorClassName | string | muted at 40% | Uniwind color class for the lines. |
strokeWidth | number | 1 | Line width. |
dashed | boolean | number[] | - | Dash pattern, or true for 5-on/5-off. |
| prop | type | default | description |
|---|---|---|---|
count | number | 6 / 5 | Approximate number of ticks. The grid follows the y-axis. |
format | (value: number) => string | String(value) | Formats tick values. Must be a worklet. |
hidden | boolean | - | Hides the labels (the y-axis still sets the grid tick count). |
| prop | type | default | description |
|---|---|---|---|
strokeWidth | number | 1 | Line width. |
dashed | boolean | number[] | true | Dash pattern, true for 4-on/4-off, false solid. |
| prop | type | default | description |
|---|---|---|---|
dataKey | string | - | Series the dot follows. |
colorClassName | string | 'accent-chart-3' | Uniwind color class for the dot. |
radius | number | 5 | Indicator radius. |
ringRadius | number | 7 | Radius of the background ring. |
| prop | type | default | description |
|---|---|---|---|
dataKey | string | - | Series whose value the default content shows. |
format | (value: number) => string | - | Formats the default value. Must be a worklet. |
followPoint | boolean | false | Floats the tooltip at the pressed point instead of above the plot. |
placement | 'top' | 'bottom' | 'top' | Preferred side of the point; flips when there is no room. |
gap | number | 16 | Space between the point and the tooltip. |
className | string | - | Tooltip view class. |
children | ReactNode | - | Custom content (ChartTooltipValue, ChartTooltipLabel). |
Returns { active, index, x } shared values of the nearest chart's press state. Use it inside chart parts for custom animated overlays.
useChartSelectedValue(dataKey, format?) returns a SharedValue<string> with the formatted value of dataKey at the pressed row.