# React Native Sparkline (Pro)

> A small React Native sparkline for iOS, Android and the web: a trend line, a gradient area or spark bars with no axes beside a number, gliding to live values, with a screen-reader summary of its range and trend.

# Sparkline Pro

A small chart with no axes, for a trend beside a number in a stat card or a list row: a line, a line over a fading fill (`variant="area"`), or bars (`variant="bar"`). It glides to new values, so it can tick with live data. Screen readers hear its range and trend ("up 32%, from 120 to 158").

```tsx
import { Sparkline } from '@patine-ui/pro';
```

## Live data

```tsx live
import { useEffect, useState } from 'react';
import { HStack, Text, VStack } from 'patine-ui';
import { Sparkline } from '@patine-ui/pro';

const step = (v) => Math.max(10, Math.round(v + (Math.random() - 0.45) * 12));

export default function Example() {
  const [data, setData] = useState([120, 132, 101, 134, 150, 142, 158, 149, 163, 171]);
  useEffect(() => {
    const id = setInterval(() => setData((d) => [...d.slice(1), step(d[d.length - 1])]), 1200);
    return () => clearInterval(id);
  }, []);
  const last = data[data.length - 1];
  const change = Math.round(((last - data[0]) / data[0]) * 100);
  return (
    <VStack gap="md" style={{ width: 340 }}>
      {['line', 'area', 'bar'].map((variant) => (
        <HStack key={variant} gap="lg" align="center">
          <VStack style={{ width: 120 }}>
            <Text size="sm" color="textSecondary">{variant === 'bar' ? 'Orders' : variant === 'area' ? 'Revenue' : 'Visitors'}</Text>
            <Text size="xl" weight="semibold">{last} <Text size="sm" color={change >= 0 ? 'success' : 'danger'}>{change >= 0 ? '+' : ''}{change}%</Text></Text>
          </VStack>
          <Sparkline data={data} variant={variant} accessibilityLabel={`${variant} trend`} style={{ flex: 1 }} />
        </HStack>
      ))}
    </VStack>
  );
}
```

## Example: a watchlist

An investing app's watchlist: each row has the last month's prices as a sparkline beside the price and the month's change. The line takes the change's color, so the list reads at a glance.

```tsx live wide
import { HStack, List, ListItem, Text, VStack } from 'patine-ui';
import { Sparkline } from '@patine-ui/pro';

const walk = (start: number, drift: number, seed: number) =>
  Array.from({ length: 30 }, (_, i) => Math.round((start + drift * i + Math.sin(i / 2 + seed) * start * 0.03) * 100) / 100);

const STOCKS = [
  { symbol: 'ASML', name: 'ASML Holding', prices: walk(640, 2.1, 1) },
  { symbol: 'NOVO', name: 'Novo Nordisk', prices: walk(98, -0.6, 2) },
  { symbol: 'SAP', name: 'SAP SE', prices: walk(212, 0.4, 3) },
  { symbol: 'ADYEN', name: 'Adyen', prices: walk(1380, -3.5, 4) },
];

export default function Example() {
  return (
    <List title="Watchlist" style={{ width: '100%', maxWidth: 520 }}>
      {STOCKS.map((s) => {
        const last = s.prices[s.prices.length - 1];
        const change = ((last - s.prices[0]) / s.prices[0]) * 100;
        const up = change >= 0;
        return (
          <ListItem
            key={s.symbol}
            title={s.symbol}
            description={s.name}
            showChevron={false}
            trailing={
              <HStack gap="md" align="center">
                <Sparkline data={s.prices} width={64} height={28} color={up ? '#16A34A' : '#DC2626'} accessibilityLabel={`${s.symbol}, last 30 days`} />
                <VStack align="flex-end" style={{ width: 92 }}>
                  <Text weight="medium">€{last.toFixed(2)}</Text>
                  <Text size="sm" color={up ? 'success' : 'danger'}>{up ? '+' : ''}{change.toFixed(1)}% · 1M</Text>
                </VStack>
              </HStack>
            }
          />
        );
      })}
    </List>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `data` | `(number \| null)[]` | — | The values in order; `null` breaks the line. |
| `variant` | `'line' \| 'area' \| 'bar'` | `'line'` | |
| `height` | `number` | `32` | |
| `width` | `number` | fills the container | |
| `color` | `string` | the theme's primary | |
| `area` | `boolean` | `false` | The same as `variant="area"`. |
| `curve` | `'monotone' \| 'linear' \| 'step' \| 'stepAfter' \| 'stepBefore'` | `'monotone'` | |
| `showLast` | `boolean` | `true` | A dot on the last value. |
| `valueFormatter` / `valueFormat` | | | For the summary. |
| `accessibilityLabel` | `string` | — | |
| `animate` | `boolean` | `true` | Grow in and glide to new values. |
