# Chart Primitive

Use Chart to draw bar, line, area, pie, radar and radial charts from your data and series config. Add parts such as Bar, XAxis and Grid to describe the chart. For a chart that the parts cannot draw, pass a TanStack Charts chart module. People can read values in a tooltip with the pointer or the keyboard, and the legend can hide a series.
## Anatomy
### Astro
Use the Astro primitive adapter to render Chart anatomy with the Runtime wiring included.
```astro
---
import { Chart } from "@starwind-ui/astro/chart";
import type { ChartConfig } from "@starwind-ui/astro/chart";

type VisitorRow = { month: string; desktop: number; mobile: number };

const data: VisitorRow[] = [
  { month: "January", desktop: 186, mobile: 80 },
  { month: "February", desktop: 305, mobile: 200 },
  { month: "March", desktop: 237, mobile: 120 },
];

const config = {
  desktop: { label: "Desktop", color: "var(--chart-1)" },
  mobile: { label: "Mobile", color: "var(--chart-2)" },
} satisfies ChartConfig;
---

<Chart.Root data={data} config={config}>
  <Chart.Style />
  <Chart.Surface />
  <Chart.Grid vertical={false} />
  <Chart.XAxis dataKey="month" tickFormatter={{ maxLength: 3 }} />
  <Chart.Bar dataKey="desktop" radius={4} />
  <Chart.Bar dataKey="mobile" radius={4} />
</Chart.Root>
```

### React
Use the React primitive adapter when Chart state participates in React rendering.
```tsx
import { Chart } from "@starwind-ui/react/chart";
import type { ChartConfig } from "@starwind-ui/react/chart";

type VisitorRow = { month: string; desktop: number; mobile: number };

const data: VisitorRow[] = [
  { month: "January", desktop: 186, mobile: 80 },
  { month: "February", desktop: 305, mobile: 200 },
  { month: "March", desktop: 237, mobile: 120 },
];

const config = {
  desktop: { label: "Desktop", color: "var(--chart-1)" },
  mobile: { label: "Mobile", color: "var(--chart-2)" },
} satisfies ChartConfig;

export function Example() {
  return (
    <Chart.Root data={data} config={config}>
      <Chart.Style />
      <Chart.Surface />
      <Chart.Grid vertical={false} />
      <Chart.XAxis dataKey="month" tickFormatter={{ maxLength: 3 }} />
      <Chart.Bar dataKey="desktop" radius={4} />
      <Chart.Bar dataKey="mobile" radius={4} />
    </Chart.Root>
  );
}
```

### HTML
Render the Chart data-sw-* contract yourself, then initialize createChart.
```html
<!-- data-chart holds the chart id. The style element receives the config colors. -->
<div data-sw-chart data-slot="chart" data-chart="visitors">
  <style data-slot="chart-style"></style>
  <div data-sw-chart-surface data-slot="chart-surface"></div>
</div>

<script type="module">
  import { createChart } from "@starwind-ui/runtime/chart";

  const data = [
    { month: "January", desktop: 186, mobile: 80 },
    { month: "February", desktop: 305, mobile: 200 },
    { month: "March", desktop: 237, mobile: 120 },
  ];

  const config = {
    desktop: { label: "Desktop", color: "var(--chart-1)" },
    mobile: { label: "Mobile", color: "var(--chart-2)" },
  };

  // The spec lists the chart parts as plain data. It needs no chart module.
  const spec = {
    parts: [
      { kind: "grid", vertical: false },
      { kind: "xAxis", dataKey: "month", tickFormatter: { maxLength: 3 } },
      { kind: "bar", dataKey: "desktop", radius: 4 },
      { kind: "bar", dataKey: "mobile", radius: 4 },
    ],
  };

  const root = document.querySelector("[data-sw-chart]");
  if (root) createChart(root, { input: { data, config, spec } });
</script>
```

### Svelte
Use the Svelte components to build Chart.
```svelte
<script lang="ts">
  import { Chart } from "@starwind-ui/svelte/chart";
  import type { ChartConfig } from "@starwind-ui/svelte/chart";

  type VisitorRow = { month: string; desktop: number; mobile: number };

  const data: VisitorRow[] = [
    { month: "January", desktop: 186, mobile: 80 },
    { month: "February", desktop: 305, mobile: 200 },
    { month: "March", desktop: 237, mobile: 120 },
  ];

  const config = {
    desktop: { label: "Desktop", color: "var(--chart-1)" },
    mobile: { label: "Mobile", color: "var(--chart-2)" },
  } satisfies ChartConfig;
</script>

<Chart.Root {data} {config}>
  <Chart.Style />
  <Chart.Surface />
  <Chart.Grid vertical={false} />
  <Chart.XAxis dataKey="month" tickFormatter={{ maxLength: 3 }} />
  <Chart.Bar dataKey="desktop" radius={4} />
  <Chart.Bar dataKey="mobile" radius={4} />
</Chart.Root>
```

