ナビゲーション

v2.1.0
データ表示

スパークライン

文中やテーブルの行、統計指標に収まる、細い線1本の推移グラフ。

このページの内容

スパークラインは、推移を細い線1本で示す小さなグラフです。統計指標の数値の下や、テーブルの列、文中など、関連するテキストのそばに置けます。線を描くインラインSVGは与えられた領域に合わせて伸縮します。線は周囲のテキストと同じ色で、太さは1.5pxのままです。最新値を示す点は、グラフが伸縮しても円形のままです。スクリプトは不要です。

スパークライン

<div class="card max-w(320) mx-auto">  <div class="stat">    <p class="stat-label">Requests per day</p>    <div class="stat-row">      <p class="stat-value">61,240</p>      <p class="stat-delta stat-delta-up">        <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="rotate(-90 8 8)" /><path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(-90 8 8)" />        </svg>12.4%      </p>    </div>    <span class="sparkline" style="--sparkline-width: 100%; --sparkline-height: 2.5rem" aria-hidden="true">      <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">        <path class="sparkline-area" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0 V100 H0 Z" /><line class="sparkline-baseline" x1="0" y1="55.5" x2="100" y2="55.5" /><path class="sparkline-line" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0" />      </svg><span class="sparkline-dot" style="--x: 100; --y: 0"></span>    </span>    <p class="stat-description">The last 14 days; the dotted line is their average</p>  </div></div>
import ArrowUpIcon from '@master/icons-react/native/16/soft/arrow-up'<div className="card max-w(320) mx-auto">  <div className="stat">    <p className="stat-label">Requests per day</p>    <div className="stat-row">      <p className="stat-value">61,240</p>      <p className="stat-delta stat-delta-up"><ArrowUpIcon className="icon" />12.4%</p>    </div>    <span className="sparkline" style={{ '--sparkline-width': '100%', '--sparkline-height': '2.5rem' } as React.CSSProperties} aria-hidden="true">      <svg className="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">        <path className="sparkline-area" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0 V100 H0 Z" /><line className="sparkline-baseline" x1="0" y1="55.5" x2="100" y2="55.5" /><path className="sparkline-line" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0" />      </svg><span className="sparkline-dot" style={{ '--x': '100', '--y': '0' } as React.CSSProperties}></span>    </span>    <p className="stat-description">The last 14 days; the dotted line is their average</p>  </div></div>
<script>  import ArrowUpIcon from '@master/icons-svelte/native/16/soft/arrow-up'</script><div class="card max-w(320) mx-auto">  <div class="stat">    <p class="stat-label">Requests per day</p>    <div class="stat-row">      <p class="stat-value">61,240</p>      <p class="stat-delta stat-delta-up"><ArrowUpIcon class="icon" />12.4%</p>    </div>    <span class="sparkline" style="--sparkline-width: 100%; --sparkline-height: 2.5rem" aria-hidden="true">      <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">        <path class="sparkline-area" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0 V100 H0 Z" /><line class="sparkline-baseline" x1="0" y1="55.5" x2="100" y2="55.5" /><path class="sparkline-line" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0" />      </svg><span class="sparkline-dot" style="--x: 100; --y: 0"></span>    </span>    <p class="stat-description">The last 14 days; the dotted line is their average</p>  </div></div>
<script setup>import ArrowUpIcon from '@master/icons-vue/native/16/soft/arrow-up'</script><template>  <div class="card max-w(320) mx-auto">    <div class="stat">      <p class="stat-label">Requests per day</p>      <div class="stat-row">        <p class="stat-value">61,240</p>        <p class="stat-delta stat-delta-up"><ArrowUpIcon class="icon" />12.4%</p>      </div>      <span class="sparkline" style="--sparkline-width: 100%; --sparkline-height: 2.5rem" aria-hidden="true">        <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">          <path class="sparkline-area" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0 V100 H0 Z" /><line class="sparkline-baseline" x1="0" y1="55.5" x2="100" y2="55.5" /><path class="sparkline-line" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0" />        </svg><span class="sparkline-dot" style="--x: 100; --y: 0"></span>      </span>      <p class="stat-description">The last 14 days; the dotted line is their average</p>    </div>  </div></template>

