Home / Distribution / Histogram
Histogram in HTML, CSS and JavaScript
A histogram is a chart that shows how a set of numbers is spread out. This free template draws one with plain SVG and vanilla JavaScript, no chart library. The example shows delivery times for 600 pizza orders.
What is a histogram?
A histogram is a chart that shows how a set of numbers is spread out. It groups the numbers into ranges, called bins, and draws a bar for each range. The taller the bar, the more values fall in that range.
It shows the shape of your data at a glance: where most values sit, whether there is a long tail, and whether there are two peaks. Changing the bin width can reveal more detail or smooth out noise.
At a glance
- Best for
- Seeing how one set of numbers is spread
- Data you need
- A list of numbers, usually 50 or more
- Skip it when
- Comparing separate categories. Use a bar chart.
- Made with
- HTML, CSS and vanilla JavaScript. No library.
When to use a histogram
- Delivery, wait or response times
- Order values or basket sizes
- Test scores or survey ratings
- Ages, heights or any measured value
When to pick another chart
- Box Plot: you want to compare the spread of several groups
- Density Plot: you want a smooth shape instead of bars
- Cumulative Distribution Chart: you want to read what share is under a value
What you get in this template
- Bins counted in plain JavaScript from the raw numbers
- A switch between 2, 5 and 10 minute bins
- A dashed line for the median and one for the 30 minute promise
- Bars past the promise use a stronger color
- Tooltips with the count and share of orders
- A data table that updates with the bin width
How the code works
Here is a short version of the idea behind this histogram. The full file adds the axis, labels, tooltips, sorting and resizing on top of it.
const minutes = [22.4, 18.9, 31.2, 26.5, 24.1, 41.8, 19.7, 27.3, 23.0, 35.6];
const binWidth = 5, from = 10, to = 50;
const bins = [];
for (let b = from; b < to; b += binWidth) bins.push(minutes.filter(v => v >= b && v < b + binWidth).length);
const max = Math.max(...bins), barW = 560 / bins.length;
bins.forEach((count, i) => {
const h = count / max * 250;
drawRect(40 + i * barW, 280 - h, barW - 1, h, '#f08a6c');
});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
- Download the file. Click Download HTML file above. You get one file called histogram.html with everything inside.
- 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.
- 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.
- 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 the difference between a histogram and a bar chart?
A histogram shows how numbers are spread across ranges, and its bars touch because the ranges join up. A bar chart compares separate items, like products, and its bars have gaps.
How do I choose the bin width for a histogram?
Try a few. Too wide and you lose the shape, too narrow and the bars get noisy. A good start is about 10 to 20 bins across your data. This template lets you switch widths to compare.
How many values do I need for a histogram?
At least 30, and ideally 100 or more. With fewer values, a strip plot or dot plot shows the data more honestly.
What does a skewed histogram mean?
It means the values bunch up on one side with a long tail on the other. Delivery times are a classic example: most are quick, but a few take much longer.
More distribution

032. Box Plot
Example: monthly rent for flats in six neighborhoods
033. Violin Plot
Example: coffee order waits at four cafes
034. Density Plot
Example: sunflower heights with and without plant feed
035. Ridgeline Plot
Example: daily high temperatures for each month of the year
036. Beeswarm Plot
Example: finish times for 300 marathon runners
037. Strip Plot
Example: train delays on five lines over 60 weekdays
038. Population Pyramid
Example: the age of a town in 2006 and 2026
039. Error Bar Chart
Example: battery life test results for five phones