### Vue
Use the Vue components to build Chart.
```vue
<script setup lang="ts">
import Chart from "@starwind-ui/vue/chart";
import type { ChartConfig } from "@starwind-ui/vue/chart";

type VisitorRow = { month: string; desktop: number; mobile: number };

const data: VisitorRow[] = [
  { month: "January", desktop: 186, mobile: 80 },
  { month: "February", desktop: 305, mobile: 200 },
  { month: "March", desktop: 237, mobile: 120 },
];

const config = {
  desktop: { label: "Desktop", color: "var(--chart-1)" },
  mobile: { label: "Mobile", color: "var(--chart-2)" },
} satisfies ChartConfig;
</script>

<template>
  <Chart.Root :data="data" :config="config">
    <Chart.Style />
    <Chart.Surface />
    <Chart.Grid :vertical="false" />
    <Chart.XAxis data-key="month" :tick-formatter="{ maxLength: 3 }" />
    <Chart.Bar data-key="desktop" :radius="4" />
    <Chart.Bar data-key="mobile" :radius="4" />
  </Chart.Root>
</template>
```
Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed package's types for the available props and events. Plain HTML uses DOM events and Runtime methods.
## API Reference
### Root
The main element that owns the Chart Runtime instance.
| Fact | Value |
| --- | --- |
| Default element | `div` |
| Discovery hook | `data-sw-chart` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| id | `string` | - | attribute | Names the chart. Leave it out to get a generated id that matches on server and client. | - |
| chart | `ChartSource<T, D, X, Y>` | - | option | The chart module or factory that draws the chart from the data and the visible keys. | - |
| definition | `DomChartDefinition<D, X, Y>` | - | option | A static native TanStack definition. It owns its visibility, so the legend is passive. | - |
| data | `readonly T[]` | - | option | The data rows that the chart module reads. | - |
| config | `ChartConfig` | - | option | The series labels, colors and icons, keyed by series. | - |
| params | `Record<string, unknown>` | - | option | Extra values that the chart module reads. | - |
| theme | `ChartInput["theme"]` | - | option | Overrides the native TanStack chart theme. | - |
| host | `NativeHostOptions<D, X, Y>` | - | option | Native TanStack host options, such as the focus and select callbacks. | - |
| disableTooltip | `boolean` | false | option | Turns the tooltip off. | - |
| stackOffset | `"none" \| "expand"` | "none" | option | How the stacked bars and areas of a parts chart stack. `"expand"` stacks each category to 100%, and the tooltip keeps the raw values. | - |
| visible | `readonly string[]` | - | control | Controls the visible state for Chart. | **React:** Use visible for controlled state and defaultVisible for default state, and onVisibleChange for change proposals.<br>**Astro:** Use visible or defaultVisible for initial state, listen for starwind-chart:visible-change, and call setVisible(next, { emit: false }) for silent updates, because a legend toggle commits on its own.<br>**Runtime / HTML:** listen for starwind-chart:visible-change and call setVisible(next, { emit: false }) for silent updates, because a legend toggle commits on its own. |
| defaultVisible | `readonly string[]` | - | control | Controls the default visible state for Chart. | **React:** Use visible for controlled state and defaultVisible for default state, and onVisibleChange for change proposals.<br>**Astro:** Use visible or defaultVisible for initial state, listen for starwind-chart:visible-change, and call setVisible(next, { emit: false }) for silent updates, because a legend toggle commits on its own.<br>**Runtime / HTML:** listen for starwind-chart:visible-change and call setVisible(next, { emit: false }) for silent updates, because a legend toggle commits on its own. |
| onVisibleChange | `(visible: string[], details: ChartVisibleChangeDetails) => void` | - | callback | Receives proposed visible keys before the cancelable starwind-chart:visible-change event. Call details.cancel() to keep the current keys. | **React:** Use visible for controlled state and defaultVisible for default state, and onVisibleChange for change proposals.<br>**Astro:** Use visible or defaultVisible for initial state, listen for starwind-chart:visible-change, and call setVisible(next, { emit: false }) for silent updates, because a legend toggle commits on its own.<br>**Runtime / HTML:** listen for starwind-chart:visible-change and call setVisible(next, { emit: false }) for silent updates, because a legend toggle commits on its own. |
| interactive | `boolean` | true | option | Renders the wire payload, the tooltip and legend templates and, for a chart module or chart parts, the lifecycle script. `false` renders a static chart that loads no script. | - |
| labels | `Record<string, string>` | - | option | Rich label HTML per config key, for a wrapper that renders its own `label-<key>` slots. A `label-<key>` slot on the Root wins. | - |
| icons | `Record<string, string>` | - | option | Icon HTML per config key, for a wrapper that renders its own `icon-<key>` slots. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart` | runtime | - | Marks the Root part so Starwind Runtime can find it. |
| `data-slot` | constant | `chart` | Identifies Root metadata for styling and selectors. |
| `data-chart` | prop | - | Reflects the chart prop on the Root part. |
#### State
| State | Value Type | Controlled Prop | Default Prop | Initial Attribute | Runtime Getter | Runtime Setter | Description | State Control Support |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| visible | `string[]` | visible | defaultVisible | - | `getVisible` | `setVisible` | Tracks the visible state for Chart. | **React:** Use visible for controlled state and defaultVisible for default state, and onVisibleChange for change proposals.<br>**Astro:** Use visible or defaultVisible for initial state, listen for starwind-chart:visible-change, and call setVisible(next, { emit: false }) for silent updates, because a legend toggle commits on its own.<br>**Runtime / HTML:** listen for starwind-chart:visible-change and call setVisible(next, { emit: false }) for silent updates, because a legend toggle commits on its own. |
#### Events
| Event | Callback | DOM Event | Value | Details | Timing | Cancelable | Description | Cancellation Sequence |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| visibleChange | onVisibleChange | starwind-chart:visible-change | visible: `string[]` | ChartVisibleChangeDetails | before-state-commit | Yes | Fires when the visible changes for Chart. | 1. Check internal eligibility and intent.<br>2. Create one details object for the proposal.<br>3. Call the Runtime callback with the details object when the controller exposes one.<br>4. Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.<br>5. Read details.isCanceled, including cancellation caused by preventDefault().<br>6. Apply the accepted state.<br>7. Notify Runtime subscribers and other accepted-only observers. |
#### Initial Markup
| Attributes | Reason |
| --- | --- |
| `data-slot`, `data-chart` | The chart root carries the chart id that scopes config colors and becomes the native id prefix. |

### Style
Writes the config color rules for light and dark themes.
| Fact | Value |
| --- | --- |
| Default element | `style` |
| Discovery hook | `data-sw-chart-style` |
| Role | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-style` | runtime | - | Marks the Style part so Starwind Runtime can find it. |
| `data-slot` | constant | `chart-style` | Identifies Style metadata for styling and selectors. |