#線の描画

グラフはviewBox="0 0 100 100"とpreserveAspectRatio="none"を指定した<svg class="sparkline-plot">で描画します。幅と高さがともに100単位の座標領域を、スパークラインの表示サイズに合わせて伸縮させます。この領域で点の座標を一度指定すれば、文中でもカード内でも表示サイズに収まります。

  • 横軸では、最初の点を0、最後の点を100に置き、点を等間隔に並べます。値が14個なら、間隔は100 / 13単位です。
  • 縦軸はSVGの座標系に従い、上端が0、下端が100です。最小値を100、最大値を0に割り当てます。複数のスパークラインを比較する場合は、独自の尺度を使うこともできます。

sparkline-lineは、各点を通る<path>(または<polyline>)です。線幅は座標領域の伸縮に連動せず(vector-effect: non-scaling-stroke)、どのサイズでも1.5pxです。線の継ぎ目と端は丸く描画します。

スパークラインは推移の形を示します。下のテーブルでは、各プロジェクトの最小値から最大値までを個別の尺度で描いています。線からは各プロジェクトの推移が分かり、値の大小は隣の数値で比較できます。

#最新値

sparkline-dotはHTMLで描く小さな点です。SVGの円はグラフの伸縮で楕円になるため、HTMLで円形を保ちます。同じ100 × 100の座標系で、最後の点の座標を--xと--yに指定し、点の中心を配置します。背後の面と同じ色のリングで点の周囲の線を抜き、線が点で終わるように見せます。スパークラインの領域内には点の半分が収まる余白を確保し、後ろの単語に点が近づきすぎないようにします。

#塗りつぶしと基準線

sparkline-areaは、同じ点を通り、下端に沿って閉じた<path>(V100 H0 Z)です。線と同じ色で淡く塗り、下に向かって透明にします。線の前に配置すると、その上に線が描画されます。

sparkline-baselineは、平均値、目標値、前期の値などの高さで、0から100まで引く細い点線の<line>です。推移を示す線より控えめに表示します。上の統計指標の例のように、基準線が何を示すかをスパークラインの隣に記載します。

#文中で使う

スパークラインの幅は4em、高さは大文字とほぼ同じ.75emです。中心を周囲の大文字の高さの中央にそろえるため、単語のように行内に収まります。ページ上でもカード内でも、インク色の背景面でも、周囲のテキスト色を使います。

文中で使う

