Home / Part to Whole / Treemap

Treemap in HTML, CSS and JavaScript

A treemap shows parts of a whole as boxes, where the size of each box matches its value. This free template draws one with plain SVG and vanilla JavaScript, no chart library. The example shows bookshop sales by section and genre.

Open live demoDownload HTML fileView code on GitHub
Treemap made with HTML and JavaScript showing bookshop sales by section and genre

What is a treemap?

A treemap shows parts of a whole as boxes, where the size of each box matches its value. Boxes can sit inside bigger boxes, so a treemap shows groups and the items inside them at the same time.

It uses every pixel of space, which makes it great for data with many items. The squarified layout used here keeps boxes close to square, so they are easier to compare and to label.

At a glance

Best for
Many items grouped into categories
Data you need
A group, a name and a value for each item
Skip it when
Small differences that must be compared exactly.
Made with
HTML, CSS and vanilla JavaScript. No library.

When to use a treemap

  • Sales by category and product
  • Budget by department and project
  • Disk or storage use by folder
  • Market size by sector and company

When to pick another chart

What you get in this template

How the code works

Here is a short version of the idea behind this treemap. The full file adds the axis, labels, tooltips, sorting and resizing on top of it.

// Slice a rectangle into strips, one box per item (simple treemap)
const items = [['Mystery', 84], ['Romance', 67], ['Cooking', 45], ['Travel', 22]];
const total = items.reduce((a, i) => a + i[1], 0);
let x = 0, y = 0, w = 600, h = 300;

items.forEach(([name, value], i) => {
  const share = value / items.slice(i).reduce((a, it) => a + it[1], 0);
  if (w >= h) { const bw = w * share; drawRect(x, y, bw - 2, h - 2, '#b5473a'); drawText(x + 8, y + 20, name); x += bw; w -= bw; }
  else { const bh = h * share; drawRect(x, y, w - 2, bh - 2, '#b5473a'); drawText(x + 8, y + 20, name); y += bh; h -= bh; }
});

The example uses four tiny helpers that create SVG shapes. Put them above the code and it runs as is.

const svg = document.querySelector('svg');
function make(tag, attrs) {
  const n = document.createElementNS('http://www.w3.org/2000/svg', tag);
  for (const k in attrs) n.setAttribute(k, attrs[k]);
  svg.appendChild(n);
  return n;
}
function drawRect(x, y, width, height, fill) { return make('rect', { x, y, width, height, fill }); }
function drawCircle(cx, cy, r, fill) { return make('circle', { cx, cy, r, fill }); }
function drawLine(x1, y1, x2, y2, stroke, width, dash) {
  return make('line', { x1, y1, x2, y2, stroke, 'stroke-width': width || 1, 'stroke-dasharray': dash || 'none' });
}
function drawText(x, y, str, anchor) {
  const t = make('text', { x, y, 'text-anchor': anchor || 'start' });
  t.textContent = str;
  return t;
}

How to add it to your website

  1. Download the file. Click Download HTML file above. You get one file called treemap.html with everything inside.
  2. Change the data. Open the file in any code editor and find the DATA list near the bottom. Replace the names and numbers with your own.
  3. Change the colors and text. Colors are at the top of the style tag. The title, subtitle and main point are plain HTML near the top of the body.
  4. Put it online. Upload the file to any host, such as GitHub Pages, Netlify or your own server, or paste it into your site. There is no build step.

Questions people ask

What is a treemap used for?

A treemap shows how a total splits into many parts and groups, using box size for value. It is common for sales, budgets, storage and market data.

What is a squarified treemap?

It is a layout method that keeps boxes as close to square as possible. Square boxes are easier to compare and have more room for labels than long thin strips.

How many levels can a treemap show?

Two levels is the sweet spot. You can nest more, but the smaller boxes quickly become too small to read. Use zooming for deeper data.

Is a treemap better than a pie chart?

For many items, yes. A pie gets unreadable after five slices, while a treemap can show dozens of items and their groups in the same space.

More part to whole