
3D Charts for shadcn/ui Where Depth Shows Data
chart3d/cn brings 3D charts to shadcn/ui: copy-paste components in plain SVG, in the colors of your theme, built on d3-3d. And every chart spends its third dimension on data, never on decoration.
Stefan Nieke
Think of the 3D chart you have seen most often. Chances are it is the tilted pie of office software: a thick disc, a glossy edge, one slice pulled out. It is also the 3D chart people ask for most. And it lies.
Its thickness adds no number. Worse, the tilt bends the numbers that are there: the slice at the front looks larger than its twin at the side, because perspective stretches what is close. The same goes for funnels, pyramids and bars drawn as cylinders. The third dimension is spent on looks, and the reader pays for it.
So when I built chart3d/cn, 3D charts for shadcn/ui, I wrote one rule down before anything else: the depth of every chart shows data, or the shape of the earth. A chart whose depth is decoration does not get in, however often it is asked for. Want a pie? You get a donut whose slices are as wide as their share and as tall as their value, so the depth carries a number too.
What chart3d/cn is
chart3d/cn is a set of chart components that work the way shadcn/ui does:
- You own the code. Install a chart with the shadcn CLI and its source lands in your project. Or install the same components from npm, if you would rather update than own.
- Your theme. A chart draws in the colors its config names, such as the
--chart-1to--chart-5tokens your theme already has, dark mode included. - The config you know.
ChartContainer3Dtakes the sameChartConfigas the chart of shadcn/ui, so one config drives a 2D and a 3D chart. - Plain SVG. The charts render on the server, print sharply and take CSS like any other element.
There are twelve charts: bar (now with stacks), bar field, area, line, scatter and surface in a box with axes; a globe, places, routes and a choropleth on the earth; and the donut. Every one is in the components overview.
You turn a chart by holding ⌘ on a Mac, or Ctrl elsewhere, and dragging, or by focusing it and using the arrow keys. A plain drag never hijacks the page: it only inspects.
Getting started
In a project set up with shadcn/ui, add a chart with the CLI:
npx shadcn@latest add niekes/chart3dcn/bar-chart-3d
Or take the same components from npm:
npm install chart3dcn
Then a first chart looks like any shadcn chart, with one more dimension:
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>
)
}
The desktop bars draw in your --chart-1, light and dark. To restyle a part of the chart, the box, the tooltip, the legend, you target its data-slot from your stylesheet. There are no style props: looks are CSS, behaviour is a prop.
How it works
Under the hood sits d3-3d, which I wrote about earlier. It never touches the DOM. It turns 3D points into projected 2D points, works out the centroid of every shape and which way every face points.
Each chart keeps one camera in React state. A drag changes the state, the next render projects the data again, and React draws the result as plain <path> and <circle> elements.
SVG has no depth buffer: what comes later in the document is drawn on top. So the charts sort their shapes back to front, and stable React keys let React move the existing nodes instead of rebuilding them. Because the result is SVG, the first frame is part of the HTML. The charts on the docs site are visible before any JavaScript runs.
The WebGL renderer that was not faster
For thousands of marks, SVG gets slow, and a canvas takes over the marks while the box, its labels and the tooltip stay SVG and HTML.
The obvious next step was WebGL, so I built a WebGL renderer and measured it. It drew no faster than the canvas: the globe's 2,500 tiles took about 40 ms a frame on both. A CPU profile showed why. The projection itself was about 5 % of a frame. The time went into the work around it: the chart's render, sorting, React. Filling the marks on the GPU saves time nobody was spending.
So the WebGL renderer is gone, and three.js is not coming in either. A frame gets faster by doing less on the CPU: caching what the camera does not change, cheaper colors, fewer faces. The decision is written down, together with what would reopen it: a renderer that projects on the GPU and a measurement that shows it faster on the same demo.
That is a pattern in the whole project. The choices that shape what a chart is, how hover lights a mark, how the gestures work, that depth shows data, are recorded as decisions in the repo, with the options that lost.
Try it
chart3d/cn is free and open source, kept alive by one maintainer in spare time. If it saves you time, a sponsorship keeps it going.