<p class="m(0) max-w(440) mx-auto fg-text-body">  Requests rose all fortnight <span class="sparkline" aria-hidden="true">    <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">      <path class="sparkline-line" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0" />    </svg><span class="sparkline-dot" style="--x: 100; --y: 0"></span>  </span> to 61,000 a day, while latency fell <span class="sparkline" aria-hidden="true">    <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">      <path class="sparkline-line" d="M0 0 L7.69 11.1 L15.38 3.7 L23.08 20.4 L30.77 25.9 L38.46 22.2 L46.15 42.6 L53.85 55.6 L61.54 50 L69.23 66.7 L76.92 75.9 L84.62 72.2 L92.31 85.2 L100 100" />    </svg>  </span> to 128 ms.</p>
<p className="m(0) max-w(440) mx-auto fg-text-body">  Requests rose all fortnight <span className="sparkline" aria-hidden="true">    <svg className="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">      <path className="sparkline-line" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0" />    </svg><span className="sparkline-dot" style={{ '--x': '100', '--y': '0' } as React.CSSProperties}></span>  </span> to 61,000 a day, while latency fell <span className="sparkline" aria-hidden="true">    <svg className="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">      <path className="sparkline-line" d="M0 0 L7.69 11.1 L15.38 3.7 L23.08 20.4 L30.77 25.9 L38.46 22.2 L46.15 42.6 L53.85 55.6 L61.54 50 L69.23 66.7 L76.92 75.9 L84.62 72.2 L92.31 85.2 L100 100" />    </svg>  </span> to 128 ms.</p>
<p class="m(0) max-w(440) mx-auto fg-text-body">  Requests rose all fortnight <span class="sparkline" aria-hidden="true">    <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">      <path class="sparkline-line" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0" />    </svg><span class="sparkline-dot" style="--x: 100; --y: 0"></span>  </span> to 61,000 a day, while latency fell <span class="sparkline" aria-hidden="true">    <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">      <path class="sparkline-line" d="M0 0 L7.69 11.1 L15.38 3.7 L23.08 20.4 L30.77 25.9 L38.46 22.2 L46.15 42.6 L53.85 55.6 L61.54 50 L69.23 66.7 L76.92 75.9 L84.62 72.2 L92.31 85.2 L100 100" />    </svg>  </span> to 128 ms.</p>
<template>  <p class="m(0) max-w(440) mx-auto fg-text-body">    Requests rose all fortnight <span class="sparkline" aria-hidden="true">      <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">        <path class="sparkline-line" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0" />      </svg><span class="sparkline-dot" style="--x: 100; --y: 0"></span>    </span> to 61,000 a day, while latency fell <span class="sparkline" aria-hidden="true">      <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">        <path class="sparkline-line" d="M0 0 L7.69 11.1 L15.38 3.7 L23.08 20.4 L30.77 25.9 L38.46 22.2 L46.15 42.6 L53.85 55.6 L61.54 50 L69.23 66.7 L76.92 75.9 L84.62 72.2 L92.31 85.2 L100 100" />      </svg>    </span> to 128 ms.  </p></template>

#テーブルで使う

--sparkline-widthと--sparkline-heightを個々のスパークラインに指定するか、テーブルなどのコンテナーにまとめて指定して、内側のすべてのスパークラインに同じサイズを適用します。上の統計指標の例のように、カードに合わせてサイズを調整する場合は--sparkline-width: 100%を指定します。

テーブルで使う