### Surface
Provides the Surface part for Chart.
| Fact | Value |
| --- | --- |
| Default element | `div` |
| Discovery hook | `data-sw-chart-surface` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| tooltip | `framework content` | - | rendering | Tooltip body content. It wins over a registered Tooltip. | - |
| fallbackTooltip | `framework content` | - | rendering | Tooltip body content when no Tooltip is registered. | - |
| nativeTooltip | `boolean` | false | option | Uses the native TanStack tooltip body. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-surface` | runtime | - | Marks the Surface part so Starwind Runtime can find it. |
| `data-slot` | constant | `chart-surface` | Identifies Surface metadata for styling and selectors. |
#### Refs
| Part | Public |
| --- | --- |
| surface | Yes |
#### Initial Markup
| Attributes | Reason |
| --- | --- |
| `data-sw-chart-surface`, `data-slot`, `style` | The surface holds the server-rendered SVG and reserves its size, so the Runtime controller adopts it without a second render. |

### Tooltip
Inside a chart root, the first Tooltip supplies the tooltip body, unless a Surface `tooltip` replaces it. Inside a tooltip body it renders its content in place. Elsewhere it renders nothing.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-tooltip` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| children | `framework content` | - | children | Tooltip body content. | - |
| cursor | `boolean` | true | option | Draws the hover band of a bar chart. `false` removes it. Line and area charts draw no hover rule. In a parts chart, the first Tooltip at chart level sets it, because that Tooltip supplies the tooltip body. When it leaves `cursor` unset, the band shows. A Tooltip inside a tooltip body sets nothing. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-tooltip` | runtime | - | Marks the Tooltip part so Starwind Runtime can find it. |

### Tooltip Content
The tooltip body content. It renders inside a tooltip body only.
| Fact | Value |
| --- | --- |
| Default element | `div` |
| Discovery hook | `data-sw-chart-tooltip-content` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| indicator | `"dot" \| "line" \| "dashed"` | - | option | Configures the indicator option for the Tooltip Content part. | - |
| hideLabel | `boolean` | - | option | Configures the hide label option for the Tooltip Content part. | - |
| hideIndicator | `boolean` | - | option | Configures the hide indicator option for the Tooltip Content part. | - |
| label | `string \| framework content` | - | rendering | Replaces the heading before labelFormatter runs. | - |
| labelKey | `string` | - | option | Configures the label key option for the Tooltip Content part. | - |
| nameKey | `string` | - | option | Configures the name key option for the Tooltip Content part. | - |
| valueKey | `string` | - | option | Configures the value key option for the Tooltip Content part. | - |
| color | `string` | - | option | Overrides each row's indicator color. | - |
| formatter | `((value: unknown, name: string, item: TooltipItem<T>, index: number, payload: T) => string \| framework content) \| ChartFormat` | - | callback | A function returns the complete row view, with shadcn's argument order. `payload` is the native datum. A ChartFormat object formats only the row value. | - |
| labelFormatter | `((label: unknown, payload: readonly TooltipItem<T>[]) => string \| framework content) \| ChartFormat` | - | callback | A function returns the complete heading view. A ChartFormat object formats the heading. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-tooltip-content` | runtime | - | Marks the Tooltip Content part so Starwind Runtime can find it. |
| `data-slot` | constant | `chart-tooltip-content` | Identifies Tooltip Content metadata for styling and selectors. |
| `data-chart` | state | - | Reflects the chart state on the Tooltip Content part. |
| `data-indicator` | state | - | Reflects the indicator state on the Tooltip Content part. |
| `data-nested` | state | - | Reflects the nested state on the Tooltip Content part. |
#### Refs
| Part | Public |
| --- | --- |
| tooltipContent | Yes |

