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
Use the Astro primitive adapter to render Chart anatomy with the Runtime wiring included.
---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>Use the React primitive adapter when Chart state participates in React rendering.
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> );}Use the Vue components to build Chart.
<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>Use the Svelte components to build Chart.
<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>Render the Chart data-sw-* contract yourself, then initialize createChart.
<!-- 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>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.
- Default element
- div
- Discovery hook
- data-sw-chart
- Role
- -
Props
id string -
- Description
- Names the chart. Leave it out to get a generated id that matches on server and client.
- Kind
- attribute
- Targets
- root
- Full type
- string
chart ChartSource<T, D, X, Y> -
- Description
- The chart module or factory that draws the chart from the data and the visible keys.
- Kind
- option
- Targets
- root
- Full type
- ChartSource<T, D, X, Y>
definition DomChartDefinition<D, X, Y> -
- Description
- A static native TanStack definition. It owns its visibility, so the legend is passive.
- Kind
- option
- Targets
- root
- Full type
- DomChartDefinition<D, X, Y>
data readonly T[] -
- Description
- The data rows that the chart module reads.
- Kind
- option
- Targets
- root
- Full type
- readonly T[]
config ChartConfig -
- Description
- The series labels, colors and icons, keyed by series.
- Kind
- option
- Targets
- root
- Full type
- ChartConfig
params Record<string, unknown> -
- Description
- Extra values that the chart module reads.
- Kind
- option
- Targets
- root
- Full type
- Record<string, unknown>
theme ChartInput["theme"] -
- Description
- Overrides the native TanStack chart theme.
- Kind
- option
- Targets
- root
- Full type
- ChartInput["theme"]
host NativeHostOptions<D, X, Y> -
- Description
- Native TanStack host options, such as the focus and select callbacks.
- Kind
- option
- Targets
- root
- Full type
- NativeHostOptions<D, X, Y>
disableTooltip boolean false
- Description
- Turns the tooltip off.
- Kind
- option
- Targets
- root
- Full type
- boolean
stackOffset "none" | "expand" "none"
- Description
- How the stacked bars and areas of a parts chart stack. `"expand"` stacks each category to 100%, and the tooltip keeps the raw values.
- Kind
- option
- Targets
- root
- Full type
- "none" | "expand"
visible readonly string[] -
- Description
- Controls the visible state for Chart.
- Kind
- control
- Targets
- root
- Full type
- readonly string[]
- React
Use visible for controlled state and defaultVisible for default state, and onVisibleChange for change proposals.
visibledefaultVisibleonVisibleChange
- 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.
visibledefaultVisiblestarwind-chart:visible-changesetVisible
- 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.
starwind-chart:visible-changesetVisible
defaultVisible readonly string[] -
- Description
- Controls the default visible state for Chart.
- Kind
- control
- Targets
- root
- Full type
- readonly string[]
- React
Use visible for controlled state and defaultVisible for default state, and onVisibleChange for change proposals.
visibledefaultVisibleonVisibleChange
- 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.
visibledefaultVisiblestarwind-chart:visible-changesetVisible
- 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.
starwind-chart:visible-changesetVisible
onVisibleChange (visible: string[], details: ChartVisibleChangeDetails) => void -
- Description
- Receives proposed visible keys before the cancelable starwind-chart:visible-change event. Call details.cancel() to keep the current keys.
- Kind
- callback
- Targets
- root
- Full type
- (visible: string[], details: ChartVisibleChangeDetails) => void
- React
Use visible for controlled state and defaultVisible for default state, and onVisibleChange for change proposals.
visibledefaultVisibleonVisibleChange
- 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.
visibledefaultVisiblestarwind-chart:visible-changesetVisible
- 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.
starwind-chart:visible-changesetVisible
interactive Astro only boolean true
- Description
- 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.
- Kind
- option
- Targets
- root
- Frameworks
- Astro
- Full type
- boolean
labels Astro only Record<string, string> -
- Description
- Rich label HTML per config key, for a wrapper that renders its own `label-<key>` slots. A `label-<key>` slot on the Root wins.
- Kind
- option
- Targets
- root
- Frameworks
- Astro
- Full type
- Record<string, string>
icons Astro only Record<string, string> -
- Description
- Icon HTML per config key, for a wrapper that renders its own `icon-<key>` slots.
- Kind
- option
- Targets
- root
- Frameworks
- Astro
- Full type
- Record<string, string>
Events
visibleChange onVisibleChange visible: string[]
- Description
- Fires when the visible changes for Chart.
- DOM event
- starwind-chart:visible-change
- Details type
- ChartVisibleChangeDetails
- Timing
- before-state-commit
- Cancelable
- Yes
- Cancellation sequence
- Check internal eligibility and intent.
- Create one details object for the proposal.
- Call the Runtime callback with the details object when the controller exposes one.
- Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.
- Read details.isCanceled, including cancellation caused by preventDefault().
- Apply the accepted state.
- Notify Runtime subscribers and other accepted-only observers.
State
visible string[] visible / defaultVisible
- Description
- Tracks the visible state for Chart.
- Initial attribute
- -
- Runtime getter
- getVisible
- Runtime setter
- setVisible
- React
Use visible for controlled state and defaultVisible for default state, and onVisibleChange for change proposals.
visibledefaultVisibleonVisibleChange
- 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.
visibledefaultVisiblestarwind-chart:visible-changesetVisible
- 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.
starwind-chart:visible-changesetVisible
Data Attributes
Runtime hooks
Metadata
Style
Writes the config color rules for light and dark themes.
- Default element
- style
- Discovery hook
- data-sw-chart-style
- Role
- -
Data Attributes
Runtime hooks
Metadata
Surface
Provides the Surface part for Chart.
- Default element
- div
- Discovery hook
- data-sw-chart-surface
- Role
- -
Props
tooltip framework content -
- Description
- Tooltip body content. It wins over a registered Tooltip.
- Kind
- rendering
- Targets
- surface
- Full type
- framework content
fallbackTooltip framework content -
- Description
- Tooltip body content when no Tooltip is registered.
- Kind
- rendering
- Targets
- surface
- Full type
- framework content
nativeTooltip boolean false
- Description
- Uses the native TanStack tooltip body.
- Kind
- option
- Targets
- surface
- Full type
- boolean
Data Attributes
Runtime hooks
Metadata
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.
- Default element
- template
- Discovery hook
- data-sw-chart-tooltip
- Role
- -
Props
children framework content -
- Description
- Tooltip body content.
- Kind
- children
- Targets
- tooltip
- Full type
- framework content
cursor boolean true
- Description
- 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.
- Kind
- option
- Targets
- tooltip
- Full type
- boolean
Data Attributes
Runtime hooks
Tooltip Content
The tooltip body content. It renders inside a tooltip body only.
- Default element
- div
- Discovery hook
- data-sw-chart-tooltip-content
- Role
- -
Props
indicator "dot" | "line" | "dashed" -
- Description
- Configures the indicator option for the Tooltip Content part.
- Kind
- option
- Targets
- tooltipContent
- Full type
- "dot" | "line" | "dashed"
hideLabel boolean -
- Description
- Configures the hide label option for the Tooltip Content part.
- Kind
- option
- Targets
- tooltipContent
- Full type
- boolean
hideIndicator boolean -
- Description
- Configures the hide indicator option for the Tooltip Content part.
- Kind
- option
- Targets
- tooltipContent
- Full type
- boolean
label string | framework content -
- Description
- Replaces the heading before labelFormatter runs.
- Kind
- rendering
- Targets
- tooltipContent
- Full type
- string | framework content
labelKey string -
- Description
- Configures the label key option for the Tooltip Content part.
- Kind
- option
- Targets
- tooltipContent
- Full type
- string
nameKey string -
- Description
- Configures the name key option for the Tooltip Content part.
- Kind
- option
- Targets
- tooltipContent
- Full type
- string
valueKey string -
- Description
- Configures the value key option for the Tooltip Content part.
- Kind
- option
- Targets
- tooltipContent
- Full type
- string
color string -
- Description
- Overrides each row's indicator color.
- Kind
- option
- Targets
- tooltipContent
- Full type
- string
formatter ((value: unknown, name: string, item: TooltipItem<T>, index: number, payload: T) => string | framework content) | ChartFormat -
- Description
- 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.
- Kind
- callback
- Targets
- tooltipContent
- Full type
- ((value: unknown, name: string, item: TooltipItem<T>, index: number, payload: T) => string | framework content) | ChartFormat
labelFormatter ((label: unknown, payload: readonly TooltipItem<T>[]) => string | framework content) | ChartFormat -
- Description
- A function returns the complete heading view. A ChartFormat object formats the heading.
- Kind
- callback
- Targets
- tooltipContent
- Full type
- ((label: unknown, payload: readonly TooltipItem<T>[]) => string | framework content) | ChartFormat
Data Attributes
Runtime hooks
State
Metadata
Tooltip Label
The tooltip heading. It renders nothing when the heading is hidden, empty or nested in a row.
- Default element
- div
- Discovery hook
- data-sw-chart-tooltip-label
- Role
- -
Data Attributes
Runtime hooks
Metadata
Tooltip Items
Renders its content once per tooltip row, with that row in context.
- Default element
- template
- Discovery hook
- data-sw-chart-tooltip-row
- Role
- -
Props
children framework content -
- Description
- Rendered once per tooltip row, with that row in context.
- Kind
- children
- Targets
- tooltipItems
- Full type
- framework content
Data Attributes
Runtime hooks
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.
- Default element
- div
- Discovery hook
- data-sw-chart-tooltip-item
- Role
- -
Props
customClass string -
- Description
- The class for a row that a tooltip formatter replaced. React names it `customClassName`.
- Kind
- attribute
- Targets
- tooltipItem
- Full type
- string
Data Attributes
Runtime hooks
State
Metadata
Tooltip Indicator
The row color marker. A row with an icon or a hidden indicator renders nothing.
- Default element
- span
- Discovery hook
- data-sw-chart-tooltip-indicator
- Role
- -
Data Attributes
Runtime hooks
Metadata
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.
- Default element
- span
- Discovery hook
- data-sw-chart-tooltip-icon
- Role
- -
Data Attributes
Runtime hooks
Metadata
Tooltip Name
The row name. One line or dashed row nests the heading inside it.
- Default element
- span
- Discovery hook
- data-sw-chart-tooltip-name
- Role
- -
Props
labelClass string -
- Description
- The class of the heading that one line or dashed row nests inside its name. React names it `labelClassName`.
- Kind
- attribute
- Targets
- tooltipName
- Full type
- string
Data Attributes
Runtime hooks
Metadata
Tooltip Value
The formatted row value.
- Default element
- span
- Discovery hook
- data-sw-chart-tooltip-value
- Role
- -
Data Attributes
Runtime hooks
Metadata
Legend
A group of legend items.
- Default element
- div
- Discovery hook
- data-sw-chart-legend
- Role
- group
Props
hideIcon boolean false
- Description
- Uses the swatch even when config supplies an icon.
- Kind
- option
- Targets
- legend
- Full type
- boolean
config Astro only ChartConfig -
- Description
- 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.
- Kind
- option
- Targets
- legend
- Frameworks
- Astro
- Full type
- ChartConfig
visible Astro only readonly string[] -
- Description
- Visible keys for a legend that takes `config`.
- Kind
- option
- Targets
- legend
- Frameworks
- Astro
- Full type
- readonly string[]
Data Attributes
Runtime hooks
Metadata
Legend Items
Renders its content once per config key, with that key in context. A parts chart lists only the keys that it draws.
- Default element
- template
- Discovery hook
- data-sw-chart-legend-template
- Role
- -
Props
children framework content -
- Description
- Rendered once per config key, with that key in context. A parts chart lists only the keys that it draws.
- Kind
- children
- Targets
- legendItems
- Full type
- framework content
Data Attributes
Runtime hooks
Legend Item
A legend toggle for one config key. A click handler that calls `preventDefault()` cancels the toggle.
- Default element
- button
- Discovery hook
- data-sw-chart-legend-item
- Role
- -
Props
value string -
- Description
- The config key this item shows or hides. Defaults to the key from LegendItems.
- Kind
- option
- Targets
- legendItem
- Full type
- string
interactive boolean -
- Description
- Defaults to false for a static native definition. A passive item renders a span.
- Kind
- option
- Targets
- legendItem
- Full type
- boolean
Data Attributes
Runtime hooks
State
Metadata
Legend Swatch
The series color swatch. It renders nothing when the legend shows the config icon.
- Default element
- span
- Discovery hook
- data-sw-chart-legend-swatch
- Role
- -
Data Attributes
Runtime hooks
Metadata
Legend Icon
The config icon of a legend item. It renders nothing without an icon or with `hideIcon`.
- Default element
- span
- Discovery hook
- data-sw-chart-legend-icon
- Role
- -
Data Attributes
Runtime hooks
Metadata
Legend Label
The config label of a legend item: a rich label, or its plain text.
- Default element
- span
- Discovery hook
- data-sw-chart-legend-label
- Role
- -
Data Attributes
Runtime hooks
Metadata
Bar
Adds bars for one data key. Bars with the same `stackId` stack. Other bars group.
- Default element
- template
- Discovery hook
- data-sw-chart-part-bar
- Role
- -
Props
dataKey string -
- Description
- The data key of the values this part draws.
- Kind
- option
- Targets
- bar, line, area, pie, radar, radialBar
- Full type
- string
stackId string | number -
- Description
- Bars with the same stackId stack. Other bars group.
- Kind
- option
- Targets
- bar
- Full type
- string | number
radius number | readonly [number, number, number, number] 0
- Description
- 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.
- Kind
- option
- Targets
- bar
- Full type
- number | readonly [number, number, number, number]
fill string -
- Description
- The fill color, or `"url(#id)"` for the ChartLinearGradient with that id. Defaults to the config color.
- Kind
- option
- Targets
- bar, area
- Full type
- string
label boolean | { dataKey?: string; position?: "top" | "inside" | "insideLeft" | "insideStart" | "right" | "outside"; offset?: number; fill?: string; fontSize?: number; formatter?: ChartTickFormatter } false
- Description
- 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.
- Kind
- option
- Targets
- bar, line, area
- Full type
- boolean | { dataKey?: string; position?: "top" | "inside" | "insideLeft" | "insideStart" | "right" | "outside"; offset?: number; fill?: string; fontSize?: number; formatter?: ChartTickFormatter }
Data Attributes
Runtime hooks
Line
Adds a line for one data key.
- Default element
- template
- Discovery hook
- data-sw-chart-part-line
- Role
- -
Props
dataKey string -
- Description
- The data key of the values this part draws.
- Kind
- option
- Targets
- bar, line, area, pie, radar, radialBar
- Full type
- string
type "natural" | "monotone" | "linear" | "step" "natural"
- Description
- The curve of the line.
- Kind
- option
- Targets
- line
- Full type
- "natural" | "monotone" | "linear" | "step"
stroke string -
- Description
- The stroke color. Defaults to the config color.
- Kind
- option
- Targets
- line, area
- Full type
- string
strokeWidth number 2
- Description
- The stroke width in pixels.
- Kind
- option
- Targets
- line, area, radar
- Full type
- number
dot boolean | { r?: number; fill?: string; fillOpacity?: number; stroke?: string; strokeWidth?: number } false
- Description
- Draws a dot at every value. An object sets the dot radius, fill and stroke.
- Kind
- option
- Targets
- line, radar
- Full type
- boolean | { r?: number; fill?: string; fillOpacity?: number; stroke?: string; strokeWidth?: number }
activeDot boolean | { r?: number; fill?: string; fillOpacity?: number; stroke?: string; strokeWidth?: number } true
- Description
- Draws a dot at the focused value. An object sets the dot radius, fill and stroke.
- Kind
- option
- Targets
- line, radar
- Full type
- boolean | { r?: number; fill?: string; fillOpacity?: number; stroke?: string; strokeWidth?: number }
label boolean | { dataKey?: string; position?: "top" | "inside" | "insideLeft" | "insideStart" | "right" | "outside"; offset?: number; fill?: string; fontSize?: number; formatter?: ChartTickFormatter } false
- Description
- 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.
- Kind
- option
- Targets
- bar, line, area
- Full type
- boolean | { dataKey?: string; position?: "top" | "inside" | "insideLeft" | "insideStart" | "right" | "outside"; offset?: number; fill?: string; fontSize?: number; formatter?: ChartTickFormatter }
Data Attributes
Runtime hooks
Area
Adds an area for one data key. Areas with the same `stackId` stack. Other areas overlap.
- Default element
- template
- Discovery hook
- data-sw-chart-part-area
- Role
- -
Props
dataKey string -
- Description
- The data key of the values this part draws.
- Kind
- option
- Targets
- bar, line, area, pie, radar, radialBar
- Full type
- string
stackId string | number -
- Description
- Areas with the same stackId stack. Other areas overlap.
- Kind
- option
- Targets
- area
- Full type
- string | number
type "natural" | "monotone" | "linear" | "step" "monotone"
- Description
- The curve of the area edge.
- Kind
- option
- Targets
- area
- Full type
- "natural" | "monotone" | "linear" | "step"
fill string -
- Description
- The fill color, or `"url(#id)"` for the ChartLinearGradient with that id. Defaults to the config color.
- Kind
- option
- Targets
- bar, area
- Full type
- string
stroke string -
- Description
- The stroke color. Defaults to the config color.
- Kind
- option
- Targets
- line, area
- Full type
- string
strokeWidth number 2
- Description
- The stroke width in pixels.
- Kind
- option
- Targets
- line, area, radar
- Full type
- number
fillOpacity number 0.22
- Description
- The fill opacity. A gradient fill without it takes 0.6, the Recharts default, so the stops look as in shadcn.
- Kind
- option
- Targets
- area
- Full type
- number
activeDot boolean true
- Description
- Draws a dot at the focused value.
- Kind
- option
- Targets
- area
- Full type
- boolean
label boolean | { dataKey?: string; position?: "top" | "inside" | "insideLeft" | "insideStart" | "right" | "outside"; offset?: number; fill?: string; fontSize?: number; formatter?: ChartTickFormatter } false
- Description
- 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.
- Kind
- option
- Targets
- bar, line, area
- Full type
- boolean | { dataKey?: string; position?: "top" | "inside" | "insideLeft" | "insideStart" | "right" | "outside"; offset?: number; fill?: string; fontSize?: number; formatter?: ChartTickFormatter }
Data Attributes
Runtime hooks
Pie
Adds a pie or donut. Slice colors come from config by `nameKey` value.
- Default element
- template
- Discovery hook
- data-sw-chart-part-pie
- Role
- -
Props
dataKey string -
- Description
- The data key of the values this part draws.
- Kind
- option
- Targets
- bar, line, area, pie, radar, radialBar
- Full type
- string
nameKey string -
- Description
- The data key whose values name the slices or bars. Their colors come from config by this value.
- Kind
- option
- Targets
- pie, radialBar
- Full type
- string
stroke string "var(--card)"
- Description
- The color of the line between slices. `"none"` removes the line.
- Kind
- option
- Targets
- pie
- Full type
- string
strokeWidth number 2
- Description
- The width of the line between slices in pixels.
- Kind
- option
- Targets
- pie
- Full type
- number
label boolean | { dataKey?: string; position?: "top" | "inside" | "insideLeft" | "insideStart" | "right" | "outside"; offset?: number; fill?: string; fontSize?: number; formatter?: ChartTickFormatter } false
- Description
- 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.
- Kind
- option
- Targets
- pie
- Full type
- boolean | { dataKey?: string; position?: "top" | "inside" | "insideLeft" | "insideStart" | "right" | "outside"; offset?: number; fill?: string; fontSize?: number; formatter?: ChartTickFormatter }
labelLine boolean true
- Description
- Draws a line from each slice to its outside label.
- Kind
- option
- Targets
- pie
- Full type
- boolean
innerRadius number | `${number}%` 0
- Description
- The inner radius: plain pixels at any chart size, or a percent of half the smaller side.
- Kind
- option
- Targets
- pie
- Full type
- number | `${number}%`
outerRadius number | `${number}%` "90%"
- Description
- The outer radius: plain pixels at any chart size, or a percent of half the smaller side.
- Kind
- option
- Targets
- pie
- Full type
- number | `${number}%`
paddingAngle number -
- Description
- The gap between slices in degrees. Without it, slices keep a gap of about 1.4 degrees.
- Kind
- option
- Targets
- pie
- Full type
- number
Data Attributes
Runtime hooks
Radar
Adds a radar area for one data key. The angle key comes from the polar angle axis.
- Default element
- template
- Discovery hook
- data-sw-chart-part-radar
- Role
- -
Props
dataKey string -
- Description
- The data key of the values this part draws.
- Kind
- option
- Targets
- bar, line, area, pie, radar, radialBar
- Full type
- string
strokeWidth number 2
- Description
- The stroke width in pixels.
- Kind
- option
- Targets
- line, area, radar
- Full type
- number
fillOpacity number 0.18
- Description
- The fill opacity.
- Kind
- option
- Targets
- radar
- Full type
- number
dot boolean | { r?: number; fill?: string; fillOpacity?: number; stroke?: string; strokeWidth?: number } false
- Description
- Draws a dot at every value. An object sets the dot radius, fill and stroke.
- Kind
- option
- Targets
- line, radar
- Full type
- boolean | { r?: number; fill?: string; fillOpacity?: number; stroke?: string; strokeWidth?: number }
activeDot boolean | { r?: number; fill?: string; fillOpacity?: number; stroke?: string; strokeWidth?: number } true
- Description
- Draws a dot at the focused value. An object sets the dot radius, fill and stroke.
- Kind
- option
- Targets
- line, radar
- Full type
- boolean | { r?: number; fill?: string; fillOpacity?: number; stroke?: string; strokeWidth?: number }
Data Attributes
Runtime hooks
Radial Bar
Adds radial bars. `background` draws the muted track. Bar colors come from config by `nameKey` value.
- Default element
- template
- Discovery hook
- data-sw-chart-part-radial-bar
- Role
- -
Props
dataKey string -
- Description
- The data key of the values this part draws.
- Kind
- option
- Targets
- bar, line, area, pie, radar, radialBar
- Full type
- string
nameKey string -
- Description
- The data key whose values name the slices or bars. Their colors come from config by this value.
- Kind
- option
- Targets
- pie, radialBar
- Full type
- string
innerRadius number | `${number}%` "24%"
- Description
- The inner radius: plain pixels at any chart size, or a percent of half the smaller side.
- Kind
- option
- Targets
- radialBar
- Full type
- number | `${number}%`
outerRadius number | `${number}%` "88%"
- Description
- The outer radius: plain pixels at any chart size, or a percent of half the smaller side.
- Kind
- option
- Targets
- radialBar
- Full type
- number | `${number}%`
background boolean false
- Description
- Draws the muted track behind each bar.
- Kind
- option
- Targets
- radialBar
- Full type
- boolean
Data Attributes
Runtime hooks
X Axis
Adds the category axis of a cartesian chart.
- Default element
- template
- Discovery hook
- data-sw-chart-part-x-axis
- Role
- -
Props
dataKey string -
- Description
- The data key of the categories.
- Kind
- option
- Targets
- xAxis, polarAngleAxis
- Full type
- string
tickFormatter ChartTickFormatter -
- Description
- Formats the tick labels: a function or a ChartFormat object. Astro accepts only a ChartFormat object.
- Kind
- option
- Targets
- xAxis, yAxis, polarAngleAxis
- Full type
- ChartTickFormatter
tickLine boolean false
- Description
- Draws tick lines.
- Kind
- option
- Targets
- xAxis, yAxis
- Full type
- boolean
axisLine boolean false
- Description
- Draws the axis line.
- Kind
- option
- Targets
- xAxis, yAxis
- Full type
- boolean
tickMargin number -
- Description
- The gap between a tick and its label in pixels.
- Kind
- option
- Targets
- xAxis, yAxis
- Full type
- number
hide boolean false
- Description
- Keeps the scale and hides the axis.
- Kind
- option
- Targets
- xAxis, yAxis
- Full type
- boolean
Data Attributes
Runtime hooks
Y Axis
Adds the value axis of a cartesian chart. Without it, the chart draws no value labels.
- Default element
- template
- Discovery hook
- data-sw-chart-part-y-axis
- Role
- -
Props
tickFormatter ChartTickFormatter -
- Description
- Formats the tick labels: a function or a ChartFormat object. Astro accepts only a ChartFormat object.
- Kind
- option
- Targets
- xAxis, yAxis, polarAngleAxis
- Full type
- ChartTickFormatter
tickLine boolean false
- Description
- Draws tick lines.
- Kind
- option
- Targets
- xAxis, yAxis
- Full type
- boolean
axisLine boolean false
- Description
- Draws the axis line.
- Kind
- option
- Targets
- xAxis, yAxis
- Full type
- boolean
tickMargin number -
- Description
- The gap between a tick and its label in pixels.
- Kind
- option
- Targets
- xAxis, yAxis
- Full type
- number
hide boolean false
- Description
- Keeps the scale and hides the axis.
- Kind
- option
- Targets
- xAxis, yAxis
- Full type
- boolean
tickCount number 5
- Description
- The preferred number of ticks.
- Kind
- option
- Targets
- yAxis
- Full type
- number
Data Attributes
Runtime hooks
Grid
Adds cartesian grid lines.
- Default element
- template
- Discovery hook
- data-sw-chart-part-grid
- Role
- -
Props
vertical boolean true
- Description
- Draws vertical grid lines.
- Kind
- option
- Targets
- grid
- Full type
- boolean
horizontal boolean true
- Description
- Draws horizontal grid lines.
- Kind
- option
- Targets
- grid
- Full type
- boolean
Data Attributes
Runtime hooks
Polar Grid
Adds the radar grid: rings and the lines from the center to each category.
- Default element
- template
- Discovery hook
- data-sw-chart-part-polar-grid
- Role
- -
Props
gridType "polygon" | "circle" "polygon"
- Description
- The shape of the radar grid.
- Kind
- option
- Targets
- polarGrid
- Full type
- "polygon" | "circle"
radialLines boolean true
- Description
- Draws the lines from the center to each category.
- Kind
- option
- Targets
- polarGrid
- Full type
- boolean
fill string -
- Description
- The fill color of the rings. Without it, the rings have no fill.
- Kind
- option
- Targets
- polarGrid
- Full type
- string
fillOpacity number -
- Description
- The fill opacity of the rings. Each ring fills its whole disc.
- Kind
- option
- Targets
- polarGrid
- Full type
- number
stroke string "var(--border)"
- Description
- The color of the rings and the lines.
- Kind
- option
- Targets
- polarGrid
- Full type
- string
strokeWidth number 1
- Description
- The width of the rings and the lines in pixels.
- Kind
- option
- Targets
- polarGrid
- Full type
- number
polarRadius readonly number[] -
- Description
- The ring radii in pixels, as in Recharts. Without it, the rings sit at the value ticks.
- Kind
- option
- Targets
- polarGrid
- Full type
- readonly number[]
Data Attributes
Runtime hooks
Polar Angle Axis
Adds the radar categories and their labels.
- Default element
- template
- Discovery hook
- data-sw-chart-part-polar-angle-axis
- Role
- -
Props
dataKey string -
- Description
- The data key of the categories.
- Kind
- option
- Targets
- xAxis, polarAngleAxis
- Full type
- string
tickFormatter ChartTickFormatter -
- Description
- Formats the tick labels: a function or a ChartFormat object. Astro accepts only a ChartFormat object.
- Kind
- option
- Targets
- xAxis, yAxis, polarAngleAxis
- Full type
- ChartTickFormatter
Data Attributes
Runtime hooks
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.
- Default element
- template
- Discovery hook
- data-sw-chart-part-label
- Role
- -
Props
position "center" -
- Description
- Where the label sits.
- Kind
- option
- Targets
- label
- Full type
- "center"
value string | number -
- Description
- The label text. Without it, a pie shows the total of its visible slices.
- Kind
- option
- Targets
- label
- Full type
- string | number
dy number 0
- Description
- Moves the text down by this many pixels. A negative value moves it up.
- Kind
- option
- Targets
- label
- Full type
- number
fontSize number 28
- Description
- The font size in pixels.
- Kind
- option
- Targets
- label
- Full type
- number
fontWeight number 400
- Description
- 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.
- Kind
- option
- Targets
- label
- Full type
- number
fill string "var(--foreground)"
- Description
- The text color.
- Kind
- option
- Targets
- label
- Full type
- string
Data Attributes
Runtime hooks
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.
- Default element
- template
- Discovery hook
- data-sw-chart-part-linear-gradient
- Role
- -
Props
id string -
- Description
- The gradient id that a fill such as `"url(#fillDesktop)"` names.
- Kind
- option
- Targets
- linearGradient
- Full type
- string
x1 number | `${number}` | `${number}%` 0
- Description
- 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.
- Kind
- option
- Targets
- linearGradient
- Full type
- number | `${number}` | `${number}%`
y1 number | `${number}` | `${number}%` 0
- Description
- The start y: 0 is the top edge and 1 the bottom edge, or a percent string.
- Kind
- option
- Targets
- linearGradient
- Full type
- number | `${number}` | `${number}%`
x2 number | `${number}` | `${number}%` 1
- Description
- The end x, as `x1`.
- Kind
- option
- Targets
- linearGradient
- Full type
- number | `${number}` | `${number}%`
y2 number | `${number}` | `${number}%` 0
- Description
- The end y, as `y1`. For a gradient from top to bottom, shadcn sets `x2` to 0 and `y2` to 1.
- Kind
- option
- Targets
- linearGradient
- Full type
- number | `${number}` | `${number}%`
stops readonly { offset: number | `${number}` | `${number}%`; stopColor: string; stopOpacity?: number }[] -
- Description
- The color stops, as SVG `<stop>` elements. An offset is a number from 0 to 1 or a percent string such as `"5%"`.
- Kind
- option
- Targets
- linearGradient
- Full type
- readonly { offset: number | `${number}` | `${number}%`; stopColor: string; stopOpacity?: number }[]
Data Attributes
Runtime hooks
Runtime API
- Factory
createChart- Import
@starwind-ui/runtime/chart- Root hook
- root
data-sw-chart - Option props
- chart, definition, host, onVisibleChange
Option Lifecycles
| Option | Lifecycle |
|---|---|
| chart | setter-backed |
| definition | setter-backed |
| host | setter-backed |
| onVisibleChange | setter-backed |
Related Styled Components
Changelog
View version history v1.0.0 1 release
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.