<div class="sheet overflow-hidden max-w(480) mx-auto">  <table class="table table-compact" style="--sparkline-width: 5rem; --sparkline-height: 1rem">    <thead>      <tr class="table-row">        <th class="table-heading" scope="col">Project</th>        <th class="table-heading" scope="col">Last 14 days</th>        <th class="table-heading table-numeric" scope="col">Today</th>      </tr>    </thead>    <tbody>      <tr class="table-row">        <th class="table-cell table-primary" scope="row">Atlas</th>        <td class="table-cell">          <span class="sparkline" role="img" aria-label="Rising steadily">            <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">              <path class="sparkline-line" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0" />            </svg><span class="sparkline-dot" style="--x: 100; --y: 0"></span>          </span>        </td>        <td class="table-cell table-numeric">61,240</td>      </tr>      <tr class="table-row">        <th class="table-cell table-primary" scope="row">Borealis</th>        <td class="table-cell">          <span class="sparkline" role="img" aria-label="Falling">            <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">              <path class="sparkline-line" d="M0 5.9 L7.69 17.6 L15.38 0 L23.08 23.5 L30.77 35.3 L38.46 29.4 L46.15 52.9 L53.85 41.2 L61.54 70.6 L69.23 58.8 L76.92 82.4 L84.62 76.5 L92.31 94.1 L100 100" />            </svg><span class="sparkline-dot" style="--x: 100; --y: 100"></span>          </span>        </td>        <td class="table-cell table-numeric">36,018</td>      </tr>      <tr class="table-row">        <th class="table-cell table-primary" scope="row">Cirrus</th>        <td class="table-cell">          <span class="sparkline" role="img" aria-label="Rising slowly">            <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">              <path class="sparkline-line" d="M0 100 L7.69 79.2 L15.38 91.7 L23.08 58.3 L30.77 66.7 L38.46 45.8 L46.15 54.2 L53.85 33.3 L61.54 41.7 L69.23 20.8 L76.92 29.2 L84.62 12.5 L92.31 16.7 L100 0" />            </svg><span class="sparkline-dot" style="--x: 100; --y: 0"></span>          </span>        </td>        <td class="table-cell table-numeric">16,420</td>      </tr>    </tbody>  </table></div>
<div className="sheet overflow-hidden max-w(480) mx-auto">  <table className="table table-compact" style={{ '--sparkline-width': '5rem', '--sparkline-height': '1rem' } as React.CSSProperties}>    <thead>      <tr className="table-row">        <th className="table-heading" scope="col">Project</th>        <th className="table-heading" scope="col">Last 14 days</th>        <th className="table-heading table-numeric" scope="col">Today</th>      </tr>    </thead>    <tbody>      <tr className="table-row">        <th className="table-cell table-primary" scope="row">Atlas</th>        <td className="table-cell">          <span className="sparkline" role="img" aria-label="Rising steadily">            <svg className="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">              <path className="sparkline-line" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0" />            </svg><span className="sparkline-dot" style={{ '--x': '100', '--y': '0' } as React.CSSProperties}></span>          </span>        </td>        <td className="table-cell table-numeric">61,240</td>      </tr>      <tr className="table-row">        <th className="table-cell table-primary" scope="row">Borealis</th>        <td className="table-cell">          <span className="sparkline" role="img" aria-label="Falling">            <svg className="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">              <path className="sparkline-line" d="M0 5.9 L7.69 17.6 L15.38 0 L23.08 23.5 L30.77 35.3 L38.46 29.4 L46.15 52.9 L53.85 41.2 L61.54 70.6 L69.23 58.8 L76.92 82.4 L84.62 76.5 L92.31 94.1 L100 100" />            </svg><span className="sparkline-dot" style={{ '--x': '100', '--y': '100' } as React.CSSProperties}></span>          </span>        </td>        <td className="table-cell table-numeric">36,018</td>      </tr>      <tr className="table-row">        <th className="table-cell table-primary" scope="row">Cirrus</th>        <td className="table-cell">          <span className="sparkline" role="img" aria-label="Rising slowly">            <svg className="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">              <path className="sparkline-line" d="M0 100 L7.69 79.2 L15.38 91.7 L23.08 58.3 L30.77 66.7 L38.46 45.8 L46.15 54.2 L53.85 33.3 L61.54 41.7 L69.23 20.8 L76.92 29.2 L84.62 12.5 L92.31 16.7 L100 0" />            </svg><span className="sparkline-dot" style={{ '--x': '100', '--y': '0' } as React.CSSProperties}></span>          </span>        </td>        <td className="table-cell table-numeric">16,420</td>      </tr>    </tbody>  </table></div>
<div class="sheet overflow-hidden max-w(480) mx-auto">  <table class="table table-compact" style="--sparkline-width: 5rem; --sparkline-height: 1rem">    <thead>      <tr class="table-row">        <th class="table-heading" scope="col">Project</th>        <th class="table-heading" scope="col">Last 14 days</th>        <th class="table-heading table-numeric" scope="col">Today</th>      </tr>    </thead>    <tbody>      <tr class="table-row">        <th class="table-cell table-primary" scope="row">Atlas</th>        <td class="table-cell">          <span class="sparkline" role="img" aria-label="Rising steadily">            <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">              <path class="sparkline-line" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0" />            </svg><span class="sparkline-dot" style="--x: 100; --y: 0"></span>          </span>        </td>        <td class="table-cell table-numeric">61,240</td>      </tr>      <tr class="table-row">        <th class="table-cell table-primary" scope="row">Borealis</th>        <td class="table-cell">          <span class="sparkline" role="img" aria-label="Falling">            <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">              <path class="sparkline-line" d="M0 5.9 L7.69 17.6 L15.38 0 L23.08 23.5 L30.77 35.3 L38.46 29.4 L46.15 52.9 L53.85 41.2 L61.54 70.6 L69.23 58.8 L76.92 82.4 L84.62 76.5 L92.31 94.1 L100 100" />            </svg><span class="sparkline-dot" style="--x: 100; --y: 100"></span>          </span>        </td>        <td class="table-cell table-numeric">36,018</td>      </tr>      <tr class="table-row">        <th class="table-cell table-primary" scope="row">Cirrus</th>        <td class="table-cell">          <span class="sparkline" role="img" aria-label="Rising slowly">            <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">              <path class="sparkline-line" d="M0 100 L7.69 79.2 L15.38 91.7 L23.08 58.3 L30.77 66.7 L38.46 45.8 L46.15 54.2 L53.85 33.3 L61.54 41.7 L69.23 20.8 L76.92 29.2 L84.62 12.5 L92.31 16.7 L100 0" />            </svg><span class="sparkline-dot" style="--x: 100; --y: 0"></span>          </span>        </td>        <td class="table-cell table-numeric">16,420</td>      </tr>    </tbody>  </table></div>
<template>  <div class="sheet overflow-hidden max-w(480) mx-auto">    <table class="table table-compact" style="--sparkline-width: 5rem; --sparkline-height: 1rem">      <thead>        <tr class="table-row">          <th class="table-heading" scope="col">Project</th>          <th class="table-heading" scope="col">Last 14 days</th>          <th class="table-heading table-numeric" scope="col">Today</th>        </tr>      </thead>      <tbody>        <tr class="table-row">          <th class="table-cell table-primary" scope="row">Atlas</th>          <td class="table-cell">            <span class="sparkline" role="img" aria-label="Rising steadily">              <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">                <path class="sparkline-line" d="M0 100 L7.69 90 L15.38 93.3 L23.08 76.7 L30.77 83.3 L38.46 66.7 L46.15 56.7 L53.85 63.3 L61.54 46.7 L69.23 30 L76.92 40 L84.62 20 L92.31 10 L100 0" />              </svg><span class="sparkline-dot" style="--x: 100; --y: 0"></span>            </span>          </td>          <td class="table-cell table-numeric">61,240</td>        </tr>        <tr class="table-row">          <th class="table-cell table-primary" scope="row">Borealis</th>          <td class="table-cell">            <span class="sparkline" role="img" aria-label="Falling">              <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">                <path class="sparkline-line" d="M0 5.9 L7.69 17.6 L15.38 0 L23.08 23.5 L30.77 35.3 L38.46 29.4 L46.15 52.9 L53.85 41.2 L61.54 70.6 L69.23 58.8 L76.92 82.4 L84.62 76.5 L92.31 94.1 L100 100" />              </svg><span class="sparkline-dot" style="--x: 100; --y: 100"></span>            </span>          </td>          <td class="table-cell table-numeric">36,018</td>        </tr>        <tr class="table-row">          <th class="table-cell table-primary" scope="row">Cirrus</th>          <td class="table-cell">            <span class="sparkline" role="img" aria-label="Rising slowly">              <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">                <path class="sparkline-line" d="M0 100 L7.69 79.2 L15.38 91.7 L23.08 58.3 L30.77 66.7 L38.46 45.8 L46.15 54.2 L53.85 33.3 L61.54 41.7 L69.23 20.8 L76.92 29.2 L84.62 12.5 L92.31 16.7 L100 0" />              </svg><span class="sparkline-dot" style="--x: 100; --y: 0"></span>            </span>          </td>          <td class="table-cell table-numeric">16,420</td>        </tr>      </tbody>    </table>  </div></template>

