Home / Comparison Charts / Horizontal Bar Chart

Horizontal Bar Chart in HTML, CSS and JavaScript

A horizontal bar chart is a bar chart turned on its side. This free template draws one with plain SVG and vanilla JavaScript, no chart library. The example shows the most borrowed library books of the year.

Open live demoDownload HTML fileView code on GitHub
Horizontal Bar Chart made with HTML and JavaScript showing the most borrowed library books of the year

What is a horizontal bar chart?

A horizontal bar chart is a bar chart turned on its side. The bars grow from left to right, and the labels sit on the left where there is plenty of room to read them.

It is the best choice for rankings and for long labels, like book titles, product names or survey questions. People read top to bottom, so a sorted horizontal chart reads like a top 10 list.

At a glance

Best for
Rankings and items with long names
Data you need
A name and a number for each item, sorted
Skip it when
Dates or time. Time should run left to right.
Made with
HTML, CSS and vanilla JavaScript. No library.

When to use a horizontal bar chart

  • Top 10 products, pages or search terms
  • Survey questions with long wording
  • Country or city rankings
  • Most read articles or most borrowed books

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

const books = [['The Quiet Orchard', 876], ['Salt and Silver', 1163]];
books.sort((a, b) => b[1] - a[1]); // biggest first
const max = books[0][1];

books.forEach(([title, count], i) => {
  const y = i * 36;
  drawText(240, y + 20, title, 'end');       // label on the left
  drawRect(250, y + 6, count / max * 400, 22, '#3b4cca');
});

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 horizontal-bar-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

When should I use a horizontal bar chart instead of a vertical one?

Use horizontal bars when your labels are long or when you are showing a ranking. Use vertical bars for short labels and for anything over time.

Should horizontal bars be sorted?

Almost always. Sorting from largest to smallest turns the chart into a ranked list that people can read in one pass.

How do I stop long labels from being cut off?

Give the left side a fixed width for labels, and on small screens put the label above the bar. This template does both.

Is a horizontal bar chart the same as a row chart?

Yes. Row chart and horizontal bar chart are two names for the same thing.

More comparison charts