Home / Comparison Charts / Dot Plot

Dot Plot in HTML, CSS and JavaScript

A dot plot shows values as dots on a line, one row per item. This free template draws one with plain SVG and vanilla JavaScript, no chart library. The example shows small, medium and large latte prices in eight cities.

Open live demoDownload HTML fileView code on GitHub
Dot Plot made with HTML and JavaScript showing small, medium and large latte prices in eight cities

What is a dot plot?

A dot plot shows values as dots on a line, one row per item. It is a simple way to compare several values for each item, like three sizes of coffee, without drawing three sets of bars.

Because a dot is a point and not a length, a dot plot does not need to start at zero. You can zoom in on the range where your data lives, which makes small differences easier to see.

At a glance

Best for
Two or three values per item, side by side
Data you need
A name and 2 or 3 numbers for each item
Skip it when
Showing amounts as size. Use bars for that.
Made with
HTML, CSS and vanilla JavaScript. No library.

When to use a dot plot

  • Prices of the same product in different cities
  • Low, average and high scores per class
  • Men and women, or two age groups, per country
  • Planned and actual values per project

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

const cities = [['Boston', [4.1, 4.9, 5.6]], ['Phoenix', [3.2, 3.8, 4.4]]];
const colors = ['#c7a8e8', '#8a5cc7', '#4b2a86'];
const x = price => 100 + (price - 3) / 3 * 480; // $3 to $6

cities.forEach(([city, prices], row) => {
  const y = 30 + row * 40;
  drawLine(x(Math.min(...prices)), y, x(Math.max(...prices)), y, '#e8e2ee', 6);
  prices.forEach((p, i) => drawCircle(x(p), y, 9, colors[i]));
});

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 dot-plot.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 dot plot?

A dot plot is a chart that shows values as dots along a line, one row per item. It is often used to compare two or three values for each item.

What is a Cleveland dot plot?

It is the same chart, named after the statistician William Cleveland, who showed that people read dot positions more accurately than bar areas.

Does a dot plot need to start at zero?

No. Dots mark a position, not a length, so you can start the axis near your lowest value. Bar charts are different and must start at zero.

What is the difference between a dot plot and a scatter plot?

A dot plot has categories on one side, like cities. A scatter plot has numbers on both axes and shows how two numbers relate.

More comparison charts