Home / Trends Over Time / Step Line Chart

Step Line Chart in HTML, CSS and JavaScript

A step line chart shows a value that stays flat and then jumps to a new level, like a price, a rate or a plan limit. This free template draws one with plain SVG and vanilla JavaScript, no chart library. The example shows the interest rate on a savings account from 2021 to 2026.

Open live demoDownload HTML fileView code on GitHub
Step Line Chart made with HTML and JavaScript showing the interest rate on a savings account from 2021 to 2026

What is a step line chart?

A step line chart shows a value that stays flat and then jumps to a new level, like a price, a rate or a plan limit. Instead of a sloped line between points, it draws a flat line and then a straight jump.

A normal line chart would suggest the rate slowly slid from one level to the next, which never happened. The steps show the truth: the rate stayed the same until the day it changed.

At a glance

Best for
Values that change in jumps and hold between them
Data you need
The date of each change and the new value
Skip it when
Values measured all the time, like temperature.
Made with
HTML, CSS and vanilla JavaScript. No library.

When to use a step line chart

  • Interest rates and savings rates
  • Prices and subscription fees
  • Stock levels and plan limits
  • Tax bands and minimum wage history

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

const changes = [[2021, 0.10], [2022.2, 0.75], [2023, 3.00], [2023.5, 3.60], [2025, 2.85]];
const end = 2026.7;
const x = t => 40 + (t - 2021) / (end - 2021) * 540, y = r => 280 - r / 4 * 260;

let d = 'M' + x(changes[0][0]) + ',' + y(changes[0][1]);
changes.slice(1).forEach(([t, rate]) => { d += 'H' + x(t) + 'V' + y(rate); });
d += 'H' + x(end);
make('path', { d, fill: 'none', stroke: '#1b5e4a', 'stroke-width': 3 });
changes.forEach(([t, rate]) => drawCircle(x(t), y(rate), 5, '#1b5e4a'));

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 step-line-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 a step line chart?

It is a line chart that moves in flat steps and sudden jumps, instead of slopes. It is used for values that stay the same until they change, like rates and prices.

When should I use a step chart instead of a line chart?

Use a step chart when the value holds steady between changes. A normal line would draw a slope that suggests gradual change, which is misleading for rates, prices or limits.

How do I draw steps in SVG?

Use the H command to draw flat to the next change date, then V to jump up or down to the new value. Repeat for each change and finish with H to the end date.

Should the step happen before or after the date?

In most cases the new value starts on the change date, so draw flat until that date and then jump. This template works that way.

More trends over time