### Tooltip Label
The tooltip heading. It renders nothing when the heading is hidden, empty or nested in a row.
| Fact | Value |
| --- | --- |
| Default element | `div` |
| Discovery hook | `data-sw-chart-tooltip-label` |
| Role | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-tooltip-label` | runtime | - | Marks the Tooltip Label part so Starwind Runtime can find it. |
| `data-slot` | constant | `chart-tooltip-label` | Identifies Tooltip Label metadata for styling and selectors. |
#### Refs
| Part | Public |
| --- | --- |
| tooltipLabel | Yes |

### Tooltip Items
Renders its content once per tooltip row, with that row in context.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-tooltip-row` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| children | `framework content` | - | children | Rendered once per tooltip row, with that row in context. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-tooltip-row` | runtime | - | Marks the Tooltip Items part so Starwind Runtime can find it. |

### Tooltip Item
One tooltip row. Rows stay hidden from assistive technology, because the Runtime keeps the native status label in sync with the visible text. A formatter row renders the formatter view.
| Fact | Value |
| --- | --- |
| Default element | `div` |
| Discovery hook | `data-sw-chart-tooltip-item` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| customClass | `string` | - | attribute | The class for a row that a tooltip formatter replaced. React names it `customClassName`. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-tooltip-item` | runtime | - | Marks the Tooltip Item part so Starwind Runtime can find it. |
| `data-slot` | constant | `chart-tooltip-item` | Identifies Tooltip Item metadata for styling and selectors. |
| `data-active` | state | - | Reflects the active state on the Tooltip Item part. |
| `data-key` | state | - | Reflects the key state on the Tooltip Item part. |
#### Refs
| Part | Public |
| --- | --- |
| tooltipItem | Yes |

### Tooltip Indicator
The row color marker. A row with an icon or a hidden indicator renders nothing.
| Fact | Value |
| --- | --- |
| Default element | `span` |
| Discovery hook | `data-sw-chart-tooltip-indicator` |
| Role | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-tooltip-indicator` | runtime | - | Marks the Tooltip Indicator part so Starwind Runtime can find it. |
| `data-slot` | constant | `chart-tooltip-indicator` | Identifies Tooltip Indicator metadata for styling and selectors. |
#### Refs
| Part | Public |
| --- | --- |
| tooltipIndicator | Yes |

### Tooltip Icon
The config icon of a row. Keep TooltipIndicator in the row too: a row with an icon shows the icon in place of the indicator, and a row without one shows the indicator.
| Fact | Value |
| --- | --- |
| Default element | `span` |
| Discovery hook | `data-sw-chart-tooltip-icon` |
| Role | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-tooltip-icon` | runtime | - | Marks the Tooltip Icon part so Starwind Runtime can find it. |
| `data-slot` | constant | `chart-tooltip-icon` | Identifies Tooltip Icon metadata for styling and selectors. |
#### Refs
| Part | Public |
| --- | --- |
| tooltipIcon | Yes |

### Tooltip Name
The row name. One line or dashed row nests the heading inside it.
| Fact | Value |
| --- | --- |
| Default element | `span` |
| Discovery hook | `data-sw-chart-tooltip-name` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| labelClass | `string` | - | attribute | The class of the heading that one line or dashed row nests inside its name. React names it `labelClassName`. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-tooltip-name` | runtime | - | Marks the Tooltip Name part so Starwind Runtime can find it. |
| `data-slot` | constant | `chart-tooltip-name` | Identifies Tooltip Name metadata for styling and selectors. |
#### Refs
| Part | Public |
| --- | --- |
| tooltipName | Yes |

### Tooltip Value
The formatted row value.
| Fact | Value |
| --- | --- |
| Default element | `span` |
| Discovery hook | `data-sw-chart-tooltip-value` |
| Role | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-tooltip-value` | runtime | - | Marks the Tooltip Value part so Starwind Runtime can find it. |
| `data-slot` | constant | `chart-tooltip-value` | Identifies Tooltip Value metadata for styling and selectors. |
#### Refs
| Part | Public |
| --- | --- |
| tooltipValue | Yes |