#ステータス色

sparkline-success、sparkline-warning、sparkline-dangerは、線と点にステータス色を適用します。値の上昇が好ましいとは限りません。たとえば、エラーの増加を示す右上がりの線には危険を示す色を使います。色だけに頼らず意味を伝えられるよう、同じ意味が隣のテキストにも示されている場合に限って使います。

ステータス色

<p class="m(0) max-w(440) mx-auto fg-text-body">  After the fix, errors fell by half <span class="sparkline sparkline-success" aria-hidden="true">    <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">      <path class="sparkline-line" d="M0 9.1 L7.69 0 L15.38 15.2 L23.08 6.1 L30.77 21.2 L38.46 39.4 L46.15 60.6 L53.85 72.7 L61.54 78.8 L69.23 87.9 L76.92 81.8 L84.62 90.9 L92.31 97 L100 100" />    </svg><span class="sparkline-dot" style="--x: 100; --y: 100"></span>  </span> to 18 an hour. Checkout got slower <span class="sparkline sparkline-danger" aria-hidden="true">    <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">      <path class="sparkline-line" d="M0 91.7 L7.69 100 L15.38 83.3 L23.08 91.7 L30.77 75 L38.46 83.3 L46.15 66.7 L53.85 58.3 L61.54 66.7 L69.23 41.7 L76.92 33.3 L84.62 25 L92.31 16.7 L100 0" />    </svg><span class="sparkline-dot" style="--x: 100; --y: 0"></span>  </span> and now takes 3.2 seconds.</p>
<p className="m(0) max-w(440) mx-auto fg-text-body">  After the fix, errors fell by half <span className="sparkline sparkline-success" aria-hidden="true">    <svg className="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">      <path className="sparkline-line" d="M0 9.1 L7.69 0 L15.38 15.2 L23.08 6.1 L30.77 21.2 L38.46 39.4 L46.15 60.6 L53.85 72.7 L61.54 78.8 L69.23 87.9 L76.92 81.8 L84.62 90.9 L92.31 97 L100 100" />    </svg><span className="sparkline-dot" style={{ '--x': '100', '--y': '100' } as React.CSSProperties}></span>  </span> to 18 an hour. Checkout got slower <span className="sparkline sparkline-danger" aria-hidden="true">    <svg className="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">      <path className="sparkline-line" d="M0 91.7 L7.69 100 L15.38 83.3 L23.08 91.7 L30.77 75 L38.46 83.3 L46.15 66.7 L53.85 58.3 L61.54 66.7 L69.23 41.7 L76.92 33.3 L84.62 25 L92.31 16.7 L100 0" />    </svg><span className="sparkline-dot" style={{ '--x': '100', '--y': '0' } as React.CSSProperties}></span>  </span> and now takes 3.2 seconds.</p>
<p class="m(0) max-w(440) mx-auto fg-text-body">  After the fix, errors fell by half <span class="sparkline sparkline-success" aria-hidden="true">    <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">      <path class="sparkline-line" d="M0 9.1 L7.69 0 L15.38 15.2 L23.08 6.1 L30.77 21.2 L38.46 39.4 L46.15 60.6 L53.85 72.7 L61.54 78.8 L69.23 87.9 L76.92 81.8 L84.62 90.9 L92.31 97 L100 100" />    </svg><span class="sparkline-dot" style="--x: 100; --y: 100"></span>  </span> to 18 an hour. Checkout got slower <span class="sparkline sparkline-danger" aria-hidden="true">    <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">      <path class="sparkline-line" d="M0 91.7 L7.69 100 L15.38 83.3 L23.08 91.7 L30.77 75 L38.46 83.3 L46.15 66.7 L53.85 58.3 L61.54 66.7 L69.23 41.7 L76.92 33.3 L84.62 25 L92.31 16.7 L100 0" />    </svg><span class="sparkline-dot" style="--x: 100; --y: 0"></span>  </span> and now takes 3.2 seconds.</p>
<template>  <p class="m(0) max-w(440) mx-auto fg-text-body">    After the fix, errors fell by half <span class="sparkline sparkline-success" aria-hidden="true">      <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">        <path class="sparkline-line" d="M0 9.1 L7.69 0 L15.38 15.2 L23.08 6.1 L30.77 21.2 L38.46 39.4 L46.15 60.6 L53.85 72.7 L61.54 78.8 L69.23 87.9 L76.92 81.8 L84.62 90.9 L92.31 97 L100 100" />      </svg><span class="sparkline-dot" style="--x: 100; --y: 100"></span>    </span> to 18 an hour. Checkout got slower <span class="sparkline sparkline-danger" aria-hidden="true">      <svg class="sparkline-plot" viewBox="0 0 100 100" preserveAspectRatio="none">        <path class="sparkline-line" d="M0 91.7 L7.69 100 L15.38 83.3 L23.08 91.7 L30.77 75 L38.46 83.3 L46.15 66.7 L53.85 58.3 L61.54 66.7 L69.23 41.7 L76.92 33.3 L84.62 25 L92.31 16.7 L100 0" />      </svg><span class="sparkline-dot" style="--x: 100; --y: 0"></span>    </span> and now takes 3.2 seconds.  </p></template>

