Home / Part to Whole / Icicle Chart

Icicle Chart in HTML, CSS and JavaScript

An icicle chart shows a hierarchy as rows of blocks. This free template draws one with plain SVG and vanilla JavaScript, no chart library. The example shows what is filling up a laptop drive.

Open live demoDownload HTML fileView code on GitHub
Icicle Chart made with HTML and JavaScript showing what is filling up a laptop drive

What is a icicle chart?

An icicle chart shows a hierarchy as rows of blocks. The first column is the whole, the next column splits it into groups, and the next splits each group again. Block height shows size.

It is the flat, rectangle version of a sunburst, and it is easier to label. Clicking a block zooms in so its parts fill the whole height, which makes deep data easy to dig into.

At a glance

Best for
Hierarchies you want to zoom into
Data you need
A path of levels and a value for each leaf
Skip it when
Only one level. Use a bar chart.
Made with
HTML, CSS and vanilla JavaScript. No library.

When to use a icicle chart

  • Disk or cloud storage use by folder
  • Budgets by department, team and line item
  • Website sections and pages by traffic
  • Company structure by headcount

When to pick another chart

  • Treemap: you want every item visible at once in a compact box
  • Sunburst Chart: you want a round, more visual layout

What you get in this template

How the code works

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

const tree = [['Photos', [['Family', 58], ['Travel', 31]]], ['Videos', [['Home videos', 61], ['Downloads', 23]]]];
const total = tree.reduce((a, [, kids]) => a + kids.reduce((s, k) => s + k[1], 0), 0);
const h = 300, colW = 180;
let y = 0;

drawRect(0, 0, colW - 2, h, '#16202c');                       // the whole
tree.forEach(([name, kids]) => {
  const size = kids.reduce((s, k) => s + k[1], 0);
  drawRect(colW, y, colW - 2, size / total * h - 2, '#3a6ea5'); // folder
  let ky = y;
  kids.forEach(([, v]) => { drawRect(colW * 2, ky, colW - 2, v / total * h - 2, '#7fa3cc'); ky += v / total * h; });
  y += size / total * h;
});

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 icicle-chart.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 an icicle chart?

An icicle chart shows a hierarchy as columns of blocks. Each column splits the one before it into smaller parts, and the size of each block matches its value.

What is the difference between an icicle chart and a sunburst chart?

They show the same data. The sunburst bends the levels into rings, and the icicle keeps them as straight columns, which makes labels easier to read.

How do I zoom in an icicle chart?

Click a block. Its parts spread out to fill the full height. Click the first block, or the same block again, to zoom back out.

Why is it called an icicle chart?

When drawn from top to bottom, the blocks hang down like icicles from a roof. This template draws it left to right, which fits labels better.

More part to whole