網站導覽

v2.1.0
資料顯示

迷你趨勢線

用一條細線呈現趨勢,小巧到能放進句子、表格列或統計數值元件。

本頁內容

迷你趨勢線用一條細線呈現趨勢,能就近放在說明文字所在的位置,例如 統計數值 元件中的數字下方、表格 的欄位內,或句子中。它是行內 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>

#線條的繪製方式

圖形由 <svg class="sparkline-plot"> 繪製,並設定 viewBox="0 0 100 100" 和 preserveAspectRatio="none"。這會建立一個寬、高各為 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 圓形隨圖形拉伸而變成橢圓。沿用寬、高各為 100 的座標系統,以最後一個點的座標設定 --x 和 --y,將圓點的中心放在該處。圓點外圍有一圈與背景同色的外框,遮住靠近圓點的線段,讓線條在圓點邊緣收尾。迷你趨勢線會在自己的方框內留出半個圓點的空間,避免圓點貼到後方的文字。

#填色區域與基準線

sparkline-area 是穿過相同座標點、沿底端閉合的 <path>(V100 H0 Z)。填色沿用線條色的淡色調,向下逐漸淡出。將它放在線條之前,讓線條繪製在填色上方。

sparkline-baseline 是一條極細的點線,以 <line> 在平均值、目標值或上一期數值所對應的高度從 0 畫到 100。基準線比趨勢線低調,並由迷你趨勢線旁的文字說明它代表什麼,如上方的統計數值範例所示。

#句中顯示

迷你趨勢線寬 4em、高 .75em,高度約等於大寫字母。它的垂直中心與旁邊大寫字母的中心對齊,因此能像單字一樣排在同一行。不論放在頁面、卡片或採用 ink 配色的區塊上,都沿用所在位置的文字色。

句中顯示

<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",並以 aria-label 描述走勢,例如「每日從 31,000 增至 61,000」。這樣輔助技術就會把其中的圖形視為一張圖片。
  • 如果旁邊的文字或數字已說明走勢,例如在句子或統計數值元件中,請用 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圓點的水平位置,範圍為 0 到 100,取繪圖中最後一點的 x 座標設定於 sparkline-dot
--y圓點的垂直位置,頂端為 0,底端為 100,取繪圖中最後一點的 y 座標設定於 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;    }  }}