chart
shadcn/chartFreeComponent
shadcn publishes no description for this item.
Live preview
live · rendered by the registry
Rendered by shadcn on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add chartSource
1"use client"23import * as React from "react"4import * as RechartsPrimitive from "recharts"56import { cn } from "@/lib/utils"78// Format: { THEME_NAME: CSS_SELECTOR }9const THEMES = { light: "", dark: ".dark" } as const1011export type ChartConfig = {12 [k in string]: {13 label?: React.ReactNode14 icon?: React.ComponentType15 } & (16 | { color?: string; theme?: never }17 | { color?: never; theme: Record<keyof typeof THEMES, string> }18 )19}2021type ChartContextProps = {22 config: ChartConfig23}2425const ChartContext = React.createContext<ChartContextProps | null>(null)2627function useChart() {28 const context = React.useContext(ChartContext)2930 if (!context) {31 throw new Error("useChart must be used within a <ChartContainer />")32 }3334 return context35}3637const ChartContainer = React.forwardRef<38 HTMLDivElement,39 React.ComponentProps<"div"> & {40 config: ChartConfig41 children: React.ComponentProps<42 typeof RechartsPrimitive.ResponsiveContainer43 >["children"]44 }45>(({ id, className, children, config, ...props }, ref) => {46 const uniqueId = React.useId()47 const chartId = `chart-${id || uniqueId.replace(/:/g, "")}`4849 return (50 <ChartContext.Provider value={{ config }}>51 <div52 data-chart={chartId}53 ref={ref}54 className={cn(55 "flex aspect-video justify-center text-xs [&_.recharts-cartesian-axis-tick_text]:fill-muted-foreground [&_.recharts-cartesian-grid_line[stroke='#ccc']]:stroke-border/50 [&_.recharts-curve.recharts-tooltip-cursor]:stroke-border [&_.recharts-dot[stroke='#fff']]:stroke-transparent [&_.recharts-layer]:outline-none [&_.recharts-polar-grid_[stroke='#ccc']]:stroke-border [&_.recharts-radial-bar-background-sector]:fill-muted [&_.recharts-rectangle.recharts-tooltip-cursor]:fill-muted [&_.recharts-reference-line_[stroke='#ccc']]:stroke-border [&_.recharts-sector[stroke='#fff']]:stroke-transparent [&_.recharts-sector]:outline-none [&_.recharts-surface]:outline-none",56 className57 )}58 {...props}59 >60 <ChartStyle id={chartId} config={config} />61 <RechartsPrimitive.ResponsiveContainer>62 {children}63 </RechartsPrimitive.ResponsiveContainer>64 </div>65 </ChartContext.Provider>66 )67})68ChartContainer.displayName = "Chart"6970const ChartStyle = ({ id, config }: { id: string; config: ChartConfig }) => {71 const colorConfig = Object.entries(config).filter(72 ([, config]) => config.theme || config.color73 )7475 if (!colorConfig.length) {76// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from shadcn
All 62 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| accordionaccordion | shadcn | Components | Free | no deps | |
| alertalert | shadcn | Components | Free | no deps | |
| alert-dialogalert-dialog | shadcn | Components | Free | no deps | |
| aspect-ratioaspect-ratio | shadcn | Components | Free | no deps | |
| attachmentattachment | shadcn | Components | Free | no deps | |
| avataravatar | shadcn | Components | Free | no deps | |
| badgebadge | shadcn | Components | Free | no deps | |
| breadcrumbbreadcrumb | shadcn | Components | Free | no deps |
