Home / Comparison Charts / 100% Stacked Bar Chart

100% Stacked Bar Chart in HTML, CSS and JavaScript

A 100% stacked bar chart shows how a whole is split into shares. This free template draws one with plain SVG and vanilla JavaScript, no chart library. The example shows customer survey results for five grocery stores.

Open live demoDownload HTML fileView code on GitHub
100% Stacked Bar Chart made with HTML and JavaScript showing customer survey results for five grocery stores

What is a 100% stacked bar chart?

A 100% stacked bar chart shows how a whole is split into shares. Every bar has the same length because every bar is 100%. The colored pieces inside show what percent each answer or part takes.

This chart is the standard way to show survey answers on a scale, like very unhappy to very happy. Because all bars are the same length, you can compare the mix between groups even when the groups had different numbers of people.

At a glance

Best for
Survey answers and shares across groups
Data you need
Percentages per group that add up to 100
Skip it when
When the real totals matter. Use a normal stacked bar.
Made with
HTML, CSS and vanilla JavaScript. No library.

When to use a 100% stacked bar chart

  • Customer or staff survey results
  • Market share by region
  • Device mix of your visitors by country
  • Time spent on tasks by team

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 100% stacked bar chart. The full file adds the axis, labels, tooltips, sorting and resizing on top of it.

const answers = ['#b83a2f', '#e8935a', '#c9ccc4', '#7fbf8e', '#2e7d4f'];
const stores = [['Elm Road', [6, 10, 22, 38, 24]], ['Hillcrest', [3, 7, 15, 41, 34]]];
const width = 500; // 100% of the bar

stores.forEach(([name, shares], row) => {
  let x = 120;
  shares.forEach((pct, i) => {
    const w = pct / 100 * width;
    drawRect(x, 20 + row * 50, w, 34, answers[i]);
    x += w;
  });
});

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 100-percent-stacked-bar.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 100% stacked bar chart used for?

It shows shares of a whole. It is the most common chart for survey answers on a scale, because every group gets the same length and you compare the mix, not the size.

How do I show Likert scale survey results?

Use a 100% stacked bar with one bar per group and colors running from negative to positive answers. Put neutral in the middle, like this template does.

Do the values need to add up to 100?

Yes. Convert your counts to percentages first. If your numbers are counts, divide each by the row total and multiply by 100.

Why are the bars horizontal?

Group names like store names are easier to read on the left side. Horizontal bars also match the way people read a scale, from left to right.

More comparison charts