GitHub

Chart

Beautiful charts built with Recharts. Copy and paste into your apps.

CMSPayload CMS Integration

Charts can display CMS-managed data with configurable labels, colors, and data series that content editors can update.

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/ui

Usage

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

PropTypeDefaultDescription
config*ChartConfig——

ChartConfig

type ChartConfig = {
  [key: string]: {
    label?: ReactNode
    icon?: ComponentType
    color?: string
    theme?: Record<"light" | "dark", string>
  }
}

ChartTooltipContent

PropTypeDefaultDescription
separatorstring——
wrapperClassNamestring——
labelClassNamestring——
formatterFormatter<TValue, TName>——
contentStyleCSSProperties——
itemStyleCSSProperties——
labelStyleCSSProperties——
labelFormatter(label: any, payload: Payload<TValue, TName>[]) => ReactNode——
labelany——
payloadPayload<TValue, TName>[]——
itemSorter(item: Payload<TValue, TName>) => string | number——
accessibilityLayerenum——
activeenum—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.
includeHiddenenum—If true, then Tooltip will information about hidden series (defaults to false). Interacting with the hide property of Area, Bar, Line, Scatter.
allowEscapeViewBoxAllowInDimension——
animationDurationnumber——
animationEasingenum——
coordinatePartial<Coordinate>——
cursorenum——
filterNullenum——
defaultIndexnumber——
isAnimationActiveenum——
offsetnumber——
payloadUniqByenum——
positionPartial<Coordinate>——
reverseDirectionAllowInDimension——
sharedenum——
triggerenum——
useTranslate3denum——
viewBoxCartesianViewBox——
wrapperStyleCSSProperties——
hideLabelenumfalse—
hideIndicatorenumfalse—
indicatorenumdot—
nameKeystring——
labelKeystring——