Chart
Beautiful charts built with Recharts. Copy and paste into your apps.
CMS Demo
See how content editors configure charts in a CMS:
Loading...
CMS ConfigurationSimulates Payload CMS chart configuration with data management
Payload Block Example
// In your Payload CMS blocks config
export const ChartBlock = {
slug: "chart",
fields: [
{ name: "title", type: "text", localized: true },
{
name: "type",
type: "select",
options: [
{ label: "Bar", value: "bar" },
{ label: "Line", value: "line" },
{ label: "Area", value: "area" },
],
},
{
name: "data",
type: "array",
fields: [
{ name: "label", type: "text", required: true },
{ name: "value", type: "number", required: true },
],
},
{
name: "config",
type: "group",
fields: [
{ name: "showGrid", type: "checkbox", defaultValue: true },
{ name: "showLegend", type: "checkbox", defaultValue: false },
],
},
],
}Rendering the Chart
import {
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from "@mordecai-design-system/ui"
import { Bar, BarChart, XAxis, YAxis } from "recharts"
export function DataChart({ title, data, config }) {
const chartConfig = {
value: {
label: "Value",
color: "hsl(var(--primary))",
},
}
return (
<div>
<h3>{title}</h3>
<ChartContainer config={chartConfig}>
<BarChart data={data}>
<XAxis dataKey="label" />
<YAxis />
<ChartTooltip content={<ChartTooltipContent />} />
<Bar dataKey="value" fill="var(--color-value)" />
</BarChart>
</ChartContainer>
</div>
)
}Basic Usage
Loading...
ChartBar chart with data visualization
Installation
pnpm add @corew500/uiUsage
import {
ChartContainer,
ChartTooltip,
ChartTooltipContent,
ChartLegend,
ChartLegendContent,
} from "@mordecai-design-system/ui"const chartConfig = {
desktop: {
label: "Desktop",
color: "hsl(var(--chart-1))",
},
mobile: {
label: "Mobile",
color: "hsl(var(--chart-2))",
},
}
<ChartContainer config={chartConfig}>
<BarChart data={data}>
<Bar dataKey="desktop" fill="var(--color-desktop)" />
<Bar dataKey="mobile" fill="var(--color-mobile)" />
<ChartTooltip content={<ChartTooltipContent />} />
</BarChart>
</ChartContainer>Examples
Line Chart
<ChartContainer config={chartConfig}>
<LineChart data={data}>
<XAxis dataKey="month" />
<YAxis />
<Line dataKey="value" stroke="var(--color-value)" />
<ChartTooltip content={<ChartTooltipContent />} />
</LineChart>
</ChartContainer>Area Chart
<ChartContainer config={chartConfig}>
<AreaChart data={data}>
<Area
dataKey="value"
fill="var(--color-value)"
stroke="var(--color-value)"
/>
<ChartTooltip content={<ChartTooltipContent />} />
</AreaChart>
</ChartContainer>With Legend
<ChartContainer config={chartConfig}>
<BarChart data={data}>
<Bar dataKey="desktop" fill="var(--color-desktop)" />
<Bar dataKey="mobile" fill="var(--color-mobile)" />
<ChartLegend content={<ChartLegendContent />} />
</BarChart>
</ChartContainer>Sub-components
| Component | Description |
|-----------|-------------|
| ChartContainer | Root container with config context |
| ChartTooltip | Recharts Tooltip wrapper |
| ChartTooltipContent | Styled tooltip content |
| ChartLegend | Recharts Legend wrapper |
| ChartLegendContent | Styled legend content |
| ChartStyle | Dynamic CSS variables |
Props
ChartContainer
| Prop | Type | Default | Description |
|---|---|---|---|
| config* | ChartConfig | — | — |
ChartConfig
type ChartConfig = {
[key: string]: {
label?: ReactNode
icon?: ComponentType
color?: string
theme?: Record<"light" | "dark", string>
}
}ChartTooltipContent
| Prop | Type | Default | Description |
|---|---|---|---|
| separator | string | — | — |
| wrapperClassName | string | — | — |
| labelClassName | string | — | — |
| formatter | Formatter<TValue, TName> | — | — |
| contentStyle | CSSProperties | — | — |
| itemStyle | CSSProperties | — | — |
| labelStyle | CSSProperties | — | — |
| labelFormatter | (label: any, payload: Payload<TValue, TName>[]) => ReactNode | — | — |
| label | any | — | — |
| payload | Payload<TValue, TName>[] | — | — |
| itemSorter | (item: Payload<TValue, TName>) => string | number | — | — |
| accessibilityLayer | enum | — | — |
| active | enum | — | If true, then Tooltip is always displayed, once an activeIndex is set by mouse over, or programmatically. If false, then Tooltip is never displayed. If active is undefined, Recharts will control when the Tooltip displays. This includes mouse and keyboard controls. |
| includeHidden | enum | — | If true, then Tooltip will information about hidden series (defaults to false). Interacting with the hide property of Area, Bar, Line, Scatter. |
| allowEscapeViewBox | AllowInDimension | — | — |
| animationDuration | number | — | — |
| animationEasing | enum | — | — |
| coordinate | Partial<Coordinate> | — | — |
| cursor | enum | — | — |
| filterNull | enum | — | — |
| defaultIndex | number | — | — |
| isAnimationActive | enum | — | — |
| offset | number | — | — |
| payloadUniqBy | enum | — | — |
| position | Partial<Coordinate> | — | — |
| reverseDirection | AllowInDimension | — | — |
| shared | enum | — | — |
| trigger | enum | — | — |
| useTranslate3d | enum | — | — |
| viewBox | CartesianViewBox | — | — |
| wrapperStyle | CSSProperties | — | — |
| hideLabel | enum | false | — |
| hideIndicator | enum | false | — |
| indicator | enum | dot | — |
| nameKey | string | — | — |
| labelKey | string | — | — |