Home / Learn / How do I build an animated waterfall chart for profit and costs?

How do I build an animated waterfall chart for profit and costs?

A waterfall chart shows how revenue becomes net profit after each cost, with every bar starting where the previous one ended. We build it with plain HTML elements and CSS variables only: each bar knows its start and height as a percentage.

Steps

  1. Write the rows: revenue is positive, each cost is negative, and the last row "Net" is computed, not typed.
  2. Keep a running total: for each row compute the low point and the high point, and convert them to percentages of the maximum.
  3. Place each bar with bottom and height from the variables --lo and --h inside a position: relative area.
  4. Animate each bar with scaleY from 0 to 1 with a staggered delay, and set transform-origin to the bottom for increases and the top for costs.
  5. The order starts on the right in RTL automatically because of flex. Under prefers-reduced-motion show the bars ready. The value above each bar stays real text.

Sources

The code

HTML

<!doctype html>
<html lang="ar" dir="rtl">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
  body { margin: 0; min-height: 100vh; display: grid; place-items: center; background: #0f172a; color: #fff; font-family: system-ui, "Segoe UI", Tahoma, sans-serif; }
  .wf { display: flex; gap: 10px; align-items: stretch; height: 260px; width: min(92vw, 560px); }     /* RTL: first bar on the right */
  .item { flex: 1; display: flex; flex-direction: column; min-width: 0; }
  .plot { position: relative; flex: 1; }
  .bar { position: absolute; inset-inline: 0; bottom: calc(var(--lo) * 1%); height: calc(var(--h) * 1%);
         background: var(--c); border-radius: 4px; transform: scaleY(0); transform-origin: var(--o);
         animation: grow .5s ease-out forwards; animation-delay: calc(var(--i) * .25s); }
  .name { text-align: center; font-size: .85rem; padding-top: 6px; }
  .val { position: absolute; inset-inline: 0; text-align: center; font-size: .8rem; bottom: calc(var(--lo) * 1% + var(--h) * 1% + 4px); direction: ltr; }
  @keyframes grow { to { transform: scaleY(1); } }
  @media (prefers-reduced-motion: reduce) { .bar { animation: none; transform: none; } }
</style>
<div class="wf" id="wf" role="img" aria-label="رسم شلال: إيرادات 1000 ثم مصروفات ثم صافي ربح"></div>
<script>
  const rows = [{ n: 'الإيرادات', v: 1000 }, { n: 'التشغيل', v: -300 }, { n: 'الرواتب', v: -250 }, { n: 'الضرائب', v: -90 }, { n: 'الصافي', total: true }];
  const max = 1000; let run = 0;
  const wf = document.getElementById('wf');
  rows.forEach((r, i) => {
    let lo, hi, color, origin;
    if (r.total) { lo = 0; hi = run; color = '#38bdf8'; origin = 'bottom'; }
    else if (r.v >= 0) { lo = run; hi = run + r.v; run = hi; color = '#22c55e'; origin = 'bottom'; }
    else { hi = run; lo = run + r.v; run = lo; color = '#ef4444'; origin = 'top'; }
    const item = document.createElement('div'); item.className = 'item';
    item.innerHTML = '<div class="plot"><div class="bar"></div><div class="val"></div></div><div class="name"></div>';
    const bar = item.querySelector('.bar');
    bar.style.cssText = `--lo:${lo / max * 100};--h:${(hi - lo) / max * 100};--c:${color};--o:${origin};--i:${i}`;
    const val = item.querySelector('.val');
    val.style.cssText = `--lo:${lo / max * 100};--h:${(hi - lo) / max * 100}`;
    val.textContent = r.total ? hi : (r.v > 0 ? '+' : '') + r.v;
    item.querySelector('.name').textContent = r.n;
    wf.appendChild(item);
  });
</script>
</html>

can't find the motion you need? Request it here

Related articles: