导航

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>

#线条的绘制方式

图形使用带有 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 圆形会随图形拉伸成椭圆。用 --x 和 --y 将圆点的中心定位到最后一个点的坐标上,沿用同一套 100 × 100 的坐标。圆点周围有一圈与背后表面同色的描边,遮住靠近圆点的线条,使线条在圆点处收尾。迷你趋势线在自身的盒子内为圆点留出半个圆点宽度的空间,避免它挤到后面的文字。

#填充区域与基准线

sparkline-area 用一条 <path> 连接相同的点,再沿底边闭合(V100 H0 Z)。填充沿用线条的颜色,以较淡的效果向下渐隐。将填充区域放在线条之前,使线条叠绘在填充区域上。

sparkline-baseline 是一条由点组成的细线,用 <line> 在均值、目标值或上一周期数值对应的高度从 0 画到 100。它比趋势线更淡,旁边的文字说明它标记的值,如上方的统计指标所示。

#在句子中使用

迷你趋势线宽 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",并用 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;    }  }}