# Chart

> Draws numbers over time or across categories, with a table of the same numbers.

A chart shows how numbers move over time or compare across categories, such as monthly streams per platform or royalty income.

## When to use

- **Bar** to compare amounts across a few categories or months.
- **Line** for one or two measures over many points in time, such as income per month.
- **Area** only for a single total over time; stacked areas are hard to read.
- Use at most 5 series. Fold the rest into "Lainnya", or split the chart.
- With 4 or more series, label each line or bar group directly, not only in the legend.
- In scatter plots and maps, where any color can sit next to any other, use at most 3 series.
- One chart, one axis. Two measures on different scales get two charts.
- A single headline number is a stat card, not a chart.

_Live preview of the React and Vue versions: https://design.sosialoka.tech/components/chart/_

_Live preview of the React and Vue versions: https://design.sosialoka.tech/components/chart/_

## Anatomy

1. Bar series in chart-1
2. Line series in chart-2
3. Axis

A [Card](https://design.sosialoka.tech/components/card/) with a title and a short description, the chart, a legend when there are two or more series, and a visually hidden table of the same numbers.

| Part | Style |
| --- | --- |
| Series | `chart-1` to `chart-5`, in order. `chart-1` is the product's own hue. Never a status color. |
| Grid | Horizontal lines only, in `border`. No axis lines. |
| Axis labels | `text-xs`, `muted-foreground`. |
| Bars | 4px rounded tops, 2px apart. |
| Lines | 2px, no dots until hover. |
| Tooltip | The popover surface: `rounded-lg`, `border`, `bg-popover`, `shadow-lg`. |
| Numbers | `tabular-nums`, in the product's locale (`12,400` in English, `12.400` in Indonesian), Rupiah with two decimals (`Rp 1.890.420,25`). |

The chart colors are tokens with 3:1 contrast on `background` and `card`. Neighboring series, in their order, stay apart for color-blind readers; colors further apart in the order can look alike, which is why 4 or more series need direct labels. See [Color](https://design.sosialoka.tech/foundations/color/#chart-palette).

## Props

| React | Vue | Flutter | Type | Default | Use |
| --- | --- | --- | --- | --- | --- |
| `ChartContainer config` | `ChartContainer :config` | colors from `context.sosialoka.chart1`… | `ChartConfig` | none | Each series' label and color, such as `{ color: "var(--chart-1)" }`. |
| Recharts children | Unovis children | `fl_chart` `BarChart`, `LineChart` | components | none | The chart itself. |
| `ChartTooltipContent formatter` | `formatter` | `getTooltipItem` | function | numbers in `locale` | Formats a value, such as Rupiah. |
| `ChartTooltipContent locale`, `ChartTable locale` | `locale` | `NumberFormat` locale | text | `en-GB` | How numbers read when there is no formatter, such as `"id-ID"` for `12.400`. |
| `ChartTable caption labelKey labelHeader data config format` | same | `Semantics label` | | none | The same numbers for screen readers. Always add it. |

## Code

**React**
```tsx
import { Bar, BarChart, CartesianGrid, XAxis, YAxis } from "recharts"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import {
  ChartContainer,
  ChartLegend,
  ChartLegendContent,
  ChartTable,
  ChartTooltip,
  ChartTooltipContent,
  type ChartConfig,
} from "@/components/ui/chart"

const data = [
  { month: "Jun", spotify: 8200, youtube: 5100 },
  { month: "Jul", spotify: 9100, youtube: 5600 },
  { month: "Agu", spotify: 9800, youtube: 6900 },
  { month: "Sep", spotify: 10600, youtube: 7300 },
  { month: "Okt", spotify: 11300, youtube: 8100 },
  { month: "Nov", spotify: 12400, youtube: 8800 },
]

const config = {
  spotify: { label: "Spotify", color: "var(--chart-1)" },
  youtube: { label: "YouTube Music", color: "var(--chart-2)" },
} satisfies ChartConfig

const compact = (value: number) => new Intl.NumberFormat("id-ID", { notation: "compact" }).format(value)

export default function ChartDemo() {
  return (
    <Card className="w-full max-w-xl">
      <CardHeader>
        <CardTitle>Streaming per bulan</CardTitle>
        <CardDescription>Jun–Nov 2026, semua rilis</CardDescription>
      </CardHeader>
      <CardContent>
        <ChartContainer config={config} className="aspect-auto h-64 w-full">
          <BarChart data={data} barGap={2}>
            <CartesianGrid vertical={false} />
            <XAxis dataKey="month" tickLine={false} axisLine={false} tickMargin={8} />
            <YAxis tickLine={false} axisLine={false} tickMargin={8} width={48} tickFormatter={compact} />
            <ChartTooltip cursor={false} content={<ChartTooltipContent locale="id-ID" />} />
            <ChartLegend content={<ChartLegendContent />} />
            <Bar dataKey="spotify" fill="var(--color-spotify)" radius={[4, 4, 0, 0]} maxBarSize={20} />
            <Bar dataKey="youtube" fill="var(--color-youtube)" radius={[4, 4, 0, 0]} maxBarSize={20} />
          </BarChart>
        </ChartContainer>
        <ChartTable locale="id-ID" caption="Streaming per bulan, Jun–Nov 2026" labelHeader="Bulan" labelKey="month" data={data} config={config} />
      </CardContent>
    </Card>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import type { ChartConfig } from "@/components/ui/chart"
import { VisAxis, VisGroupedBar, VisXYContainer } from "@unovis/vue"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import {
  ChartContainer,
  ChartCrosshair,
  ChartLegendContent,
  ChartTable,
  ChartTooltip,
  ChartTooltipContent,
  componentToString,
} from "@/components/ui/chart"

const data = [
  { month: "Jun", spotify: 8200, youtube: 5100 },
  { month: "Jul", spotify: 9100, youtube: 5600 },
  { month: "Agu", spotify: 9800, youtube: 6900 },
  { month: "Sep", spotify: 10600, youtube: 7300 },
  { month: "Okt", spotify: 11300, youtube: 8100 },
  { month: "Nov", spotify: 12400, youtube: 8800 },
]
type Row = (typeof data)[number]

const config = {
  spotify: { label: "Spotify", color: "var(--chart-1)" },
  youtube: { label: "YouTube Music", color: "var(--chart-2)" },
} satisfies ChartConfig

const compact = (value: number) => new Intl.NumberFormat("id-ID", { notation: "compact" }).format(value)
</script>

<template>
  <Card class="w-full max-w-xl">
    <CardHeader>
      <CardTitle>Streaming per bulan</CardTitle>
      <CardDescription>Jun–Nov 2026, semua rilis</CardDescription>
    </CardHeader>
    <CardContent>
      <ChartContainer :config="config" class="aspect-auto h-64 w-full">
        <VisXYContainer :data="data" :padding="{ top: 8 }">
          <VisGroupedBar
            :x="(_: Row, i: number) => i"
            :y="[(d: Row) => d.spotify, (d: Row) => d.youtube]"
            :color="[config.spotify.color, config.youtube.color]"
            :rounded-corners="4"
            :bar-padding="0.3"
            :group-max-width="44"
          />
          <VisAxis type="x" :x="(_: Row, i: number) => i" :tick-format="(i: number) => data[i]?.month" :num-ticks="6" :grid-line="false" :tick-line="false" :domain-line="false" />
          <VisAxis type="y" :num-ticks="4" :tick-format="compact" :tick-line="false" :domain-line="false" />
          <ChartTooltip />
          <ChartCrosshair :template="componentToString(config, ChartTooltipContent, { labelKey: 'month', locale: 'id-ID' })" color="transparent" />
        </VisXYContainer>
        <ChartLegendContent />
      </ChartContainer>
      <ChartTable locale="id-ID" caption="Streaming per bulan, Jun–Nov 2026" label-header="Bulan" label-key="month" :data="data" :config="config" />
    </CardContent>
  </Card>
</template>
```

**Flutter**
```dart
// Charts come from fl_chart, colored with the chart palette of the Sosialoka theme (context.sosialoka.chart1…5).
// The bars are hidden from screen readers; a Semantics label carries the numbers instead.
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:sosialoka_ui/sosialoka_ui.dart';

const _months = ['Jun', 'Jul', 'Agu', 'Sep', 'Okt', 'Nov'];
const _spotify = [8200, 9100, 9800, 10600, 11300, 12400];
const _youtube = [5100, 5600, 6900, 7300, 8100, 8800];

class ChartDemo extends StatelessWidget {
  const ChartDemo({super.key});

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);
    final c = context.sosialoka;
    final number = NumberFormat.decimalPattern('id');
    final summary = [
      for (var i = 0; i < _months.length; i++)
        '${_months[i]}: Spotify ${number.format(_spotify[i])}, YouTube Music ${number.format(_youtube[i])}',
    ].join('; ');
    Widget legend(Color color, String label) => Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          width: 10,
          height: 10,
          decoration: BoxDecoration(color: color, borderRadius: BorderRadius.circular(2)),
        ),
        const SizedBox(width: 6),
        Text(label, style: theme.textTheme.muted),
      ],
    );
    return ShadCard(
      width: 576,
      title: Text('Streaming per bulan', style: theme.textTheme.h4),
      description: const Text('Jun–Nov 2026, semua rilis'),
      child: Padding(
        padding: const EdgeInsets.only(top: 16),
        child: Column(
          children: [
            Semantics(
              label: 'Streaming per bulan, Jun–Nov 2026. $summary.',
              child: ExcludeSemantics(
                child: SizedBox(
                  height: 220,
                  child: BarChart(
                    BarChartData(
                      // The tooltip sits on the popover surface, like the web charts.
                      barTouchData: BarTouchData(
                        touchTooltipData: BarTouchTooltipData(
                          getTooltipColor: (_) => theme.colorScheme.popover,
                          tooltipBorder: BorderSide(color: theme.colorScheme.border),
                          getTooltipItem: (group, _, rod, rodIndex) => BarTooltipItem(
                            '${rodIndex == 0 ? 'Spotify' : 'YouTube Music'} ${number.format(rod.toY)}',
                            theme.textTheme.small.copyWith(color: theme.colorScheme.popoverForeground),
                          ),
                        ),
                      ),
                      gridData: FlGridData(
                        drawVerticalLine: false,
                        getDrawingHorizontalLine: (_) => FlLine(color: theme.colorScheme.border, strokeWidth: 1),
                      ),
                      borderData: FlBorderData(show: false),
                      titlesData: FlTitlesData(
                        topTitles: const AxisTitles(),
                        rightTitles: const AxisTitles(),
                        leftTitles: AxisTitles(
                          sideTitles: SideTitles(
                            showTitles: true,
                            reservedSize: 44,
                            getTitlesWidget: (value, meta) => Text(
                              NumberFormat.compact(locale: 'id').format(value),
                              style: theme.textTheme.muted,
                            ),
                          ),
                        ),
                        bottomTitles: AxisTitles(
                          sideTitles: SideTitles(
                            showTitles: true,
                            getTitlesWidget: (value, meta) => Text(_months[value.toInt()], style: theme.textTheme.muted),
                          ),
                        ),
                      ),
                      barGroups: [
                        for (var i = 0; i < _months.length; i++)
                          BarChartGroupData(
                            x: i,
                            barsSpace: 2,
                            barRods: [
                              BarChartRodData(
                                toY: _spotify[i].toDouble(),
                                color: c.chart1,
                                width: 14,
                                borderRadius: const BorderRadius.vertical(top: Radius.circular(4)),
                              ),
                              BarChartRodData(
                                toY: _youtube[i].toDouble(),
                                color: c.chart2,
                                width: 14,
                                borderRadius: const BorderRadius.vertical(top: Radius.circular(4)),
                              ),
                            ],
                          ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
            const SizedBox(height: 12),
            Wrap(spacing: 16, children: [legend(c.chart1, 'Spotify'), legend(c.chart2, 'YouTube Music')]),
          ],
        ),
      ),
    );
  }
}
```

### Line chart

**React**
```tsx
import { CartesianGrid, Line, LineChart, XAxis, YAxis } from "recharts"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { ChartContainer, ChartTable, ChartTooltip, ChartTooltipContent, type ChartConfig } from "@/components/ui/chart"

const data = [
  { month: "Jun", revenue: 1250000 },
  { month: "Jul", revenue: 1418500.5 },
  { month: "Agu", revenue: 1377250 },
  { month: "Sep", revenue: 1602000.75 },
  { month: "Okt", revenue: 1745300 },
  { month: "Nov", revenue: 1890420.25 },
]

const config = { revenue: { label: "Pendapatan" , color: "var(--chart-1)" } } satisfies ChartConfig

const rupiah = (value: unknown) =>
  new Intl.NumberFormat("id-ID", { style: "currency", currency: "IDR", minimumFractionDigits: 2 }).format(Number(value))
const compact = (value: number) => new Intl.NumberFormat("id-ID", { notation: "compact" }).format(value)

export default function ChartLineDemo() {
  return (
    <Card className="w-full max-w-xl">
      <CardHeader>
        <CardTitle>Pendapatan royalti</CardTitle>
        <CardDescription>Jun–Nov 2026, dalam Rupiah</CardDescription>
      </CardHeader>
      <CardContent>
        <ChartContainer config={config} className="aspect-auto h-56 w-full">
          <LineChart data={data} margin={{ right: 8 }}>
            <CartesianGrid vertical={false} />
            <XAxis dataKey="month" tickLine={false} axisLine={false} tickMargin={8} />
            <YAxis tickLine={false} axisLine={false} tickMargin={8} width={48} tickFormatter={compact} />
            <ChartTooltip cursor={false} content={<ChartTooltipContent formatter={(value) => rupiah(value)} hideIndicator />} />
            <Line dataKey="revenue" type="monotone" stroke="var(--color-revenue)" strokeWidth={2} dot={false} activeDot={{ r: 4, strokeWidth: 2 }} />
          </LineChart>
        </ChartContainer>
        <ChartTable caption="Pendapatan royalti, Jun–Nov 2026" labelHeader="Bulan" labelKey="month" data={data} config={config} format={rupiah} />
      </CardContent>
    </Card>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import type { ChartConfig } from "@/components/ui/chart"
import { VisAxis, VisLine, VisXYContainer } from "@unovis/vue"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { ChartContainer, ChartCrosshair, ChartTable, ChartTooltip, ChartTooltipContent, componentToString } from "@/components/ui/chart"

const data = [
  { month: "Jun", revenue: 1250000 },
  { month: "Jul", revenue: 1418500.5 },
  { month: "Agu", revenue: 1377250 },
  { month: "Sep", revenue: 1602000.75 },
  { month: "Okt", revenue: 1745300 },
  { month: "Nov", revenue: 1890420.25 },
]
type Row = (typeof data)[number]

const config = { revenue: { label: "Pendapatan", color: "var(--chart-1)" } } satisfies ChartConfig

const rupiah = (value: unknown) =>
  new Intl.NumberFormat("id-ID", { style: "currency", currency: "IDR", minimumFractionDigits: 2 }).format(Number(value))
const compact = (value: number) => new Intl.NumberFormat("id-ID", { notation: "compact" }).format(value)
</script>

<template>
  <Card class="w-full max-w-xl">
    <CardHeader>
      <CardTitle>Pendapatan royalti</CardTitle>
      <CardDescription>Jun–Nov 2026, dalam Rupiah</CardDescription>
    </CardHeader>
    <CardContent>
      <ChartContainer :config="config" class="aspect-auto h-56 w-full" cursor>
        <VisXYContainer :data="data" :padding="{ top: 8, right: 8 }">
          <VisLine :x="(_: Row, i: number) => i" :y="(d: Row) => d.revenue" :color="config.revenue.color" :line-width="2" />
          <VisAxis type="x" :x="(_: Row, i: number) => i" :tick-format="(i: number) => data[i]?.month" :num-ticks="6" :grid-line="false" :tick-line="false" :domain-line="false" />
          <VisAxis type="y" :num-ticks="4" :tick-format="compact" :tick-line="false" :domain-line="false" />
          <ChartTooltip />
          <ChartCrosshair :template="componentToString(config, ChartTooltipContent, { labelKey: 'month', hideIndicator: true, formatter: rupiah })" :color="config.revenue.color" />
        </VisXYContainer>
      </ChartContainer>
      <ChartTable caption="Pendapatan royalti, Jun–Nov 2026" label-header="Bulan" label-key="month" :data="data" :config="config" :format="rupiah" />
    </CardContent>
  </Card>
</template>
```

**Flutter**
```dart
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:sosialoka_ui/sosialoka_ui.dart';

const revenue = [
  (month: 'Jun', value: 1250000.0),
  (month: 'Jul', value: 1418500.5),
  (month: 'Agu', value: 1377250.0),
  (month: 'Sep', value: 1602000.75),
  (month: 'Okt', value: 1745300.0),
  (month: 'Nov', value: 1890420.25),
];

// One series in chart-1, with Rupiah to two decimals. Screen readers get the same numbers as one sentence.
class ChartLineDemo extends StatelessWidget {
  const ChartLineDemo({super.key});

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);
    final c = context.sosialoka;
    final rupiah = NumberFormat.currency(locale: 'id', symbol: 'Rp', decimalDigits: 2);
    final compact = NumberFormat.compact(locale: 'id');
    final axis = theme.textTheme.muted.copyWith(fontSize: 12);
    final summary = [
      'Pendapatan royalti, Jun–Nov 2026.',
      for (final r in revenue) '${r.month}: ${rupiah.format(r.value)}.',
    ].join(' ');

    return ShadCard(
      width: 576,
      title: Text('Pendapatan royalti', style: theme.textTheme.h4),
      description: const Text('Jun–Nov 2026, dalam Rupiah'),
      child: Semantics(
        label: summary,
        child: ExcludeSemantics(
          child: Padding(
            padding: const EdgeInsets.only(top: 16),
            child: SizedBox(
              height: 220,
              child: LineChart(
                LineChartData(
                  minY: 1000000,
                  maxY: 2000000,
                  lineBarsData: [
                    LineChartBarData(
                      spots: [for (var i = 0; i < revenue.length; i++) FlSpot(i.toDouble(), revenue[i].value)],
                      isCurved: true,
                      color: c.chart1,
                      barWidth: 2,
                      dotData: const FlDotData(show: false),
                    ),
                  ],
                  gridData: FlGridData(
                    drawVerticalLine: false,
                    horizontalInterval: 250000,
                    getDrawingHorizontalLine: (_) => FlLine(color: theme.colorScheme.border, strokeWidth: 1),
                  ),
                  borderData: FlBorderData(show: false),
                  titlesData: FlTitlesData(
                    topTitles: const AxisTitles(),
                    rightTitles: const AxisTitles(),
                    bottomTitles: AxisTitles(
                      sideTitles: SideTitles(
                        showTitles: true,
                        interval: 1,
                        getTitlesWidget: (value, meta) => SideTitleWidget(meta: meta, child: Text(revenue[value.toInt()].month, style: axis)),
                      ),
                    ),
                    leftTitles: AxisTitles(
                      sideTitles: SideTitles(
                        showTitles: true,
                        reservedSize: 44,
                        interval: 250000,
                        getTitlesWidget: (value, meta) => SideTitleWidget(meta: meta, child: Text(compact.format(value), style: axis)),
                      ),
                    ),
                  ),
                  lineTouchData: LineTouchData(
                    touchTooltipData: LineTouchTooltipData(
                      getTooltipColor: (_) => theme.colorScheme.popover,
                      tooltipBorder: BorderSide(color: theme.colorScheme.border),
                      getTooltipItems: (spots) => [
                        for (final s in spots)
                          LineTooltipItem(rupiah.format(s.y), theme.textTheme.small.copyWith(color: theme.colorScheme.popoverForeground)),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
```

## Install

**React**
```bash
npx shadcn@latest add https://design.sosialoka.tech/r/react/chart.json
```

**Vue**
```bash
npx shadcn-vue@latest add https://design.sosialoka.tech/r/vue/chart.json
```

React draws with Recharts and Vue with Unovis; Flutter uses `fl_chart`.

## Accessibility

- Every chart has a text alternative: `ChartTable` renders a visually hidden table with the same numbers, and Flutter wraps the chart in a `Semantics` label that reads them.
- The drawing is hidden from screen readers and kept out of the tab order, so the table is the one source.
- The tooltip is never the only place a number lives.
- A legend names the series whenever there are two or more, so color is never the only way to tell them apart.

## Migrating from ApexCharts

Cover Clearance draws its charts with ApexCharts today. When a screen moves to this design system, its charts move to Chart: same data, the `chart-n` colors, and a `ChartTable`.
