Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build React UIs using shadcn/ui components with Tailwind CSS, Radix primitives, and proper accessibility patterns.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/chart.md
1### shadcn/ui Chart Component - Installation23Source: https://ui.shadcn.com/docs/components/chart45The chart component in shadcn/ui is built on Recharts, providing direct access to all Recharts capabilities with consistent theming.67```bash8npx shadcn@latest add chart9```1011--------------------------------1213### shadcn/ui Chart Component - Basic Usage1415Source: https://ui.shadcn.com/docs/components/chart1617The ChartContainer wraps your Recharts component and accepts a config prop for theming. Requires `min-h-[value]` for responsiveness.1819```tsx20import { Bar, BarChart, CartesianGrid, XAxis, YAxis } from "recharts"21import { ChartContainer, ChartTooltipContent } from "@/components/ui/chart"2223const chartConfig = {24desktop: {25label: "Desktop",26color: "var(--chart-1)",27},28mobile: {29label: "Mobile",30color: "var(--chart-2)",31},32} satisfies import("@/components/ui/chart").ChartConfig3334const chartData = [35{ month: "January", desktop: 186, mobile: 80 },36{ month: "February", desktop: 305, mobile: 200 },37{ month: "March", desktop: 237, mobile: 120 },38]3940export function BarChartDemo() {41return (42<ChartContainer config={chartConfig} className="min-h-[200px] w-full">43<BarChart data={chartData}>44<CartesianGrid vertical={false} />45<XAxis dataKey="month" tickLine={false} axisLine={false} />46<Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />47<Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />48<ChartTooltip content={<ChartTooltipContent />} />49</BarChart>50</ChartContainer>51)52}53```5455--------------------------------5657### shadcn/ui Chart Component - ChartConfig with Custom Colors5859Source: https://ui.shadcn.com/docs/components/chart6061You can define custom colors directly in the configuration using hex values or CSS variables.6263```tsx64const chartConfig = {65desktop: {66label: "Desktop",67color: "#2563eb",68theme: {69light: "#2563eb",70dark: "#60a5fa",71},72},73mobile: {74label: "Mobile",75color: "var(--chart-2)",76},77} satisfies import("@/components/ui/chart").ChartConfig78```7980--------------------------------8182### shadcn/ui Chart Component - CSS Variables8384Source: https://ui.shadcn.com/docs/components/chart8586Add chart color variables to your globals.css for consistent theming.8788```css89:root {90/* Chart colors */91--chart-1: oklch(0.646 0.222 41.116);92--chart-2: oklch(0.6 0.118 184.704);93--chart-3: oklch(0.546 0.198 38.228);94--chart-4: oklch(0.596 0.151 343.253);95--chart-5: oklch(0.546 0.158 49.157);96}9798.dark {99--chart-1: oklch(0.488 0.243 264.376);100--chart-2: oklch(0.696 0.17 162.48);101--chart-3: oklch(0.698 0.141 24.311);102--chart-4: oklch(0.676 0.172 171.196);103--chart-5: oklch(0.578 0.192 302.85);104}105```106107--------------------------------108109### shadcn/ui Chart Component - Line Chart Example110111Source: https://ui.shadcn.com/docs/components/chart112113Creating a line chart with shadcn/ui charts component.114115```tsx116import { Line, LineChart, CartesianGrid, XAxis, YAxis } from "recharts"117import { ChartContainer, ChartTooltipContent } from "@/components/ui/chart"118119const chartConfig = {120price: {121label: "Price",122color: "var(--chart-1)",123},124} satisfies import("@/components/ui/chart").ChartConfig125126const chartData = [127{ month: "January", price: 186 },128{ month: "February", price: 305 },129{ month: "March", price: 237 },130{ month: "April", price: 203 },131{ month: "May", price: 276 },132]133134export function LineChartDemo() {135return (136<ChartContainer config={chartConfig} className="min-h-[200px]">137<LineChart data={chartData}>138<CartesianGrid vertical={false} />139<XAxis dataKey="month" tickLine={false} axisLine={false} />140<YAxis tickLine={false} axisLine={false} tickFormatter={(value) => `$${value}`} />141<Line142dataKey="price"143stroke="var(--color-price)"144strokeWidth={2}145dot={false}146/>147<ChartTooltip content={<ChartTooltipContent />} />148</LineChart>149</ChartContainer>150)151}152```153154--------------------------------155156### shadcn/ui Chart Component - Area Chart Example157158Source: https://ui.shadcn.com/docs/components/chart159160Creating an area chart with gradient fill and legend.161162```tsx163import { Area, AreaChart, XAxis, YAxis } from "recharts"164import {165ChartContainer,166ChartLegend,167ChartLegendContent,168ChartTooltipContent,169} from "@/components/ui/chart"170171const chartConfig = {172desktop: { label: "Desktop", color: "var(--chart-1)" },173mobile: { label: "Mobile", color: "var(--chart-2)" },174} satisfies import("@/components/ui/chart").ChartConfig175176export function AreaChartDemo() {177return (178<ChartContainer config={chartConfig} className="min-h-[200px]">179<AreaChart data={chartData}>180<XAxis dataKey="month" tickLine={false} axisLine={false} />181<YAxis tickLine={false} axisLine={false} />182<Area183dataKey="desktop"184fill="var(--color-desktop)"185stroke="var(--color-desktop)"186fillOpacity={0.3}187/>188<Area189dataKey="mobile"190fill="var(--color-mobile)"191stroke="var(--color-mobile)"192fillOpacity={0.3}193/>194<ChartTooltip content={<ChartTooltipContent />} />195<ChartLegend content={<ChartLegendContent />} />196</AreaChart>197</ChartContainer>198)199}200```201202--------------------------------203204### shadcn/ui Chart Component - Pie Chart Example205206Source: https://ui.shadcn.com/docs/components/chart207208Creating a pie/donut chart with shadcn/ui.209210```tsx211import { Pie, PieChart } from "recharts"212import {213ChartContainer,214ChartLegend,215ChartLegendContent,216ChartTooltipContent,217} from "@/components/ui/chart"218219const chartConfig = {220chrome: { label: "Chrome", color: "var(--chart-1)" },221safari: { label: "Safari", color: "var(--chart-2)" },222firefox: { label: "Firefox", color: "var(--chart-3)" },223} satisfies import("@/components/ui/chart").ChartConfig224225const pieData = [226{ browser: "Chrome", visitors: 275, fill: "var(--color-chrome)" },227{ browser: "Safari", visitors: 200, fill: "var(--color-safari)" },228{ browser: "Firefox", visitors: 187, fill: "var(--color-firefox)" },229]230231export function PieChartDemo() {232return (233<ChartContainer config={chartConfig} className="min-h-[200px]">234<PieChart>235<Pie236data={pieData}237dataKey="visitors"238nameKey="browser"239cx="50%"240cy="50%"241outerRadius={80}242/>243<ChartTooltip content={<ChartTooltipContent />} />244<ChartLegend content={<ChartLegendContent />} />245</PieChart>246</ChartContainer>247)248}249```250251--------------------------------252253### shadcn/ui ChartTooltipContent Props254255Source: https://ui.shadcn.com/docs/components/chart256257The ChartTooltipContent component accepts these props for customizing tooltip behavior.258259| Prop | Type | Default | Description |260|------|------|---------|-------------|261| `labelKey` | string | "label" | Key for tooltip label |262| `nameKey` | string | "name" | Key for tooltip name |263| `indicator` | "dot" \| "line" \| "dashed" | "dot" | Indicator style |264| `hideLabel` | boolean | false | Hide label |265| `hideIndicator` | boolean | false | Hide indicator |266267--------------------------------268269### shadcn/ui Chart Component - Accessibility270271Source: https://ui.shadcn.com/docs/components/chart272273Enable keyboard navigation and screen reader support by adding the accessibilityLayer prop.274275```tsx276<BarChart accessibilityLayer data={chartData}>277<CartesianGrid vertical={false} />278<XAxis dataKey="month" />279<Bar dataKey="desktop" fill="var(--color-desktop)" />280<ChartTooltip content={<ChartTooltipContent />} />281</BarChart>282```283284This adds:285- Keyboard arrow key navigation286- ARIA labels for chart elements287- Screen reader announcements for data values288289--------------------------------290291### shadcn/ui Chart Component - Recharts Dependencies292293Source: https://ui.shadcn.com/docs/components/chart294295The chart component requires the following Recharts dependencies to be installed.296297```bash298pnpm add recharts299npm install recharts300yarn add recharts301```302303Recharts provides the following chart types:304- Area, Bar, Line, Pie, Composed305- Radar, RadialBar, Scatter306- Funnel, Treemap307