Chart
optics/chartFreeComponent
Beautiful charts built with Recharts.
See it running
Open the demo on optics
optics.agusmayol.com.ar/components/chartInstall it
npx shadcn@latest add https://optics.agusmayol.com.ar/r/chart.jsonSource
1"use client";2import * as React from "react";3import * as RechartsPrimitive from "recharts";45import { cn } from "@/lib/utils";67// Format: { THEME_NAME: CSS_SELECTOR }8const THEMES = {9 light: "",10 dark: ".dark",11};1213const ChartContext = React.createContext(null);1415function useChart() {16 const context = React.useContext(ChartContext);1718 if (!context) {19 throw new Error("useChart must be used within a <ChartContainer />");20 }2122 return context;23}2425function ChartContainer({26 id = undefined,27 className = "",28 children = null,29 config = {},30 ...props31}) {32 const uniqueId = React.useId();33 const chartId = `chart-${id || uniqueId.replace(/:/g, "")}`;3435 return (36 <ChartContext.Provider value={{ config }}>37 <div38 data-slot="chart"39 data-chart={chartId}40 className={cn(41 "[&_.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",42 className,43 )}44 {...props}45 >46 <ChartStyle id={chartId} config={config} />47 <RechartsPrimitive.ResponsiveContainer>48 {children}49 </RechartsPrimitive.ResponsiveContainer>50 </div>51 </ChartContext.Provider>52 );53}5455const ChartStyle = ({ id = "", config = {} }) => {56 const colorConfig = Object.entries(config).filter(57 ([, config]) => config.theme || config.color,58 );5960 if (!colorConfig.length) {61 return null;62 }6364 return (65 <style66 dangerouslySetInnerHTML={{67 __html: Object.entries(THEMES)68 .map(69 ([theme, prefix]) => `70${prefix} [data-chart=${id}] {71${colorConfig72 .map(([key, itemConfig]) => {73 const color = itemConfig.theme?.[theme] || itemConfig.color;74 return color ? ` --color-${key}: ${color};` : null;75 })76 .join("\n")}77}78`,79 )80 .join("\n"),81 }}82 />83 );84};8586const ChartTooltip = RechartsPrimitive.Tooltip;8788function ChartTooltipContent({89 active = false,90 payload = [],91 className = "",92 indicator = "dot",93 hideLabel = false,94 hideIndicator = false,95// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from optics
All 79 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | optics | Components | Free | 5 deps | |
| Alertalert | optics | Components | Free | 4 deps | |
| Alert Dialogalert-dialog | optics | Components | Free | 4 deps | |
| Aspect Ratioaspect-ratio | optics | Components | Free | 2 deps | |
| Auto Heightauto-height | optics | Components | Free | 3 deps | |
| Avataravatar | optics | Components | Free | 3 deps | |
| Badgebadge | optics | Components | Free | 4 deps | |
| Breadcrumbbreadcrumb | optics | Components | Free | 4 deps |
