Next.js Treemap Chart
A Next.js treemap chart for the App Router: an ApexCharts client component in AdminKit PRO that sizes a rectangle per value, with theme colors.
This component is only available in AdminKit PRO. Learn more.
A treemap shows which parts take up most of a whole. It divides a rectangle into tiles, one per value, and each tile's area is proportional to its value. In AdminKit for Next.js you draw it with ApexChart, a client component that wraps ApexCharts. See Chart setup for the component.
The tiles are only drawn in the browser, while your page is a server component that renders on the server. Write the chart as a client component file and render it from the page.
Example
"use client";
import { ApexChart } from "@/components/plugins/apexcharts";
type City = { x: string; y: number };
export function CitiesTreemap({ cities }: { cities: City[] }) {
return (
<div className="position-relative" style={{ minHeight: 365 }}>
<ApexChart
options={() => ({
series: [{ data: cities }],
legend: { show: false },
chart: { height: 350, type: "treemap" },
title: { text: "Basic Treemap" },
})}
/>
</div>
);
}Here the page owns the data and passes it as a prop. An array of plain objects can cross from a server component to a client component. The options function cannot, so it stays in the client file.
import { CitiesTreemap } from "@/components/cities-treemap";
import { pageMetadata } from "@/lib/site";
export const metadata = pageMetadata("/cities");
const cities = [
{ x: "New Delhi", y: 218 },
{ x: "Kolkata", y: 149 },
{ x: "Mumbai", y: 184 },
{ x: "Ahmedabad", y: 55 },
{ x: "Bengaluru", y: 84 },
{ x: "Pune", y: 31 },
{ x: "Chennai", y: 70 },
{ x: "Jaipur", y: 30 },
{ x: "Surat", y: 44 },
{ x: "Hyderabad", y: 68 },
{ x: "Lucknow", y: 28 },
{ x: "Indore", y: 19 },
{ x: "Kanpur", y: 29 },
];
export default function CitiesPage() {
return (
<div className="card">
<div className="card-body">
<CitiesTreemap cities={cities} />
</div>
</div>
);
}List /cities in src/data/pages.json, where pageMetadata() reads its title and description. With the static export, the page runs at build time, so the data is the data of the last build.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
options | (theme: Theme) => ApexOptions | The ApexCharts options, with series and chart.type. Called again with the new colors when the color mode changes |
Data and colors
Each tile is { x, y }: the label and the value that sets its area. With one series, the tiles are shades of the primary color, the wrapper's first default color.
plotOptions.treemap | Effect |
|---|---|
distributed: true | Each tile takes the next color of colors |
enableShades: false | No lighter and darker shades by value |
options={(theme) => ({
series: [{ data: cities }],
legend: { show: false },
chart: { height: 350, type: "treemap" },
colors: [
theme.primary,
theme.success,
theme.warning,
theme.danger,
theme.info,
],
plotOptions: { treemap: { distributed: true, enableShades: false } },
})}Several series make groups of tiles, each in its own color. Turn the legend on to name the groups.
Accessibility
- Tiles show their label in the chart, but small tiles may not have room for it, and the value shows in the tooltip on hover only. Give the values in a table where they matter.
- Areas are hard to compare by eye: when the ranking matters, also show a sorted list.
- Name the chart with
role="figure"andaria-labelonApexChart. Other props go to its<div>.
Related
- React treemap chart: the same chart in AdminKit for React (Vite)
- Bootstrap 5 treemap chart: the ApexCharts example of the HTML version
- Chart setup: the
ApexChart,ChartJsandVectorMapclient components - Next.js heatmap chart: values as colors in a grid
- ApexCharts demo: the line, area, bar, column, pie, heatmap, mixed and candlestick charts of the demo
Last updated on
Timeline chartPro
A Next.js timeline chart for the App Router: tasks from start to end date as an ApexCharts rangeBar in a client component of AdminKit PRO.
Lucide Icons
Next.js Lucide icons from lucide-react, used straight in server component pages: find an icon, import it by name, size and color it, label icon-only buttons.