### Legend
A group of legend items.
| Fact | Value |
| --- | --- |
| Default element | `div` |
| Discovery hook | `data-sw-chart-legend` |
| Role | `group` |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| hideIcon | `boolean` | false | option | Uses the swatch even when config supplies an icon. | - |
| config | `ChartConfig` | - | option | Renders the legend from this config. Leave it out inside a chart Root, which fills the legend with its own config, visibility and rich label slots. | - |
| visible | `readonly string[]` | - | option | Visible keys for a legend that takes `config`. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-legend` | runtime | - | Marks the Legend part so Starwind Runtime can find it. |
| `data-slot` | constant | `chart-legend` | Identifies Legend metadata for styling and selectors. |
#### Refs
| Part | Public |
| --- | --- |
| legend | Yes |

### Legend Items
Renders its content once per config key, with that key in context. A parts chart lists only the keys that it draws.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-legend-template` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| children | `framework content` | - | children | Rendered once per config key, with that key in context. A parts chart lists only the keys that it draws. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-legend-template` | runtime | - | Marks the Legend Items part so Starwind Runtime can find it. |

### Legend Item
A legend toggle for one config key. A click handler that calls `preventDefault()` cancels the toggle.
| Fact | Value |
| --- | --- |
| Default element | `button` |
| Discovery hook | `data-sw-chart-legend-item` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| value | `string` | - | option | The config key this item shows or hides. Defaults to the key from LegendItems. | - |
| interactive | `boolean` | - | option | Defaults to false for a static native definition. A passive item renders a span. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-legend-item` | runtime | - | Marks the Legend Item part so Starwind Runtime can find it. |
| `data-slot` | constant | `chart-legend-item` | Identifies Legend Item metadata for styling and selectors. |
| `data-state` | state | - | Reflects the current state on the Legend Item part. |
| `data-key` | prop | - | Reflects the key prop on the Legend Item part. |
#### Refs
| Part | Public |
| --- | --- |
| legendItem | Yes |

### Legend Swatch
The series color swatch. It renders nothing when the legend shows the config icon.
| Fact | Value |
| --- | --- |
| Default element | `span` |
| Discovery hook | `data-sw-chart-legend-swatch` |
| Role | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-legend-swatch` | runtime | - | Marks the Legend Swatch part so Starwind Runtime can find it. |
| `data-slot` | constant | `chart-legend-swatch` | Identifies Legend Swatch metadata for styling and selectors. |
#### Refs
| Part | Public |
| --- | --- |
| legendSwatch | Yes |

### Legend Icon
The config icon of a legend item. It renders nothing without an icon or with `hideIcon`.
| Fact | Value |
| --- | --- |
| Default element | `span` |
| Discovery hook | `data-sw-chart-legend-icon` |
| Role | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-legend-icon` | runtime | - | Marks the Legend Icon part so Starwind Runtime can find it. |
| `data-slot` | constant | `chart-legend-icon` | Identifies Legend Icon metadata for styling and selectors. |
#### Refs
| Part | Public |
| --- | --- |
| legendIcon | Yes |

