Installation

Add a 3D chart to a shadcn/ui project.

Requirements

A project set up with shadcn/ui: React 19 and Tailwind CSS 4, with a components.json. If you start from scratch, run npx shadcn@latest init first. The code of the registry compiles under the strict checks of TypeScript, but not yet with noUncheckedIndexedAccess, which create-t3-app turns on: in such a project, install the npm package instead.

Add a chart

Every chart is an item of the registry in the GitHub repository. Install one with the shadcn CLI:

npx shadcn@latest add niekes/chart3dcn/bar-chart-3d#v1.2.0

The CLI copies chart-3d.tsx, the shared core, and bar-chart-3d.tsx into your components/ui folder, and installs d3-3d and d3-array. The globe, the places, the routes and the choropleth chart also install d3-geo.

Pin a release with a tag:

npx shadcn@latest add niekes/chart3dcn/bar-chart-3d#v<version>

The tag pins chart-3d as well: every release tag points to a commit in which the charts depend on the core of the same release.

A namespace instead

The registry is also served as JSON from this site. Add it to the registries of your components.json:

components.json
{
  "registries": {
    "@chart3dcn": "https://chart3dcn.niekes.com/r/{name}.json"
  }
}

Then the items install by their short name:

npx shadcn@latest add @chart3dcn/bar-chart-3d

The namespace always serves the latest version. To pin a release, use the GitHub form above.

From npm

The same components ship as an npm package, for projects that would rather update with npm than own a copy:

npm install chart3dcn

The components style themselves with Tailwind classes, and Tailwind only generates the classes it finds. Add the package to its sources in your global CSS, with the path relative to that file:

app/globals.css
@import "tailwindcss";
@source "../node_modules/chart3dcn/dist";

Then import the charts from the package:

import { BarChart3D, ChartContainer3D, type ChartConfig } from "chart3dcn"

A chart draws in the colors its config names - the examples name --chart-1 to --chart-5, the tokens of a shadcn/ui theme - and the box, its labels and the tooltip read --muted, --muted-foreground, --border, --background and --foreground. A project without shadcn/ui defines them itself, see Theming.

Registry or npm? The registry copies the code into your project: you can change it, and you update it by hand. The package keeps the code in node_modules: you update it with npm, and you use it as it is. Which parts of it semver covers, see Stability.

Your first chart

import { BarChart3D } from "@/components/ui/bar-chart-3d"
import { ChartContainer3D, type ChartConfig } from "@/components/ui/chart-3d"

const chartData = [
  { month: "January", desktop: 186, mobile: 80 },
  { month: "February", desktop: 305, mobile: 200 },
  { month: "March", desktop: 237, mobile: 120 },
]

const chartConfig = {
  desktop: { label: "Desktop", color: "var(--chart-1)" },
  mobile: { label: "Mobile", color: "var(--chart-2)" },
} satisfies ChartConfig

export function Visitors() {
  return (
    <ChartContainer3D config={chartConfig} legend className="max-w-xl">
      <BarChart3D data={chartData} index="month" titles={{ y: "Visitors" }} />
    </ChartContainer3D>
  )
}

Hold ⌘ on a Mac, or Ctrl on Windows and Linux, and drag the chart to turn it, or focus it and use the arrow keys. Press ? on the chart for every shortcut.

Edit on GitHub

On this page