#アクセシビリティ

  • 隣のテキストに推移の説明がない場合は、スパークラインにrole="img"を指定し、「1日あたり31,000から61,000に増加」など、推移を説明するaria-labelを付けます。グラフ全体が1つの画像として扱われます。
  • 文中や統計指標のように、隣のテキストや数値ですでに推移が伝わる場合は、読み上げの重複を防ぐため、aria-hidden="true"で支援技術から隠します。
  • 強制カラーモードでは、線と点にテキスト色が適用され、塗りつぶしは表示されません。

#SVGマスクのフォールバック

塗りつぶしのフェードには、SVGの図形にCSSマスクを適用します。SVGの図形にマスクを適用できないブラウザーでは、線と同じ色で均一に淡く塗りつぶします。

#クラス

クラス説明
sparkline描画のサイズを合わせるボックスです。幅は 4em、高さは .75em で、下端を大文字の高さの中央に揃えます。role="img" と傾向を示す aria-label を付けるか、数値を隣に表示する場合は aria-hidden="true" を付けます。
sparkline-success好ましい傾向を示す成功色です。隣のテキストでも同じ意味を伝えます。sparkline と併用します
sparkline-warning注意が必要な傾向を示す警告色です。隣のテキストでも同じ意味を伝えます。sparkline と併用します
sparkline-danger悪化した傾向を示す危険色です。隣のテキストでも同じ意味を伝えます。sparkline と併用します
sparkline-plot描画には viewBox="0 0 100 100" と preserveAspectRatio="none" を指定した SVG を使い、スパークラインのボックスに合わせて伸縮させます。線はボックスの外にはみ出してもかまいません。
sparkline-line傾向を表す線は周囲の文字色を使った1.5pxの線で、接合部と端は丸く、描画が伸縮しても線幅を保つ
sparkline-area線の下の領域です。線と同じ点列を下端に沿って閉じ、線の色を薄めて塗り、下に向かってフェードアウトさせます。線の前に配置します。
sparkline-baseline基準値、平均値、目標値を示す極細の点線。データ線より控えめ
sparkline-dot最新値を示す 5px の点。--xと--yで位置を指定し、描画領域が伸縮しても正円を保ちます。背後の面と同色のリングで、点の周囲の線を抜きます。

