グラフ
1年分のカレンダーのマス目が、斜めの波を描くように順に色づくヒートマップ
デモ
デモを読み込めませんでした。
仕様
- 用途
- グラフ
- 動作
- DOM
- 種類
- レシピ
- 動作確認
- three 0.186.0・gsap 3.15.0
- 公開日
しくみと調整
明るい光の波が、曜日を行・週を列にしたカレンダーのマス目の左上から右へ向かって、斜めの筋を描きながら走り抜けます。波が通過したマスは、その日の件数に応じた濃さのスレートカラー(青みのあるグレー)へと落ち着き、格子全体がひと筆書きのように鮮やかに塗られていきます。
波の筋が斜めに傾いて見えるのは、マスごとにアニメーションの開始時刻を少しずつずらしているからです。列が右へ1つ進むごとに0.03秒、行が1つ下へ下がるごとにその1.2列分の遅延を加算しました。下の行ほどわずかに遅れて反応するため、波の先端が縦一直線にならず斜めのラインを描きます。各マスが明るいハイライト色で光るのは0.16秒間だけで、そこから0.5秒かけて本来の件数に応じた色へと落ち着きます。
データがない空のマスの色(--empty)と波のハイライト色(--flash)はCSS変数で定義し、getComputedStyle を使って実際のカラーコードを取得してからGSAPに渡しています。CSS変数の名前を文字列のまま渡してしまうと、GSAPはそれを色として解釈・補間できず、マスの色が変化しなくなってしまうためです。
配色は1つの色相に統一し、明度(明るさ)の差だけで段階をつけています。件数が少ない日ほど淡く、多い日ほど濃く塗られるため、色覚の個人差に関わらず多い・少ないの傾向を直感的に読み取れます。日ごとの件数と週の開始日は格子の下にあるHTMLテーブルから読み取っており、数値を修正したいときも表を書き換えるだけでグラフに反映されます。
波の進む速さと傾きは、スクリプト冒頭の定数で簡単に調整できます。列ごとの遅延を大きくすると波はゆっくりと進み、行ごとの遅延を大きくすると波の傾きがより緩やかに寝ていきます。どちらの値を大きくしても、全体が塗り終わるまでの総時間は長くなります。
コード
<figure class="heatmap">
<figcaption class="heatmap__caption">
<span class="heatmap__eyebrow">Activity</span>
<span class="heatmap__title">週ごとに<wbr>完了した<wbr>タスクの数</span>
</figcaption>
<div class="heatmap__body">
<!-- 見た目の格子。色は下の表の値から JS が付ける -->
<div class="heatmap__grid" aria-hidden="true"></div>
<!-- 値はこの表に書く。週の始まり(月/日)と、月〜日の 7 つの件数 -->
<div class="heatmap__scroll">
<table class="heatmap__table">
<caption>週ごとの完了タスク数(月曜始まり)</caption>
<thead>
<tr><th scope="col">週</th><th scope="col">月</th><th scope="col">火</th><th scope="col">水</th><th scope="col">木</th><th scope="col">金</th><th scope="col">土</th><th scope="col">日</th></tr>
</thead>
<tbody>
<tr><th scope="row">7/6</th><td>4</td><td>6</td><td>3</td><td>7</td><td>5</td><td>1</td><td>0</td></tr>
<tr><th scope="row">7/13</th><td>2</td><td>5</td><td>8</td><td>4</td><td>6</td><td>0</td><td>1</td></tr>
<tr><th scope="row">7/20</th><td>6</td><td>3</td><td>5</td><td>9</td><td>7</td><td>2</td><td>0</td></tr>
<tr><th scope="row">7/27</th><td>5</td><td>7</td><td>4</td><td>6</td><td>8</td><td>1</td><td>0</td></tr>
<tr><th scope="row">8/3</th><td>3</td><td>6</td><td>7</td><td>5</td><td>9</td><td>0</td><td>2</td></tr>
<tr><th scope="row">8/10</th><td>7</td><td>8</td><td>6</td><td>10</td><td>9</td><td>2</td><td>1</td></tr>
<tr><th scope="row">8/17</th><td>6</td><td>9</td><td>8</td><td>11</td><td>10</td><td>3</td><td>1</td></tr>
<tr><th scope="row">8/24</th><td>8</td><td>7</td><td>9</td><td>6</td><td>8</td><td>2</td><td>0</td></tr>
<tr><th scope="row">8/31</th><td>4</td><td>6</td><td>5</td><td>7</td><td>6</td><td>1</td><td>0</td></tr>
<tr><th scope="row">9/7</th><td>5</td><td>4</td><td>6</td><td>8</td><td>7</td><td>2</td><td>1</td></tr>
<tr><th scope="row">9/14</th><td>3</td><td>5</td><td>4</td><td>6</td><td>5</td><td>0</td><td>1</td></tr>
<tr><th scope="row">9/21</th><td>2</td><td>4</td><td>3</td><td>5</td><td>4</td><td>1</td><td>0</td></tr>
</tbody>
</table>
</div>
</div>
</figure>.heatmap {
--ink: #262626;
--mute: #6b6b6b;
--surface: #ffffff;
--line: rgba(38, 38, 38, 0.12);
--cell: 18px; /* マスの大きさ */
--gap: 3px; /* マスの間 */
--empty: #ecebe7; /* 0 件のマスの色 */
--flash: #b8c7d3; /* 波が通っているあいだの色 */
--level-1: #c8d3dd; /* 1〜3 件 */
--level-2: #91a9ba; /* 4〜6 件 */
--level-3: #62839D; /* 7〜9 件 */
--level-4: #3e4f59; /* 10 件以上 */
margin: 0;
padding: 32px;
background: var(--surface);
color: var(--ink);
font-variant-numeric: tabular-nums;
}
.heatmap__caption { display: grid; gap: 6px; margin-bottom: 24px; }
.heatmap__eyebrow { font-size: 13px; font-weight: 700; letter-spacing: 0.14em; text-transform: uppercase; color: var(--mute); }
.heatmap__title { font-size: 24px; font-weight: 700; letter-spacing: -0.01em; word-break: keep-all; }
.heatmap__body { display: grid; gap: 20px; }
/* 週(列)ごとに縦へ積む。曜日(行)の順は表と同じにする */
.heatmap__grid {
display: grid;
grid-auto-flow: column;
grid-template-rows: repeat(7, var(--cell));
grid-auto-columns: var(--cell);
gap: var(--gap);
}
.heatmap__cell { border-radius: 3px; background: var(--empty); }
.heatmap__scroll { overflow-x: auto; }
.heatmap__table {
border-collapse: separate;
border-spacing: 0;
font-size: 12px;
white-space: nowrap;
}
.heatmap__table caption {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
}
.heatmap__table th,
.heatmap__table td {
padding: 4px 8px;
text-align: right;
border-top: 1px solid var(--line);
color: var(--mute);
}
.heatmap__table thead th {
border-top: 0;
padding-bottom: 6px;
font-weight: 600;
letter-spacing: 0.04em;
}
.heatmap__table th[scope="row"] {
text-align: left;
color: var(--ink);
font-weight: 600;
}import { gsap } from "gsap";
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const EACH = 0.03; // 1 列(週)ぶんの遅れ
const SLANT = 1.2; // 1 段下がるごとに、何列ぶん遅れるか(波の傾き)
const FLASH_T = 0.16; // 波が通るときの色になっている時間
const SETTLE = 0.5; // 波の色から件数の色に落ち着くまで
const levelOf = (n) => (n === 0 ? 0 : n <= 3 ? 1 : n <= 6 ? 2 : n <= 9 ? 3 : 4);
document.querySelectorAll(".heatmap").forEach((chart) => {
const style = getComputedStyle(chart);
// 色は CSS の変数で決める。GSAP は var(--x) のままでは補間できないので、値にして読む
const empty = style.getPropertyValue("--empty").trim();
const flash = style.getPropertyValue("--flash").trim();
const levels = [1, 2, 3, 4].map((k) => style.getPropertyValue(`--level-${k}`).trim());
levels.unshift(empty);
const grid = chart.querySelector(".heatmap__grid");
const days = [...chart.querySelectorAll("thead th")].slice(1).map((th) => th.textContent);
const weeks = [...chart.querySelectorAll("tbody tr")].map((row) => ({
date: row.querySelector("th").textContent,
counts: [...row.querySelectorAll("td")].map((td) => Number(td.textContent)),
}));
const cells = [];
weeks.forEach(({ date, counts }, col) => {
counts.forEach((n, row) => {
const el = document.createElement("div");
el.className = "heatmap__cell";
el.title = `${date}(${days[row]}): ${n}件`;
grid.append(el);
cells.push({ el, col, row, n });
});
});
const targets = cells.map((c) => c.el);
const delay = (i) => (cells[i].col + cells[i].row * SLANT) * EACH;
const colorOf = (i) => levels[levelOf(cells[i].n)];
const tl = gsap
.timeline({ paused: true })
.fromTo(targets, { backgroundColor: empty }, { backgroundColor: flash, duration: FLASH_T, ease: "none", stagger: delay })
.fromTo(targets, { backgroundColor: flash }, { backgroundColor: colorOf, duration: SETTLE, ease: "power2.out", stagger: delay, immediateRender: false }, FLASH_T);
// 動きを減らす設定では、色づききった状態で見せる
if (reduceMotion) {
tl.progress(1);
return;
}
// 画面に入ったら 1 回だけ色づける
const io = new IntersectionObserver(([entry]) => {
if (!entry.isIntersecting) return;
io.disconnect();
tl.play();
}, { rootMargin: "0px 0px -20%" });
io.observe(chart);
});










