chart
9ui/chartFreeComponent
9ui publishes no description for this item.
Live preview
live · rendered by the registry
Rendered by 9ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://9ui.dev/r/chart.jsonSource
1"use client"23import * as React from "react"4import * as RechartsPrimitive from "recharts"56import { cn } from "@/lib/utils"78const THEMES = { light: "", dark: ".dark" } as const910export type ChartConfig = {11 [k in string]: {12 label?: React.ReactNode13 icon?: React.ComponentType14 } & (15 | { color?: string; theme?: never }16 | { color?: never; theme: Record<keyof typeof THEMES, string> }17 )18}1920type ChartContextProps = {21 config: ChartConfig22}2324const ChartContext = React.createContext<ChartContextProps | null>(null)2526function useChart() {27 const context = React.useContext(ChartContext)2829 if (!context) {30 throw new Error("useChart must be used within a <ChartContainer />")31 }3233 return context34}3536function ChartContainer({37 id,38 className,39 children,40 config,41 ...props42}: React.ComponentProps<"div"> & {43 config: ChartConfig44 children: React.ComponentProps<45 typeof RechartsPrimitive.ResponsiveContainer46 >["children"]47}) {48 const uniqueId = React.useId()49 const chartId = `chart-${id || uniqueId.replace(/:/g, "")}`5051 return (52 <ChartContext.Provider value={{ config }}>53 <div54 data-slot="chart"55 data-chart={chartId}56 className={cn(57 "[&_.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-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 flex aspect-video justify-center text-xs [&_.recharts-dot[stroke='#fff']]:stroke-transparent [&_.recharts-layer]:outline-hidden [&_.recharts-sector]:outline-hidden [&_.recharts-sector[stroke='#fff']]:stroke-transparent [&_.recharts-surface]:outline-hidden",58 className59 )}60 {...props}61 >62 <ChartStyle id={chartId} config={config} />63 <RechartsPrimitive.ResponsiveContainer>64 {children}65 </RechartsPrimitive.ResponsiveContainer>66 </div>67 </ChartContext.Provider>68 )69}7071const ChartStyle = ({ id, config }: { id: string; config: ChartConfig }) => {72 const colorConfig = Object.entries(config).filter(73 ([, config]) => config.theme || config.color74 )7576 if (!colorConfig.length) {77 return null78 }7980 return (81 <style82 dangerouslySetInnerHTML={{83 __html: Object.entries(THEMES)84 .map(85 ([theme, prefix]) => `86${prefix} [data-chart=${id}] {87${colorConfig88 .map(([key, itemConfig]) => {89// … truncated
What it pulls in
npm packages
Files it writes
More from 9ui
All 56 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| accordionaccordion | 9ui | Components | Free | no deps | |
| alertalert | 9ui | Components | Free | no deps | |
| alert-dialogalert-dialog | 9ui | Components | Free | no deps | |
| aspect-ratioaspect-ratio | 9ui | Components | Free | no deps | |
| autocompleteautocomplete | 9ui | Components | Free | no deps | |
| avataravatar | 9ui | Components | Free | no deps | |
| badgebadge | 9ui | Components | Free | no deps | |
| breadcrumbsbreadcrumbs | 9ui | Components | Free | no deps |