#カスタムプロパティ

プロパティ既定値説明
--sparkline-widthスパークラインの幅。未指定時は4em。表などのコンテナーに設定すると、その中のすべてのスパークラインの幅を指定できる
--sparkline-heightスパークラインの高さ。未設定時は大文字とほぼ同じ高さの .75em。コンテナーにも設定できます。
--sparkline-stroke1.5px線の太さ
--sparkline-dot5px最新値を示すドットのサイズ
--x点の横位置です。描画の最後の点の x 座標を 0〜100 で指定します。指定先:sparkline-dot
--y点の縦位置です。描画の最後の点の y 座標を、上端の 0 から下端の 100 までの範囲で指定します。指定先:sparkline-dot

#ソース

@master/ui/components/sparkline.css
/* * Sparkline: a trend as one fine line, small enough for a sentence, a row or a stat. * It is an inline SVG drawn in a box 100 units wide and 100 tall that stretches to the sparkline's size * (preserveAspectRatio="none"), so the points are written once and the line fits whatever room it is given. * The line keeps a 1.5px stroke however far the box stretches (vector-effect: non-scaling-stroke), with round * joins and ends, in the ink of the text around it: a sparkline in a sentence, in a card and on an ink surface * needs no variant. Under it an optional area fades out downward, and an optional hairline marks a baseline * or a target. The latest value is a dot of HTML placed by --x and --y on the same 100 by 100 scale, since a * circle drawn in the SVG would stretch with it; a ring of the surface behind it cuts it out of the line. * It is 4em wide and .75em tall, about the height of a capital, and its middle stands on the middle of the * capitals beside it, so it sits in a line of text like a word; set its size on it, or on a container such as * a table, to stand in a stat or the rows of a column. It is not interactive and has no states. The status * inks are for a trend whose words say the same thing beside it. * The area fades with a mask; a browser that cannot mask an SVG shape shows it as an even tint. */@layer components.ui {  .sparkline {    --sparkline-stroke: 1.5px;    --sparkline-dot: 5px;    position: relative;    display: inline-block;    flex: none;    box-sizing: border-box;    /* Read with a fallback, so a table or a list can size every sparkline it holds at once. */    width: var(--sparkline-width, 4em);    height: var(--sparkline-height, .75em);    margin: 0;    /* Its middle stands on the middle of a capital (.355em in Geist), whatever its height. */    vertical-align: calc(.355em - var(--sparkline-height, .75em) / 2);    /* A dot at the end needs half its width beside the line, inside the sparkline's own box. */    &:has(.sparkline-dot) {      padding-right: calc(var(--sparkline-dot) / 2);    }    &.sparkline-success {      color: var(--color-success);    }    &.sparkline-warning {      color: var(--color-warning);    }    &.sparkline-danger {      color: var(--color-danger);    }    @media (forced-colors: active) {      color: CanvasText;    }  }  /* The drawing. Strokes may stand half their width past its edge, so nothing at the top or bottom is cut. */  .sparkline-plot {    display: block;    width: 100%;    height: 100%;    overflow: visible;    fill: none;  }  .sparkline-line {    fill: none;    stroke: currentColor;    stroke-width: var(--sparkline-stroke);    stroke-linecap: round;    stroke-linejoin: round;    vector-effect: non-scaling-stroke;  }  /* The area under the line: a tint of its ink that fades out toward the foot. */  .sparkline-area {    fill: color-mix(in oklab, currentColor 16%, transparent);    stroke: none;    mask-image: linear-gradient(to bottom, black, transparent);    @media (forced-colors: active) {      fill: none;    }  }  /* A baseline or a target: a dotted hairline, quieter than the line. */  .sparkline-baseline {    fill: none;    stroke: color-mix(in oklab, currentColor 32%, transparent);    stroke-width: 1px;    stroke-dasharray: 1 2;    vector-effect: non-scaling-stroke;    /* A tint of the forced text color would be an author color: the hairline is the system's gray, as a chart's grid is. */    @media (forced-colors: active) {      stroke: GrayText;    }  }  /* The latest value: a round dot by its middle at --x and --y, cut out of the line by a ring of the surface. */  .sparkline-dot {    position: absolute;    top: calc(var(--y, 50) * 1%);    left: calc((100% - var(--sparkline-dot) / 2) * var(--x, 100) / 100);    width: var(--sparkline-dot);    height: var(--sparkline-dot);    margin: calc(var(--sparkline-dot) / -2) 0 0 calc(var(--sparkline-dot) / -2);    border-radius: 999px;    corner-shape: round;    background: currentColor;    box-shadow: 0 0 0 1.5px var(--surface-color, var(--color-surface-base));    pointer-events: none;    @media (forced-colors: active) {      forced-color-adjust: none;      background: CanvasText;      box-shadow: none;    }  }}