mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 18:05:11 +03:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES ## What kind of change does this PR introduce? This is a first draft at introducing semantic colours to our Observability charts. This moves away from just random colours being assigned to prop after prop. They're only scoped to the Database reports right now, but if it flows nice, we can open it up to the other reports too. This also aims to tone down some of the harsher colours in our charts, such as the orange which sometimes can look like a warning metric/prop. | Before | After | |--------|--------| | <img width="839" height="336" alt="Screenshot 2026-06-10 at 09 14 56" src="https://github.com/user-attachments/assets/222747c5-973b-4165-aa53-df7b93412ad3" /> | <img width="950" height="341" alt="Screenshot 2026-09-14 at 18 14 47" src="https://github.com/user-attachments/assets/836f3ddd-4a97-4064-b8cf-3a3b435417ac" /> | cc @supabase/design for additional thoughts. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added semantic chart color roles with light and dark theme variants for consistent visualizations. * Standardized colors and fills across database, networking, storage, and connection charts. * Maximum-value lines now use configured chart colors when available. * Added chart palette reference and stress-test examples. * Added stacked bar charts, customizable margins, and gradient-filled line charts. * Improved multi-series bar chart focus and date-range footer alignment. * **Documentation** * Documented the chart palette, theme variants, accessibility guidance, and usage recommendations. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Gildas Garcia <1122076+djhi@users.noreply.github.com>
507 lines
13 KiB
Plaintext
507 lines
13 KiB
Plaintext
---
|
|
title: Chart
|
|
description: Beautiful charts. Built using Recharts. Copy and paste into your apps.
|
|
component: true
|
|
source:
|
|
shadcn: true
|
|
---
|
|
|
|
<ComponentPreview name="chart-bar-interactive" peekCode wide />
|
|
|
|
Introducing **Charts**. A collection of chart components that you can copy and paste into your apps.
|
|
|
|
Charts are designed to look great out of the box. They work well with the other components and are fully customizable to fit your project.
|
|
|
|
[Browse the Charts Library](/charts).
|
|
|
|
## Component
|
|
|
|
We use [Recharts](https://recharts.org/) under the hood.
|
|
|
|
We designed the `chart` component with composition in mind. **You build your charts using Recharts components and only bring in custom components, such as `ChartTootlip`, when and where you need it**.
|
|
|
|
```tsx showLineNumbers /ChartContainer/ /ChartTooltipContent/
|
|
import { Bar, BarChart } from 'recharts'
|
|
|
|
import { ChartContainer, ChartTooltipContent } from '@/components/ui/charts'
|
|
|
|
export function MyChart() {
|
|
return (
|
|
<ChartContainer>
|
|
<BarChart data={data}>
|
|
<Bar dataKey="value" />
|
|
<ChartTooltip content={<ChartTooltipContent />} />
|
|
</BarChart>
|
|
</ChartContainer>
|
|
)
|
|
}
|
|
```
|
|
|
|
We do not wrap Recharts. This means you're not locked into an abstraction. When a new Recharts version is released, you can follow the official upgrade path to upgrade your charts.
|
|
|
|
**The components are yours**.
|
|
|
|
## Installation
|
|
|
|
<Callout className="mt-4">
|
|
|
|
**Note:** If you are trying to use charts with **React 19** or the **Next.js 15**, you will need the [recharts@alpha](https://github.com/recharts/recharts/releases/tag/v2.13.0-alpha.4) release currently.
|
|
|
|
</Callout>
|
|
|
|
Chart colors are already defined for every app in `packages/config/css/charts.css`, which ships through the shared Tailwind config. It provides eight categorical slots, `--chart-1` through `--chart-8`, each with a matching `-fill` token, resolved per theme. See the [Charts](/docs/ui-patterns/charts) pattern page for the palette and the rules for assigning slots.
|
|
|
|
## Your First Chart
|
|
|
|
Let's build your first chart. We'll build a bar chart, add a grid, axis, tooltip and legend.
|
|
|
|
<Steps>
|
|
|
|
<Step>Start by defining your data</Step>
|
|
|
|
The following data represents the number of desktop and mobile users for each month.
|
|
|
|
<Callout className="mt-4">
|
|
|
|
**Note:** Your data can be in any shape. You are not limited to the shape of the data below. Use the `dataKey` prop to map your data to the chart.
|
|
|
|
</Callout>
|
|
|
|
```tsx
|
|
const chartData = [
|
|
{ month: 'January', desktop: 186, mobile: 80 },
|
|
{ month: 'February', desktop: 305, mobile: 200 },
|
|
{ month: 'March', desktop: 237, mobile: 120 },
|
|
{ month: 'April', desktop: 73, mobile: 190 },
|
|
{ month: 'May', desktop: 209, mobile: 130 },
|
|
{ month: 'June', desktop: 214, mobile: 140 },
|
|
]
|
|
```
|
|
|
|
<Step>Define your chart config</Step>
|
|
|
|
The chart config holds configuration for the chart. This is where you place human-readable strings, such as labels, icons and color tokens for theming.
|
|
|
|
```tsx
|
|
import { type ChartConfig } from '@/components/ui/chart'
|
|
|
|
const chartConfig = {
|
|
desktop: {
|
|
label: 'Desktop',
|
|
color: '#2563eb',
|
|
},
|
|
mobile: {
|
|
label: 'Mobile',
|
|
color: '#60a5fa',
|
|
},
|
|
} satisfies ChartConfig
|
|
```
|
|
|
|
<Step>Build your chart</Step>
|
|
|
|
You can now build your chart using Recharts components.
|
|
|
|
<Callout className="mt-4 bg-amber-50 border-amber-200 dark:bg-amber-950/50 dark:border-amber-950">
|
|
|
|
**Important:** Remember to set a `min-h-[VALUE]` on the `ChartContainer` component. This is required for the chart be responsive.
|
|
|
|
</Callout>
|
|
|
|
<ComponentSource name="chart-bar-demo" />
|
|
|
|
<ComponentPreview name="chart-bar-demo" className="[&_.preview]:p-4 [&_.preview]:min-h-[250px]" />
|
|
|
|
</Steps>
|
|
|
|
### Add a Grid
|
|
|
|
Let's add a grid to the chart.
|
|
|
|
<Steps>
|
|
|
|
<Step>Import the `CartesianGrid` component.</Step>
|
|
|
|
```tsx /CartesianGrid/
|
|
import { Bar, BarChart, CartesianGrid } from 'recharts'
|
|
```
|
|
|
|
<Step>Add the `CartesianGrid` component to your chart.</Step>
|
|
|
|
```tsx showLineNumbers {3}
|
|
<ChartContainer config={chartConfig} className="min-h-[200px] w-full">
|
|
<BarChart accessibilityLayer data={chartData}>
|
|
<CartesianGrid vertical={false} />
|
|
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
|
|
<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
|
|
</BarChart>
|
|
</ChartContainer>
|
|
```
|
|
|
|
<ComponentPreview
|
|
name="chart-bar-demo-grid"
|
|
className="[&_.preview]:p-4 [&_.preview]:min-h-[250px]"
|
|
/>
|
|
|
|
</Steps>
|
|
|
|
### Add an Axis
|
|
|
|
To add an x-axis to the chart, we'll use the `XAxis` component.
|
|
|
|
<Steps>
|
|
|
|
<Step>Import the `XAxis` component.</Step>
|
|
|
|
```tsx /XAxis/
|
|
import { Bar, BarChart, CartesianGrid, XAxis } from 'recharts'
|
|
```
|
|
|
|
<Step>Add the `XAxis` component to your chart.</Step>
|
|
|
|
```tsx showLineNumbers {4-10}
|
|
<ChartContainer config={chartConfig} className="h-[200px] w-full">
|
|
<BarChart accessibilityLayer data={chartData}>
|
|
<CartesianGrid vertical={false} />
|
|
<XAxis
|
|
dataKey="month"
|
|
tickLine={false}
|
|
tickMargin={10}
|
|
axisLine={false}
|
|
tickFormatter={(value) => value.slice(0, 3)}
|
|
/>
|
|
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
|
|
<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
|
|
</BarChart>
|
|
</ChartContainer>
|
|
```
|
|
|
|
<ComponentPreview
|
|
name="chart-bar-demo-axis"
|
|
className="[&_.preview]:p-4 [&_.preview]:min-h-[250px]"
|
|
/>
|
|
|
|
</Steps>
|
|
|
|
### Add Tooltip
|
|
|
|
So far we've only used components from Recharts. They look great out of the box thanks to some customization in the `chart` component.
|
|
|
|
To add a tooltip, we'll use the custom `ChartTooltip` and `ChartTooltipContent` components from `chart`.
|
|
|
|
<Steps>
|
|
|
|
<Step>Import the `ChartTooltip` and `ChartTooltipContent` components.</Step>
|
|
|
|
```tsx
|
|
import { ChartTooltip, ChartTooltipContent } from '@/components/ui/chart'
|
|
```
|
|
|
|
<Step>Add the components to your chart.</Step>
|
|
|
|
```tsx showLineNumbers {11}
|
|
<ChartContainer config={chartConfig} className="h-[200px] w-full">
|
|
<BarChart accessibilityLayer data={chartData}>
|
|
<CartesianGrid vertical={false} />
|
|
<XAxis
|
|
dataKey="month"
|
|
tickLine={false}
|
|
tickMargin={10}
|
|
axisLine={false}
|
|
tickFormatter={(value) => value.slice(0, 3)}
|
|
/>
|
|
<ChartTooltip content={<ChartTooltipContent />} />
|
|
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
|
|
<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
|
|
</BarChart>
|
|
</ChartContainer>
|
|
```
|
|
|
|
<ComponentPreview
|
|
name="chart-bar-demo-tooltip"
|
|
className="[&_.preview]:p-4 [&_.preview]:min-h-[250px]"
|
|
/>
|
|
|
|
Hover to see the tooltips. Easy, right? Two components, and we've got a beautiful tooltip.
|
|
|
|
</Steps>
|
|
|
|
### Add Legend
|
|
|
|
We'll do the same for the legend. We'll use the `ChartLegend` and `ChartLegendContent` components from `chart`.
|
|
|
|
<Steps>
|
|
|
|
<Step>Import the `ChartLegend` and `ChartLegendContent` components.</Step>
|
|
|
|
```tsx
|
|
import { ChartLegend, ChartLegendContent } from '@/components/ui/chart'
|
|
```
|
|
|
|
<Step>Add the components to your chart.</Step>
|
|
|
|
```tsx showLineNumbers {12}
|
|
<ChartContainer config={chartConfig} className="h-[200px] w-full">
|
|
<BarChart accessibilityLayer data={chartData}>
|
|
<CartesianGrid vertical={false} />
|
|
<XAxis
|
|
dataKey="month"
|
|
tickLine={false}
|
|
tickMargin={10}
|
|
axisLine={false}
|
|
tickFormatter={(value) => value.slice(0, 3)}
|
|
/>
|
|
<ChartTooltip content={<ChartTooltipContent />} />
|
|
<ChartLegend content={<ChartLegendContent />} />
|
|
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
|
|
<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
|
|
</BarChart>
|
|
</ChartContainer>
|
|
```
|
|
|
|
<ComponentPreview
|
|
name="chart-bar-demo-legend"
|
|
className="[&_.preview]:p-4 [&_.preview]:min-h-[250px]"
|
|
/>
|
|
|
|
</Steps>
|
|
|
|
Done. You've built your first chart! What's next?
|
|
|
|
- [Themes and Colors](/docs/components/chart#theming)
|
|
- [Tooltip](/docs/components/chart#tooltip)
|
|
- [Legend](/docs/components/chart#legend)
|
|
|
|
## Chart Config
|
|
|
|
The chart config is where you define the labels, icons and colors for a chart.
|
|
|
|
It is intentionally decoupled from chart data.
|
|
|
|
This allows you to share config and color tokens between charts. It can also works independently for cases where your data or color tokens live remotely or in a different format.
|
|
|
|
```tsx /ChartConfig/
|
|
import { Monitor } from 'lucide-react'
|
|
|
|
import { type ChartConfig } from '@/components/ui/chart'
|
|
|
|
const chartConfig = {
|
|
desktop: {
|
|
label: 'Desktop',
|
|
icon: Monitor,
|
|
// A color like 'hsl(220, 98%, 61%)' or 'var(--color-name)'
|
|
color: '#2563eb',
|
|
// OR a theme object with 'light' and 'dark' keys
|
|
theme: {
|
|
light: '#2563eb',
|
|
dark: '#dc2626',
|
|
},
|
|
},
|
|
} satisfies ChartConfig
|
|
```
|
|
|
|
## Theming
|
|
|
|
Charts has built-in support for theming. You can use css variables (recommended) or color values in any color format, such as hex, hsl or oklch.
|
|
|
|
### CSS Variables
|
|
|
|
<Steps>
|
|
|
|
<Step>Pick a slot from the shared palette</Step>
|
|
|
|
```css title="packages/config/css/charts.css"
|
|
:root {
|
|
--chart-1: var(--color-brand-800);
|
|
--chart-2: var(--color-blue-900);
|
|
/* ... */
|
|
}
|
|
|
|
[data-theme*='dark'] {
|
|
--chart-1: var(--color-brand-900);
|
|
--chart-2: var(--color-blue-1100);
|
|
/* ... */
|
|
}
|
|
```
|
|
|
|
<Step>Add the color to your `chartConfig`</Step>
|
|
|
|
```tsx {4,8}
|
|
const chartConfig = {
|
|
desktop: {
|
|
label: 'Desktop',
|
|
color: 'var(--chart-1)',
|
|
},
|
|
mobile: {
|
|
label: 'Mobile',
|
|
color: 'var(--chart-2)',
|
|
},
|
|
} satisfies ChartConfig
|
|
```
|
|
|
|
<Callout className="mt-4">
|
|
|
|
The slots are full color values, so pass them as `var(--chart-1)`. Do not wrap them in `hsl()`; that form is for bare HSL triplets and produces an invalid color here.
|
|
|
|
</Callout>
|
|
|
|
</Steps>
|
|
|
|
### hex, hsl or oklch
|
|
|
|
You can also define your colors directly in the chart config. Use the color format you prefer.
|
|
|
|
```tsx
|
|
const chartConfig = {
|
|
desktop: {
|
|
label: 'Desktop',
|
|
color: '#2563eb',
|
|
},
|
|
} satisfies ChartConfig
|
|
```
|
|
|
|
### Using Colors
|
|
|
|
To use the theme colors in your chart, reference the colors using the format `var(--color-KEY)`.
|
|
|
|
#### Components
|
|
|
|
```tsx
|
|
<Bar dataKey="desktop" fill="var(--color-desktop)" />
|
|
```
|
|
|
|
#### Chart Data
|
|
|
|
```tsx
|
|
const chartData = [
|
|
{ browser: 'chrome', visitors: 275, fill: 'var(--color-chrome)' },
|
|
{ browser: 'safari', visitors: 200, fill: 'var(--color-safari)' },
|
|
]
|
|
```
|
|
|
|
#### Tailwind
|
|
|
|
```tsx
|
|
<LabelList className="fill-(--color-desktop)" />
|
|
```
|
|
|
|
## Tooltip
|
|
|
|
A chart tooltip contains a label, name, indicator and value. You can use a combination of these to customize your tooltip.
|
|
|
|
<ComponentPreview name="chart-tooltip-demo" className="[&_.preview]:py-0" hideCode />
|
|
|
|
You can turn on/off any of these using the `hideLabel`, `hideIndicator` props and customize the indicator style using the `indicator` prop.
|
|
|
|
Use `labelKey` and `nameKey` to use a custom key for the tooltip label and name.
|
|
|
|
Chart comes with the `<ChartTooltip>` and `<ChartTooltipContent>` components. You can use these two components to add custom tooltips to your chart.
|
|
|
|
```tsx
|
|
import { ChartTooltip, ChartTooltipContent } from '@/components/ui/chart'
|
|
```
|
|
|
|
```tsx
|
|
<ChartTooltip content={<ChartTooltipContent />} />
|
|
```
|
|
|
|
### Props
|
|
|
|
Use the following props to customize the tooltip.
|
|
|
|
| Prop | Type | Description |
|
|
| :-------------- | :----------------------- | :------------------------------------------- |
|
|
| `labelKey` | string | The config or data key to use for the label. |
|
|
| `nameKey` | string | The config or data key to use for the name. |
|
|
| `indicator` | `dot` `line` or `dashed` | The indicator style for the tooltip. |
|
|
| `hideLabel` | boolean | Whether to hide the label. |
|
|
| `hideIndicator` | boolean | Whether to hide the indicator. |
|
|
|
|
### Colors
|
|
|
|
Colors are automatically referenced from the chart config.
|
|
|
|
### Custom
|
|
|
|
To use a custom key for tooltip label and names, use the `labelKey` and `nameKey` props.
|
|
|
|
```tsx /browser/
|
|
const chartData = [
|
|
{ browser: 'chrome', visitors: 187, fill: 'var(--color-chrome)' },
|
|
{ browser: 'safari', visitors: 200, fill: 'var(--color-safari)' },
|
|
]
|
|
|
|
const chartConfig = {
|
|
visitors: {
|
|
label: 'Total Visitors',
|
|
},
|
|
chrome: {
|
|
label: 'Chrome',
|
|
color: 'var(--chart-1)',
|
|
},
|
|
safari: {
|
|
label: 'Safari',
|
|
color: 'var(--chart-2)',
|
|
},
|
|
} satisfies ChartConfig
|
|
```
|
|
|
|
```tsx
|
|
<ChartTooltip content={<ChartTooltipContent labelKey="visitors" nameKey="browser" />} />
|
|
```
|
|
|
|
This will use `Total Visitors` for label and `Chrome` and `Safari` for the tooltip names.
|
|
|
|
## Legend
|
|
|
|
You can use the custom `<ChartLegend>` and `<ChartLegendContent>` components to add a legend to your chart.
|
|
|
|
```tsx
|
|
import { ChartLegend, ChartLegendContent } from '@/components/ui/chart'
|
|
```
|
|
|
|
```tsx
|
|
<ChartLegend content={<ChartLegendContent />} />
|
|
```
|
|
|
|
### Colors
|
|
|
|
Colors are automatically referenced from the chart config.
|
|
|
|
### Custom
|
|
|
|
To use a custom key for legend names, use the `nameKey` prop.
|
|
|
|
```tsx /browser/
|
|
const chartData = [
|
|
{ browser: 'chrome', visitors: 187, fill: 'var(--color-chrome)' },
|
|
{ browser: 'safari', visitors: 200, fill: 'var(--color-safari)' },
|
|
]
|
|
|
|
const chartConfig = {
|
|
chrome: {
|
|
label: 'Chrome',
|
|
color: 'var(--chart-1)',
|
|
},
|
|
safari: {
|
|
label: 'Safari',
|
|
color: 'var(--chart-2)',
|
|
},
|
|
} satisfies ChartConfig
|
|
```
|
|
|
|
```tsx
|
|
<ChartLegend content={<ChartLegendContent nameKey="browser" />} />
|
|
```
|
|
|
|
This will use `Chrome` and `Safari` for the legend names.
|
|
|
|
## Accessibility
|
|
|
|
You can turn on the `accessibilityLayer` prop to add an accessible layer to your chart.
|
|
|
|
This prop adds keyboard access and screen reader support to your charts.
|
|
|
|
```tsx
|
|
<LineChart accessibilityLayer />
|
|
```
|