小さな折れ線
数字の傾向だけを、軸のない小さな折れ線で添える部品です。傾向は aria-label に言葉で書きます。
見本で種類と状態を確かめる
高さ 32、線 1.5、色 accent で、最後の点に 4 の丸を置きます。比べる線(去年など)は text-3 です。軸と目盛りは描きません。
比べる線あり
単勝の回収率(直近7週)
128%
▲ 先週より 9ポイント線だけ
1日あたりの支出(直近7日)
5,900円
▲ 7日で 84% 増コードを写す
HTML
<svg class="mz-spark" viewBox="0 0 100 32" preserveAspectRatio="none" role="img" aria-label="7週で82%から128%に上がった。去年の同じ時期(85〜97%)より高い">
<path class="mz-spark__compare" d="M0 27.83 L16.67 26.43 L33.33 29.91 L50 25.04 L66.67 22.96 L83.33 24.35 L100 21.57"/>
<path class="mz-spark__line" d="M0 32 L16.67 22.26 L33.33 25.74 L50 12.52 L66.67 16.7 L83.33 6.26 L100 0"/>
<path class="mz-spark__dot" d="M100 0 h0"/>
</svg>React の呼び出し方
<Spark
values={[82, 96, 91, 110, 104, 119, 128]}
compare={[87, 89, 85, 91, 94, 92, 97]}
label="7週で82%から128%に上がった。去年の同じ時期(85〜97%)より高い"
/>実装を見る(react/Spark.tsx)
React の実装(react/Spark.tsx)
/**
* Spark — 小さな折れ線(docs/spec/components.md「spark」)
* 見た目は css/components/spark.css だけが持つ。ここでは値の並びを SVG の線に変えるだけ。
* 横は入れ物の幅に伸ばす(viewBox 0 0 100 32・preserveAspectRatio="none")。最後の点は長さ 0 の線を丸い端で描く。
* label(傾向を言葉にした文。例:「7 日で 12% 増」)は必ず渡す。
*/
import { forwardRef, type SVGAttributes } from 'react';
const W = 100;
const H = 32;
function toPath(values: number[], min: number, max: number): { d: string; last: [number, number] } {
const span = max - min || 1;
const step = values.length > 1 ? W / (values.length - 1) : 0;
const pts = values.map((v, i) => [i * step, H - ((v - min) / span) * H] as [number, number]);
const d = pts.map(([x, y], i) => `${i === 0 ? 'M' : 'L'}${x.toFixed(2)} ${y.toFixed(2)}`).join(' ');
return { d, last: pts[pts.length - 1] ?? [0, H] };
}
export interface SparkProps extends Omit<SVGAttributes<SVGSVGElement>, 'values'> {
values: number[];
/** 比べる線(text-3) */
compare?: number[];
/** 傾向を言葉にした文(aria-label) */
label: string;
}
export const Spark = forwardRef<SVGSVGElement, SparkProps>(function Spark(
{ values, compare, label, className, ...rest },
ref,
) {
// 有限の値が 2 つ未満の線は描かない(空・NaN・Infinity で線が崩れるため)
const finite = (list?: number[]) => (list ?? []).filter((n) => Number.isFinite(n));
const mainValues = finite(values);
const compareValues = finite(compare);
if (mainValues.length < 2) return null;
const hasCompare = compareValues.length >= 2;
const all = hasCompare ? mainValues.concat(compareValues) : mainValues;
const min = Math.min(...all);
const max = Math.max(...all);
const main = toPath(mainValues, min, max);
const cmp = hasCompare ? toPath(compareValues, min, max) : null;
const [lx, ly] = main.last;
return (
<svg
ref={ref}
className={className ? `mz-spark ${className}` : 'mz-spark'}
viewBox={`0 0 ${W} ${H}`}
preserveAspectRatio="none"
role="img"
aria-label={label}
{...rest}
>
{cmp ? <path className="mz-spark__compare" d={cmp.d} /> : null}
<path className="mz-spark__line" d={main.d} />
<path className="mz-spark__dot" d={`M${lx.toFixed(2)} ${ly.toFixed(2)} h0`} />
</svg>
);
});点の座標は、値を 0〜100 の横と 0〜32 の縦に写して計算します(React の Spark は値の並びから作ります)。
いつ使うか
- 数字の下に置き、「増えているか」「いつもと比べてどうか」だけを伝えます。
- 細かい値を読ませたいときは、軸のあるグラフか表にします。
- 強調の地(highlight)の数字に添えるときは、比べる線を highlight-ink の点線にします。
読み上げとキーボードにどう応えるか
- SVG に
role="img"を付け、aria-labelに傾向を言葉で書きます(「7日で3,200円から5,900円に84%増えた」)。 - 線の形だけで伝えず、増減の文を数字の下にも書きます。
よい例と悪い例
更新日