一年分の活動を一画面で振り返る
Apple Watchのワークアウトをダッシュボードにまとめた例。期間全体の傾向と一回ごとの記録をどう見渡すか。
月別の分布と期間合計を並べる
元の実例の着眼点を試すための独自のWebサンプルです。
年を切り替え、いずれかの月を押して記録を確認してください。
実装の手順
- 記録を月単位で集計し、同じ単位の12個の値にする。サンプルは月ごとの運動時間。
- 年を切り替えたら合計と棒を同じデータから更新する。年ごとの比較には共通の目盛りを使う。
- 各月を選ぶと数値を表示する。棒の長さだけに頼らず、キーボードでも月を確認できるようにする。
動作確認
合計と各月の数値が一致し、どちらの年でも同じ値は同じ高さになる。
サンプルコードを見る
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>月別の分布と期間合計を並べる</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 16px;
background: #23232a;
color: #f4f0ea;
font: 16px/1.6 system-ui, sans-serif;
}
label, output, strong, audio, progress { display: block; margin: 6px 0; }
input, select, button { max-width: 100%; font: inherit; }
input, select { padding: 4px 7px; }
button {
margin: 4px 0;
padding: 5px 12px;
cursor: pointer;
border: 1px solid #b5a399;
border-radius: 6px;
}
:focus-visible { outline: 3px solid #ffad73; outline-offset: 2px; }
audio { width: 100%; }
[hidden] { display: none !important; }
#months { display: flex; gap: 3px; align-items: end; }
#months button {
flex: 1;
min-width: 0;
padding: 0;
font-size: 12px;
color: #18392e;
background: #a5dbc5;
}
</style>
</head>
<body>
<label>対象年 <select id="year"><option>2025</option><option>2026</option></select></label>
<strong id="total"></strong>
<div id="months" aria-label="月別の運動時間"></div>
<output id="value"></output>
<script>
const records = {
2025: [3,5,2,7,4,8,6,5,3,7,8,9],
2026: [5,6,3,5,8,9,4,6,7,8,5,6]
};
const year = document.querySelector('#year');
function render() {
const data = records[year.value];
const sum = data.reduce((a, b) => a + b, 0);
document.querySelector('#total').textContent = '年間 ' + sum + ' 時間';
const months = document.querySelector('#months');
months.replaceChildren();
document.querySelector('#value').textContent = '月を選んでください';
data.forEach((hours, i) => {
const button = document.createElement('button');
const label = (i + 1) + '月:' + hours + '時間';
button.style.height = (24 + hours * 5) + 'px';
button.textContent = i + 1;
button.setAttribute('aria-label', label);
button.onclick = () => { document.querySelector('#value').textContent = label; };
months.append(button);
});
}
year.onchange = render;
render();
</script>
</body>
</html>index.html に保存し、ブラウザで開くと試せます。