Chart
A composable chart container with tooltip and legend primitives for building data visualizations.
The Chart component provides a context-driven container for building custom charts in React Native. It includes a ChartContainer that distributes config via context, along with ChartTooltip, ChartLegend, and their content sub-components for consistent styling.
Preview
Installation
npx novaui-cli add chartImport
import {
ChartContainer,
ChartTooltip,
ChartTooltipContent,
ChartLegend,
ChartLegendContent,
useChart,
} from 'novaui-components'Basic Usage
The Chart is a compound component with these parts:
ChartContainer— Root wrapper that providesChartConfigcontext and appliesflex-1 justify-centerChartTooltip— Styled tooltip overlay withborder border-border bg-background shadow-sm px-3 py-2 rounded-lgChartTooltipContent— Inner tooltip layout withflex-col gap-1ChartLegend— Legend row withflex-row items-center justify-center gap-4 mt-4ChartLegendContent— Individual legend item withflex-row items-center gap-2useChart— Hook to access the currentChartConfigfrom context
import { View, Text } from 'react-native'
import {
ChartContainer,
ChartLegend,
ChartLegendContent,
} from 'novaui-components'
const config = {
revenue: {
label: 'Revenue',
color: '#3b82f6',
},
}
const data = [
{ month: 'Jan', value: 60 },
{ month: 'Feb', value: 80 },
{ month: 'Mar', value: 45 },
{ month: 'Apr', value: 90 },
{ month: 'May', value: 55 },
{ month: 'Jun', value: 70 },
]
export function BasicChart() {
return (
<ChartContainer config={config}>
<View className="flex-row items-end justify-between gap-2 h-48 px-4">
{data.map((item) => (
<View key={item.month} className="flex-col items-center gap-1 flex-1">
<View
className="w-full rounded-t-md"
style={{
height: `${item.value}%`,
backgroundColor: config.revenue.color,
}}
/>
<Text className="text-xs text-muted-foreground">{item.month}</Text>
</View>
))}
</View>
<ChartLegend>
<ChartLegendContent>
<View
className="h-3 w-3 rounded-sm"
style={{ backgroundColor: config.revenue.color }}
/>
<Text className="text-sm text-muted-foreground">Revenue</Text>
</ChartLegendContent>
</ChartLegend>
</ChartContainer>
)
}With Tooltip
Display contextual data using the tooltip primitives:
import { View, Text } from 'react-native'
import {
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from 'novaui-components'
export function ChartWithTooltip() {
return (
<ChartContainer config={config}>
{/* ... chart bars ... */}
<ChartTooltip>
<ChartTooltipContent>
<Text className="text-sm font-medium text-foreground">April</Text>
<View className="flex-row items-center gap-2">
<View
className="h-3 w-3 rounded-sm"
style={{ backgroundColor: config.revenue.color }}
/>
<Text className="text-sm text-muted-foreground">
Revenue: $9,000
</Text>
</View>
</ChartTooltipContent>
</ChartTooltip>
</ChartContainer>
)
}Multi-Series Chart
Show multiple data series with separate colors in the legend:
import { View, Text } from 'react-native'
import {
ChartContainer,
ChartLegend,
ChartLegendContent,
} from 'novaui-components'
const config = {
revenue: { label: 'Revenue', color: '#3b82f6' },
expenses: { label: 'Expenses', color: '#f97316' },
}
export function MultiSeriesChart() {
return (
<ChartContainer config={config}>
<View className="flex-row items-end justify-between gap-3 h-48 px-4">
{/* Render grouped bars per quarter */}
</View>
<ChartLegend>
<ChartLegendContent>
<View className="h-3 w-3 rounded-sm" style={{ backgroundColor: '#3b82f6' }} />
<Text className="text-sm text-muted-foreground">Revenue</Text>
</ChartLegendContent>
<ChartLegendContent>
<View className="h-3 w-3 rounded-sm" style={{ backgroundColor: '#f97316' }} />
<Text className="text-sm text-muted-foreground">Expenses</Text>
</ChartLegendContent>
</ChartLegend>
</ChartContainer>
)
}Props API
ChartConfig
The configuration object passed to ChartContainer:
| Key | Type | Default | Description |
|---|---|---|---|
[key] | object | - | Keyed by series name |
[key].label | string | - | Human-readable label for the series |
[key].color | string | - | Color used for bars, lines, or legend indicators |
[key].theme | Record<string, string> | - | Optional theme-aware color overrides |
ChartContainer Props
Extends React.ComponentPropsWithoutRef<typeof View>:
| Prop | Type | Default | Description |
|---|---|---|---|
config | ChartConfig | required | Chart configuration defining series labels and colors |
className | string | - | Additional CSS classes (base: flex-1 justify-center) |
ChartTooltip Props
Extends React.ComponentPropsWithoutRef<typeof View>:
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes (base: border border-border bg-background shadow-sm px-3 py-2 rounded-lg) |
ChartTooltipContent Props
Extends React.ComponentPropsWithoutRef<typeof View>:
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes (base: flex-col gap-1) |
ChartLegend Props
Extends React.ComponentPropsWithoutRef<typeof View>:
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes (base: flex-row items-center justify-center gap-4 mt-4) |
ChartLegendContent Props
Extends React.ComponentPropsWithoutRef<typeof View>:
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes (base: flex-row items-center gap-2) |
useChart Hook
Returns the current ChartConfig from context. Must be used within a ChartContainer.
const { config } = useChart()Best Practices
-
Define a clear config: Always provide a
ChartConfigwith descriptive labels and distinct colors per series for readability and accessibility. -
Use the context hook: Access chart config via
useChart()in custom chart elements rather than prop-drilling colors and labels. -
Keep tooltips concise: Show only the most relevant data point information — avoid cluttering with too many fields.
-
Legend placement: The
ChartLegenddefaults tomt-4below the chart. Override withclassNameif you need a different layout. -
Compose freely: These are low-level primitives — combine them with any charting library or custom bar/line views to build the visualization you need.
Related Components
- Use
Cardto wrap charts in a bordered container with a title - Use
Separatorto divide multiple charts in a dashboard - Use
Textfor axis labels and data annotations