### Legend Label
The config label of a legend item: a rich label, or its plain text.
| Fact | Value |
| --- | --- |
| Default element | `span` |
| Discovery hook | `data-sw-chart-legend-label` |
| Role | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-legend-label` | runtime | - | Marks the Legend Label part so Starwind Runtime can find it. |
| `data-slot` | constant | `chart-legend-label` | Identifies Legend Label metadata for styling and selectors. |
#### Refs
| Part | Public |
| --- | --- |
| legendLabel | Yes |

### Bar
Adds bars for one data key. Bars with the same `stackId` stack. Other bars group.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-part-bar` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| dataKey | `string` | - | option | The data key of the values this part draws. | - |
| stackId | `string \| number` | - | option | Bars with the same stackId stack. Other bars group. | - |
| radius | `number \| readonly [number, number, number, number]` | 0 | option | The corner radius in pixels. A number rounds the bar end. Four numbers set the top left, top right, bottom right and bottom left corners, as in Recharts. | - |
| fill | `string` | - | option | The fill color, or `"url(#id)"` for the ChartLinearGradient with that id. Defaults to the config color. | - |
| label | `boolean \| { dataKey?: string; position?: "top" \| "inside" \| "insideLeft" \| "insideStart" \| "right" \| "outside"; offset?: number; fill?: string; fontSize?: number; formatter?: ChartTickFormatter }` | false | option | Draws a label on each bar or value, as a Recharts `label` or `LabelList`. An object sets the row field, position, offset, color, font size and formatter. Labels sit in the middle of the mark, 5px from it, unless `position` says otherwise. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-part-bar` | runtime | - | Marks the Bar part so Starwind Runtime can find it. |

### Line
Adds a line for one data key.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-part-line` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| dataKey | `string` | - | option | The data key of the values this part draws. | - |
| type | `"natural" \| "monotone" \| "linear" \| "step"` | "natural" | option | The curve of the line. | - |
| stroke | `string` | - | option | The stroke color. Defaults to the config color. | - |
| strokeWidth | `number` | 2 | option | The stroke width in pixels. | - |
| dot | `boolean \| { r?: number; fill?: string; fillOpacity?: number; stroke?: string; strokeWidth?: number }` | false | option | Draws a dot at every value. An object sets the dot radius, fill and stroke. | - |
| activeDot | `boolean \| { r?: number; fill?: string; fillOpacity?: number; stroke?: string; strokeWidth?: number }` | true | option | Draws a dot at the focused value. An object sets the dot radius, fill and stroke. | - |
| label | `boolean \| { dataKey?: string; position?: "top" \| "inside" \| "insideLeft" \| "insideStart" \| "right" \| "outside"; offset?: number; fill?: string; fontSize?: number; formatter?: ChartTickFormatter }` | false | option | Draws a label on each bar or value, as a Recharts `label` or `LabelList`. An object sets the row field, position, offset, color, font size and formatter. Labels sit in the middle of the mark, 5px from it, unless `position` says otherwise. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-part-line` | runtime | - | Marks the Line part so Starwind Runtime can find it. |

### Area
Adds an area for one data key. Areas with the same `stackId` stack. Other areas overlap.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-part-area` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| dataKey | `string` | - | option | The data key of the values this part draws. | - |
| stackId | `string \| number` | - | option | Areas with the same stackId stack. Other areas overlap. | - |
| type | `"natural" \| "monotone" \| "linear" \| "step"` | "monotone" | option | The curve of the area edge. | - |
| fill | `string` | - | option | The fill color, or `"url(#id)"` for the ChartLinearGradient with that id. Defaults to the config color. | - |
| stroke | `string` | - | option | The stroke color. Defaults to the config color. | - |
| strokeWidth | `number` | 2 | option | The stroke width in pixels. | - |
| fillOpacity | `number` | 0.22 | option | The fill opacity. A gradient fill without it takes 0.6, the Recharts default, so the stops look as in shadcn. | - |
| activeDot | `boolean` | true | option | Draws a dot at the focused value. | - |
| label | `boolean \| { dataKey?: string; position?: "top" \| "inside" \| "insideLeft" \| "insideStart" \| "right" \| "outside"; offset?: number; fill?: string; fontSize?: number; formatter?: ChartTickFormatter }` | false | option | Draws a label on each bar or value, as a Recharts `label` or `LabelList`. An object sets the row field, position, offset, color, font size and formatter. Labels sit in the middle of the mark, 5px from it, unless `position` says otherwise. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-part-area` | runtime | - | Marks the Area part so Starwind Runtime can find it. |

### Pie
Adds a pie or donut. Slice colors come from config by `nameKey` value.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-part-pie` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| dataKey | `string` | - | option | The data key of the values this part draws. | - |
| nameKey | `string` | - | option | The data key whose values name the slices or bars. Their colors come from config by this value. | - |
| stroke | `string` | "var(--card)" | option | The color of the line between slices. `"none"` removes the line. | - |
| strokeWidth | `number` | 2 | option | The width of the line between slices in pixels. | - |
| label | `boolean \| { dataKey?: string; position?: "top" \| "inside" \| "insideLeft" \| "insideStart" \| "right" \| "outside"; offset?: number; fill?: string; fontSize?: number; formatter?: ChartTickFormatter }` | false | option | Draws a label on each slice, as a Recharts pie `label`. Labels sit 20px outside the slices, and the pie shrinks to make room. `position: "inside"` puts them in the middle of the slices. | - |
| labelLine | `boolean` | true | option | Draws a line from each slice to its outside label. | - |
| innerRadius | `number \| `${number}%`` | 0 | option | The inner radius: plain pixels at any chart size, or a percent of half the smaller side. | - |
| outerRadius | `number \| `${number}%`` | "90%" | option | The outer radius: plain pixels at any chart size, or a percent of half the smaller side. | - |
| paddingAngle | `number` | - | option | The gap between slices in degrees. Without it, slices keep a gap of about 1.4 degrees. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-part-pie` | runtime | - | Marks the Pie part so Starwind Runtime can find it. |

### Radar
Adds a radar area for one data key. The angle key comes from the polar angle axis.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-part-radar` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| dataKey | `string` | - | option | The data key of the values this part draws. | - |
| strokeWidth | `number` | 2 | option | The stroke width in pixels. | - |
| fillOpacity | `number` | 0.18 | option | The fill opacity. | - |
| dot | `boolean \| { r?: number; fill?: string; fillOpacity?: number; stroke?: string; strokeWidth?: number }` | false | option | Draws a dot at every value. An object sets the dot radius, fill and stroke. | - |
| activeDot | `boolean \| { r?: number; fill?: string; fillOpacity?: number; stroke?: string; strokeWidth?: number }` | true | option | Draws a dot at the focused value. An object sets the dot radius, fill and stroke. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-part-radar` | runtime | - | Marks the Radar part so Starwind Runtime can find it. |

### Radial Bar
Adds radial bars. `background` draws the muted track. Bar colors come from config by `nameKey` value.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-part-radial-bar` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| dataKey | `string` | - | option | The data key of the values this part draws. | - |
| nameKey | `string` | - | option | The data key whose values name the slices or bars. Their colors come from config by this value. | - |
| innerRadius | `number \| `${number}%`` | "24%" | option | The inner radius: plain pixels at any chart size, or a percent of half the smaller side. | - |
| outerRadius | `number \| `${number}%`` | "88%" | option | The outer radius: plain pixels at any chart size, or a percent of half the smaller side. | - |
| background | `boolean` | false | option | Draws the muted track behind each bar. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-part-radial-bar` | runtime | - | Marks the Radial Bar part so Starwind Runtime can find it. |

### X Axis
Adds the category axis of a cartesian chart.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-part-x-axis` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| dataKey | `string` | - | option | The data key of the categories. | - |
| tickFormatter | `ChartTickFormatter` | - | option | Formats the tick labels: a function or a ChartFormat object. Astro accepts only a ChartFormat object. | - |
| tickLine | `boolean` | false | option | Draws tick lines. | - |
| axisLine | `boolean` | false | option | Draws the axis line. | - |
| tickMargin | `number` | - | option | The gap between a tick and its label in pixels. | - |
| hide | `boolean` | false | option | Keeps the scale and hides the axis. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-part-x-axis` | runtime | - | Marks the X Axis part so Starwind Runtime can find it. |

### Y Axis
Adds the value axis of a cartesian chart. Without it, the chart draws no value labels.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-part-y-axis` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| tickFormatter | `ChartTickFormatter` | - | option | Formats the tick labels: a function or a ChartFormat object. Astro accepts only a ChartFormat object. | - |
| tickLine | `boolean` | false | option | Draws tick lines. | - |
| axisLine | `boolean` | false | option | Draws the axis line. | - |
| tickMargin | `number` | - | option | The gap between a tick and its label in pixels. | - |
| hide | `boolean` | false | option | Keeps the scale and hides the axis. | - |
| tickCount | `number` | 5 | option | The preferred number of ticks. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-part-y-axis` | runtime | - | Marks the Y Axis part so Starwind Runtime can find it. |

### Grid
Adds cartesian grid lines.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-part-grid` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| vertical | `boolean` | true | option | Draws vertical grid lines. | - |
| horizontal | `boolean` | true | option | Draws horizontal grid lines. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-part-grid` | runtime | - | Marks the Grid part so Starwind Runtime can find it. |

### Polar Grid
Adds the radar grid: rings and the lines from the center to each category.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-part-polar-grid` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| gridType | `"polygon" \| "circle"` | "polygon" | option | The shape of the radar grid. | - |
| radialLines | `boolean` | true | option | Draws the lines from the center to each category. | - |
| fill | `string` | - | option | The fill color of the rings. Without it, the rings have no fill. | - |
| fillOpacity | `number` | - | option | The fill opacity of the rings. Each ring fills its whole disc. | - |
| stroke | `string` | "var(--border)" | option | The color of the rings and the lines. | - |
| strokeWidth | `number` | 1 | option | The width of the rings and the lines in pixels. | - |
| polarRadius | `readonly number[]` | - | option | The ring radii in pixels, as in Recharts. Without it, the rings sit at the value ticks. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-part-polar-grid` | runtime | - | Marks the Polar Grid part so Starwind Runtime can find it. |

### Polar Angle Axis
Adds the radar categories and their labels.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-part-polar-angle-axis` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| dataKey | `string` | - | option | The data key of the categories. | - |
| tickFormatter | `ChartTickFormatter` | - | option | Formats the tick labels: a function or a ChartFormat object. Astro accepts only a ChartFormat object. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-part-polar-angle-axis` | runtime | - | Marks the Polar Angle Axis part so Starwind Runtime can find it. |

### Label
Adds a center line of text to a pie. Without `value`, it shows the total of the visible slices. Each label adds one line, so a second label with `dy` adds a line below the total.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-part-label` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| position | `"center"` | - | option | Where the label sits. | - |
| value | `string \| number` | - | option | The label text. Without it, a pie shows the total of its visible slices. | - |
| dy | `number` | 0 | option | Moves the text down by this many pixels. A negative value moves it up. | - |
| fontSize | `number` | 28 | option | The font size in pixels. | - |
| fontWeight | `number` | 400 | option | The font weight, such as 700 for the bold total of a shadcn donut chart. At the default, the text keeps the weight of the page. | - |
| fill | `string` | "var(--foreground)" | option | The text color. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-part-label` | runtime | - | Marks the Label part so Starwind Runtime can find it. |

### Linear Gradient
Adds a linear gradient that a bar or an area fills from with `fill="url(#id)"`. The chart scopes the id, so two charts can use the same id.
| Fact | Value |
| --- | --- |
| Default element | `template` |
| Discovery hook | `data-sw-chart-part-linear-gradient` |
| Role | - |
#### Props
| Prop | Type | Default | Kind | Description | Framework Behavior |
| --- | --- | --- | --- | --- | --- |
| id | `string` | - | option | The gradient id that a fill such as `"url(#fillDesktop)"` names. | - |
| x1 | `number \| `${number}` \| `${number}%`` | 0 | option | The start x: 0 is the left edge and 1 the right edge, or a percent string. With the defaults, the gradient runs from left to right, as in SVG. | - |
| y1 | `number \| `${number}` \| `${number}%`` | 0 | option | The start y: 0 is the top edge and 1 the bottom edge, or a percent string. | - |
| x2 | `number \| `${number}` \| `${number}%`` | 1 | option | The end x, as `x1`. | - |
| y2 | `number \| `${number}` \| `${number}%`` | 0 | option | The end y, as `y1`. For a gradient from top to bottom, shadcn sets `x2` to 0 and `y2` to 1. | - |
| stops | `readonly { offset: number \| `${number}` \| `${number}%`; stopColor: string; stopOpacity?: number }[]` | - | option | The color stops, as SVG `<stop>` elements. An offset is a number from 0 to 1 or a percent string such as `"5%"`. | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-chart-part-linear-gradient` | runtime | - | Marks the Linear Gradient part so Starwind Runtime can find it. |
## Runtime API
| Fact | Value |
| --- | --- |
| Factory | [`createChart`](/docs/runtime/#create-chart) |
| Import | `@starwind-ui/runtime/chart` |
| Root part | root |
| Option props | chart, definition, host, onVisibleChange |
| Option lifecycles | chart: setter-backed, definition: setter-backed, host: setter-backed, onVisibleChange: setter-backed |
## Related Styled Components
| Component | Relationship |
| --- | --- |
| [Chart](/docs/components/chart/) | Direct Primitive |
## Exports
| Group | Import | Exports |
| --- | --- | --- |
| Runtime | `@starwind-ui/runtime/chart` | `createChart` |
| Astro Primitive | `@starwind-ui/astro/chart` | `Chart`, `ChartRoot`, `ChartSurface`, `ChartTooltipContent`, `ChartTooltipLabel`, `ChartTooltipItem`, `ChartTooltipIndicator`, `ChartTooltipIcon`, `ChartTooltipName`, `ChartTooltipValue`, `ChartLegend`, `ChartLegendItem`, `ChartLegendSwatch`, `ChartLegendIcon`, `ChartLegendLabel` |
| React Primitive | `@starwind-ui/react/chart` | `Chart`, `ChartRoot`, `ChartSurface`, `ChartTooltipContent`, `ChartTooltipLabel`, `ChartTooltipItem`, `ChartTooltipIndicator`, `ChartTooltipIcon`, `ChartTooltipName`, `ChartTooltipValue`, `ChartLegend`, `ChartLegendItem`, `ChartLegendSwatch`, `ChartLegendIcon`, `ChartLegendLabel` |
## Canonical Names
| Kind | Name |
| --- | --- |
| namespace | `Chart` |
| runtime-factory | `createChart` |
| part | `Chart.Root` |
| part | `Chart.Surface` |
| part | `Chart.TooltipContent` |
| part | `Chart.TooltipLabel` |
| part | `Chart.TooltipItem` |
| part | `Chart.TooltipIndicator` |
| part | `Chart.TooltipIcon` |
| part | `Chart.TooltipName` |
| part | `Chart.TooltipValue` |
| part | `Chart.Legend` |
| part | `Chart.LegendItem` |
| part | `Chart.LegendSwatch` |
| part | `Chart.LegendIcon` |
| part | `Chart.LegendLabel` |
## Changelog
### v1.0.0
- First release. Chart draws bar, line, area, pie, radar and radial charts from your data and series config. Put `Bar`, `Line`, `Area`, `Pie`, `Radar` or `RadialBar` parts inside `Chart.Root`, and add `XAxis`, `YAxis`, `Grid`, `PolarGrid`, `PolarAngleAxis` and `Label` for axes, grid lines and center text. Add `LinearGradient` for gradient fills. Runtime adds the Starwind tooltip, legend toggles and series colors from your theme tokens. The parts are the same in Astro, React, Vue and Svelte.
- The parts take the props of common shadcn/ui examples: `label` draws values on bars, lines, areas and pie slices in place of a nested `LabelList`, `Bar` takes four corner radii, `stackOffset="expand"` on `Chart.Root` stacks to 100%, and `cursor={false}` on `Chart.Tooltip` removes the hover band. The legend lists only the series that the chart draws.
- `tickFormatter`, `formatter` and `labelFormatter` take a `ChartFormat` object in every framework. React, Vue and Svelte also take functions. `{ configLabel: true }` shows the config label of a value. For a chart that the parts cannot draw, pass a TanStack Charts chart module to the `chart` prop. Install `@tanstack/charts` in your app to write chart modules. `starwind primitives add chart` installs it for you.