# How it works > d3-3d does the math, React draws the SVG. ## d3-3d does the math [d3-3d](https://niekes.github.io/d3-3d/) never touches the DOM. It turns 3D points into projected 2D points, the centroid of every shape and the orientation of every face. chart3d/cn projects the data on every render and returns plain `` and `` elements. ## One camera per chart `ChartContainer3D` keeps the view, the yaw and the tilt, in React state and builds a `camera3D()` from it. Every shape of the chart hangs on that camera with `.camera(camera)`, so a drag changes the state and the next render projects everything again. ## Depth by the order of the elements SVG has no depth buffer: what comes later in the document is drawn on top. The charts sort their elements back to front with d3-3d's `sort`, and stable React keys make React move the existing DOM nodes rather than rebuild them. What stands side by side on a floor - bars, the ribbons of an area, the cells of a surface - sorts by its footprint: how high a mark reaches says nothing about what it hides. Bars draw only the faces turned towards the camera, the ones d3-3d marks `ccw`. ## Rendered on the server Because the charts are SVG, the first frame is part of the HTML. The pages of this site are rendered at build time, and the charts in them are visible before any JavaScript runs. ## Limits Sorting is exact for points and for what stands side by side on one floor - bars, the ribbons of an area, the cells of a surface - with `perspective` too. Geometry that intersects itself can show artifacts, such as a surface whose columns or rows do not ascend and so folds back over itself. SVG stays fast up to a few thousand elements; beyond that the canvas renderer is the better fit. # Introduction > 3D charts for shadcn/ui, built on d3-3d. chart3d/cn is a set of copy-paste chart components for [shadcn/ui](https://ui.shadcn.com). You install them with the shadcn CLI, the code lands in your project, and you own it from then on. - **SVG first.** The charts render on the server as plain SVG, print sharply and take CSS like any other element. For thousands of marks, a canvas takes over the marks, and the box, its labels and the tooltip stay SVG and HTML. - **Your theme.** The config names the colors, such as the `--chart-1` to `--chart-5` tokens your theme already has, dark mode included. - **The config you know.** `ChartContainer3D` takes the same `ChartConfig` as the chart of shadcn/ui, so one config drives a 2D and a 3D chart. - **Built on [d3-3d](https://niekes.github.io/d3-3d/).** d3-3d does the projection, the depth sorting and the face orientation; React draws the result. For an AI assistant, the docs come as Markdown too: [llms.txt](https://chart3dcn.niekes.com/llms.txt) lists every page, [llms-full.txt](https://chart3dcn.niekes.com/llms-full.txt) holds them all, and every page has a copy at its address plus `.md`. # 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: ```package-install 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: ```package-install npx shadcn@latest add niekes/chart3dcn/bar-chart-3d#v ``` 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`: ```json title="components.json" { "registries": { "@chart3dcn": "https://chart3dcn.niekes.com/r/{name}.json" } } ``` Then the items install by their short name: ```package-install 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: ```package-install 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: ```css title="app/globals.css" @import "tailwindcss"; @source "../node_modules/chart3dcn/dist"; ``` Then import the charts from the package: ```tsx 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](https://chart3dcn.niekes.com/docs/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](https://chart3dcn.niekes.com/docs/components/chart-3d#stability). ## Your first chart ```tsx 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 ( ) } ``` 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. # Theming > Colors, slots, shading and dark mode. Customization climbs a ladder. **Behaviour is a prop**: the keys and gestures (`keymap`), the renderer, the durations, and what shows (`legend`, `shortcuts`, `gizmo`) - see [chart-3d](https://chart3dcn.niekes.com/docs/components/chart-3d). **Looks are CSS**: the theme tokens below and the slots, never style props. **Identity is fixed**: how hover highlights, how the gestures are gated and that every renderer draws the same scene are decisions, recorded in the [ADRs](https://github.com/Niekes/chart3dcn/tree/main/docs/adr) - to change those, edit your copy of the registry file: you own it. ## The config `ChartContainer3D` takes a `ChartConfig`: one entry per series, with a label and a color. It has the same shape as the config of the shadcn/ui chart, so you can share one between a 2D and a 3D chart. ```tsx const chartConfig = { desktop: { label: "Desktop", color: "var(--chart-1)" }, mobile: { label: "Mobile", theme: { light: "oklch(0.6 0.118 184.704)", dark: "oklch(0.696 0.17 162.48)" }, }, } satisfies ChartConfig ``` The container turns every key into a CSS variable, `--color-desktop` and `--color-mobile`, scoped to the chart; a `ChartLegend3D` placed on its own scopes them itself. A key may hold any text: a character a CSS identifier cannot hold, such as the space of `Net sales`, is escaped, and a chart of your own reads the variable of a key with `colorOf(key)`. `color` is used in both themes, `theme` sets one color per theme. The container writes its `id`, the keys and the colors into a `