导航

v2.1.0
数据展示

仪表盘

表盘上的弧线填充到读数对应的位置。

本页内容

仪表盘用一段占整圆三分之二、底部留有开口的圆弧显示读数,墨色沿圆弧填充至读数对应的位置。数字居中,数字下方的简短标签位于圆弧两端之间。元素带有 role="meter",读数以文本呈现,圆弧由样式表绘制。无需脚本或 SVG。

仪表盘

<div class="card max-w(480) mx-auto">  <div class="flex flex-wrap justify-around gap-md">    <div class="gauge" role="meter" aria-label="CPU" aria-valuenow="38" style="--gauge-value: 38">      <span class="gauge-value">38%</span>      <span class="gauge-label">CPU</span>    </div>    <div class="gauge" role="meter" aria-label="Memory" aria-valuenow="72" style="--gauge-value: 72">      <span class="gauge-value">72%</span>      <span class="gauge-label">Memory</span>    </div>    <div class="gauge" role="meter" aria-label="Disk" aria-valuenow="91" style="--gauge-value: 91">      <span class="gauge-value">91%</span>      <span class="gauge-label">Disk</span>    </div>  </div></div>
<div className="card max-w(480) mx-auto">  <div className="flex flex-wrap justify-around gap-md">    <div className="gauge" role="meter" aria-label="CPU" aria-valuenow={38} style={{ '--gauge-value': '38' } as React.CSSProperties}>      <span className="gauge-value">38%</span>      <span className="gauge-label">CPU</span>    </div>    <div className="gauge" role="meter" aria-label="Memory" aria-valuenow={72} style={{ '--gauge-value': '72' } as React.CSSProperties}>      <span className="gauge-value">72%</span>      <span className="gauge-label">Memory</span>    </div>    <div className="gauge" role="meter" aria-label="Disk" aria-valuenow={91} style={{ '--gauge-value': '91' } as React.CSSProperties}>      <span className="gauge-value">91%</span>      <span className="gauge-label">Disk</span>    </div>  </div></div>
<div class="card max-w(480) mx-auto">  <div class="flex flex-wrap justify-around gap-md">    <div class="gauge" role="meter" aria-label="CPU" aria-valuenow="38" style="--gauge-value: 38">      <span class="gauge-value">38%</span>      <span class="gauge-label">CPU</span>    </div>    <div class="gauge" role="meter" aria-label="Memory" aria-valuenow="72" style="--gauge-value: 72">      <span class="gauge-value">72%</span>      <span class="gauge-label">Memory</span>    </div>    <div class="gauge" role="meter" aria-label="Disk" aria-valuenow="91" style="--gauge-value: 91">      <span class="gauge-value">91%</span>      <span class="gauge-label">Disk</span>    </div>  </div></div>
<template>  <div class="card max-w(480) mx-auto">    <div class="flex flex-wrap justify-around gap-md">      <div class="gauge" role="meter" aria-label="CPU" aria-valuenow="38" style="--gauge-value: 38">        <span class="gauge-value">38%</span>        <span class="gauge-label">CPU</span>      </div>      <div class="gauge" role="meter" aria-label="Memory" aria-valuenow="72" style="--gauge-value: 72">        <span class="gauge-value">72%</span>        <span class="gauge-label">Memory</span>      </div>      <div class="gauge" role="meter" aria-label="Disk" aria-valuenow="91" style="--gauge-value: 91">        <span class="gauge-value">91%</span>        <span class="gauge-label">Disk</span>      </div>    </div>  </div></template>

#如何确定填充弧长

读数由 aria-valuenow 指定,取值介于 aria-valuemin 和 aria-valuemax 之间。省略范围属性时,下限为 0,上限为 100。支持带类型的 attr() 的浏览器可直接读取这些属性,因此数值只需写一次。其他浏览器读取 --gauge-value,自定义范围时还会读取 --gauge-min 和 --gauge-max。在所有浏览器引擎都支持带类型的 attr() 之前,请像这里的示例一样同时写上两套值。

轨道和填充弧均采用圆形端点,读数变化时,填充弧平滑过渡。填充量为零时,填充弧的端点大小也为零,只显示空轨道。

自定义范围

<div class="gauge" role="meter" aria-label="Engine speed" aria-valuenow="4200" aria-valuemin="0" aria-valuemax="8000" style="--gauge-value: 4200; --gauge-max: 8000">  <span class="gauge-value">4,200</span>  <span class="gauge-label">rpm</span></div><div class="gauge" role="meter" aria-label="Rating" aria-valuenow="4.6" aria-valuemin="1" aria-valuemax="5" style="--gauge-value: 4.6; --gauge-min: 1; --gauge-max: 5">  <span class="gauge-value">4.6</span>  <span class="gauge-label">of 5</span></div><div class="gauge" role="meter" aria-label="Queue" aria-valuenow="0" style="--gauge-value: 0">  <span class="gauge-value">0</span>  <span class="gauge-label">Queued</span></div>
<>  <div className="gauge" role="meter" aria-label="Engine speed" aria-valuenow={4200} aria-valuemin={0} aria-valuemax={8000} style={{ '--gauge-value': '4200', '--gauge-max': '8000' } as React.CSSProperties}>    <span className="gauge-value">4,200</span>    <span className="gauge-label">rpm</span>  </div>  <div className="gauge" role="meter" aria-label="Rating" aria-valuenow={4.6} aria-valuemin={1} aria-valuemax={5} style={{ '--gauge-value': '4.6', '--gauge-min': '1', '--gauge-max': '5' } as React.CSSProperties}>    <span className="gauge-value">4.6</span>    <span className="gauge-label">of 5</span>  </div>  <div className="gauge" role="meter" aria-label="Queue" aria-valuenow={0} style={{ '--gauge-value': '0' } as React.CSSProperties}>    <span className="gauge-value">0</span>    <span className="gauge-label">Queued</span>  </div></>
<div class="gauge" role="meter" aria-label="Engine speed" aria-valuenow="4200" aria-valuemin="0" aria-valuemax="8000" style="--gauge-value: 4200; --gauge-max: 8000">  <span class="gauge-value">4,200</span>  <span class="gauge-label">rpm</span></div><div class="gauge" role="meter" aria-label="Rating" aria-valuenow="4.6" aria-valuemin="1" aria-valuemax="5" style="--gauge-value: 4.6; --gauge-min: 1; --gauge-max: 5">  <span class="gauge-value">4.6</span>  <span class="gauge-label">of 5</span></div><div class="gauge" role="meter" aria-label="Queue" aria-valuenow="0" style="--gauge-value: 0">  <span class="gauge-value">0</span>  <span class="gauge-label">Queued</span></div>
<template>  <div class="gauge" role="meter" aria-label="Engine speed" aria-valuenow="4200" aria-valuemin="0" aria-valuemax="8000" style="--gauge-value: 4200; --gauge-max: 8000">    <span class="gauge-value">4,200</span>    <span class="gauge-label">rpm</span>  </div>  <div class="gauge" role="meter" aria-label="Rating" aria-valuenow="4.6" aria-valuemin="1" aria-valuemax="5" style="--gauge-value: 4.6; --gauge-min: 1; --gauge-max: 5">    <span class="gauge-value">4.6</span>    <span class="gauge-label">of 5</span>  </div>  <div class="gauge" role="meter" aria-label="Queue" aria-valuenow="0" style="--gauge-value: 0">    <span class="gauge-value">0</span>    <span class="gauge-label">Queued</span>  </div></template>

#尺寸

仪表盘默认宽 120px。gauge-sm 宽 88px,适合紧凑仪表板中的卡片。gauge-lg 宽 160px,适合以单个读数为重点的视图。弧线的描边粗细为仪表盘宽度的十五分之一,数字也随尺寸缩放。省略 gauge-label 时,数字位于表盘正中央。

尺寸

<div class="gauge gauge-sm" role="meter" aria-label="Battery" aria-valuenow="64" style="--gauge-value: 64">  <span class="gauge-value">64%</span>  <span class="gauge-label">Battery</span></div><div class="gauge" role="meter" aria-label="Battery" aria-valuenow="64" style="--gauge-value: 64">  <span class="gauge-value">64%</span>  <span class="gauge-label">Battery</span></div><div class="gauge gauge-lg" role="meter" aria-label="Battery" aria-valuenow="64" style="--gauge-value: 64">  <span class="gauge-value">64%</span></div>
<>  <div className="gauge gauge-sm" role="meter" aria-label="Battery" aria-valuenow={64} style={{ '--gauge-value': '64' } as React.CSSProperties}>    <span className="gauge-value">64%</span>    <span className="gauge-label">Battery</span>  </div>  <div className="gauge" role="meter" aria-label="Battery" aria-valuenow={64} style={{ '--gauge-value': '64' } as React.CSSProperties}>    <span className="gauge-value">64%</span>    <span className="gauge-label">Battery</span>  </div>  <div className="gauge gauge-lg" role="meter" aria-label="Battery" aria-valuenow={64} style={{ '--gauge-value': '64' } as React.CSSProperties}>    <span className="gauge-value">64%</span>  </div></>
<div class="gauge gauge-sm" role="meter" aria-label="Battery" aria-valuenow="64" style="--gauge-value: 64">  <span class="gauge-value">64%</span>  <span class="gauge-label">Battery</span></div><div class="gauge" role="meter" aria-label="Battery" aria-valuenow="64" style="--gauge-value: 64">  <span class="gauge-value">64%</span>  <span class="gauge-label">Battery</span></div><div class="gauge gauge-lg" role="meter" aria-label="Battery" aria-valuenow="64" style="--gauge-value: 64">  <span class="gauge-value">64%</span></div>
<template>  <div class="gauge gauge-sm" role="meter" aria-label="Battery" aria-valuenow="64" style="--gauge-value: 64">    <span class="gauge-value">64%</span>    <span class="gauge-label">Battery</span>  </div>  <div class="gauge" role="meter" aria-label="Battery" aria-valuenow="64" style="--gauge-value: 64">    <span class="gauge-value">64%</span>    <span class="gauge-label">Battery</span>  </div>  <div class="gauge gauge-lg" role="meter" aria-label="Battery" aria-valuenow="64" style="--gauge-value: 64">    <span class="gauge-value">64%</span>  </div></template>

#用读数表示状态

填充弧默认使用墨色。读数本身表示警告时,将 --gauge-color 设为相应的状态颜色,并用文字说明警告含义。状态信息不能只靠颜色传达。轨道沿用填充弧的颜色,视觉上更淡。

状态

<div class="gauge" role="meter" aria-label="Storage" aria-valuenow="84" aria-valuetext="84%, nearly full" style="--gauge-value: 84; --gauge-color: var(--color-warning)">  <span class="gauge-value">84%</span>  <span class="gauge-label">Nearly full</span></div><div class="gauge" role="meter" aria-label="Error budget" aria-valuenow="97" aria-valuetext="97% spent, over budget soon" style="--gauge-value: 97; --gauge-color: var(--color-danger)">  <span class="gauge-value">97%</span>  <span class="gauge-label">Spent</span></div>
<>  <div className="gauge" role="meter" aria-label="Storage" aria-valuenow={84} aria-valuetext="84%, nearly full" style={{ '--gauge-value': '84', '--gauge-color': 'var(--color-warning)' } as React.CSSProperties}>    <span className="gauge-value">84%</span>    <span className="gauge-label">Nearly full</span>  </div>  <div className="gauge" role="meter" aria-label="Error budget" aria-valuenow={97} aria-valuetext="97% spent, over budget soon" style={{ '--gauge-value': '97', '--gauge-color': 'var(--color-danger)' } as React.CSSProperties}>    <span className="gauge-value">97%</span>    <span className="gauge-label">Spent</span>  </div></>
<div class="gauge" role="meter" aria-label="Storage" aria-valuenow="84" aria-valuetext="84%, nearly full" style="--gauge-value: 84; --gauge-color: var(--color-warning)">  <span class="gauge-value">84%</span>  <span class="gauge-label">Nearly full</span></div><div class="gauge" role="meter" aria-label="Error budget" aria-valuenow="97" aria-valuetext="97% spent, over budget soon" style="--gauge-value: 97; --gauge-color: var(--color-danger)">  <span class="gauge-value">97%</span>  <span class="gauge-label">Spent</span></div>
<template>  <div class="gauge" role="meter" aria-label="Storage" aria-valuenow="84" aria-valuetext="84%, nearly full" style="--gauge-value: 84; --gauge-color: var(--color-warning)">    <span class="gauge-value">84%</span>    <span class="gauge-label">Nearly full</span>  </div>  <div class="gauge" role="meter" aria-label="Error budget" aria-valuenow="97" aria-valuetext="97% spent, over budget soon" style="--gauge-value: 97; --gauge-color: var(--color-danger)">    <span class="gauge-value">97%</span>    <span class="gauge-label">Spent</span>  </div></template>

#仪表盘、计量条和进度指示器

仪表盘和计量条都显示一定范围内的测量值。读数是展示重点且有独立空间时,例如在仪表板上,使用仪表盘。读数放在表单或表格的一行中时,使用计量条。进度指示器用于有明确完成节点的任务,其环形样式采用闭合的圆环。仪表盘的圆弧始终留有开口。

#无障碍

  • 为元素添加 role="meter",用 aria-label 或 aria-labelledby 提供无障碍名称,并在 aria-valuenow 中填写读数。范围不是 0 到 100 时,添加 aria-valuemin 和 aria-valuemax。
  • 在 gauge-value 的文本中再写一次读数,并注明单位。数字需要文字说明才能理解时,添加 aria-valuetext。
  • 圆弧由样式表绘制,屏幕阅读器不会播报圆弧本身。在强制颜色模式下,填充弧使用系统高亮色,轨道使用系统灰色。

#类名

类名说明
gauge表盘直径为 120px,使用带 role="meter" 和无障碍名称的元素,将读数写入 aria-valuenow。表盘是底部开口的三分之二圆环,按读数填充墨色。
gauge-sm88px 表盘,默认为 120px与 gauge 一同使用
gauge-lg160px 的表盘,默认为 120px与 gauge 一同使用
gauge-value在表盘中央用大号等宽数字显示读数
gauge-label读数下方、圆弧两端之间放一个弱化显示的词作为标签。省略标签时,读数居中。

#自定义属性

属性默认值说明
--gauge-value读数。在支持带类型的 attr() 时从 aria-valuenow 获取,否则手动设置
--gauge-min0范围下限。支持带类型的 attr() 时,从 aria-valuemin 读取
--gauge-max100范围上限。支持带类型的 attr() 时,从 aria-valuemax 读取。
--gauge-size7.5rem表盘直径
--gauge-widthcalc(var(--gauge-size) / 15)圆弧粗细。
--gauge-colorvar(--color-surface-inverse)填充部分的墨色,轨道采用同色的淡化版本。读数表示某种状态时,设置对应的状态颜色,并用文字说明状态。

#源码

@master/ui/components/gauge.css
/* * Gauge: a reading on a dial, for one measurement a reader takes in at a glance. * An element with role="meter" draws two thirds of a ring, open at the foot, and fills it with ink as far as * the reading has come. The figure stands in the middle and one word under it, between the two ends of the arc. * Where the browser supports typed attr(), the gauge reads aria-valuenow, aria-valuemin and aria-valuemax * itself, so the reading is written once, for people and for the drawing; elsewhere set --gauge-value. * The track and the fill both have round ends, and the fill eases to each new reading. * A gauge is 120px across (88 small, 160 large) and its stroke is a fifteenth of that. */@property --gauge-value {  syntax: '<number>';  inherits: true;  initial-value: 0;}@layer components.ui {  .gauge {    --gauge-size: 7.5rem;    --gauge-width: calc(var(--gauge-size) / 15);    --gauge-color: var(--color-surface-inverse);    --gauge-min: 0;    --gauge-max: 100;    /* How far along the arc the reading is, from nothing to all of it. */    --gauge-share: clamp(0, calc((var(--gauge-value) - var(--gauge-min)) / (var(--gauge-max) - var(--gauge-min))), 1);    --gauge-radius: calc((var(--gauge-size) - var(--gauge-width)) / 2);    position: relative;    display: inline-grid;    flex: none;    align-content: end;    justify-items: center;    gap: calc(var(--gauge-size) * .05);    box-sizing: border-box;    width: var(--gauge-size);    /* The arc ends a quarter of the way below the middle, so the box stops there, and so does what it can scroll. */    height: calc(var(--gauge-size) * .75 + var(--gauge-width) / 4);    margin: 0;    overflow: clip;    /* The figure and the word rise over the drawings inside the gauge, not over what stands near it on the page. */    isolation: isolate;    color: var(--color-text-strong);    font: var(--font-weight-control) calc(var(--gauge-size) * .235)/1 var(--font-family-sans);    font-feature-settings: inherit;    font-variant-numeric: tabular-nums;    letter-spacing: -.02em;    text-align: center;    vertical-align: middle;    transition: --gauge-value var(--duration-base) var(--easing-glide);    @supports (x: attr(x type(*))) {      &[aria-valuenow] {        --gauge-value: attr(aria-valuenow type(<number>), 0);      }      &[aria-valuemin] {        --gauge-min: attr(aria-valuemin type(<number>), 0);      }      &[aria-valuemax] {        --gauge-max: attr(aria-valuemax type(<number>), 100);      }    }    /* Both drawings are a square as wide as the gauge, cut down to a ring; the box shows its upper three quarters. */    &::before, &::after {      content: '';      position: absolute;      inset: 0 0 auto;      height: var(--gauge-size);      border-radius: 999px;      mask: radial-gradient(farthest-side, transparent calc(100% - var(--gauge-width) - .5px), black calc(100% - var(--gauge-width)), black calc(100% - .5px), transparent);      pointer-events: none;    }    /*     * The track: the whole arc, from 240 to 480 degrees, with a dot at each end to round it. It is drawn in     * the fill's own color and stepped back as one layer, so the dots and the arc never add up.     */    &::before {      background:        radial-gradient(circle at calc(50% + var(--gauge-radius) * sin(240deg)) calc(50% - var(--gauge-radius) * cos(240deg)), var(--gauge-color) calc(var(--gauge-width) / 2 - .5px), transparent calc(var(--gauge-width) / 2)),        radial-gradient(circle at calc(50% + var(--gauge-radius) * sin(480deg)) calc(50% - var(--gauge-radius) * cos(480deg)), var(--gauge-color) calc(var(--gauge-width) / 2 - .5px), transparent calc(var(--gauge-width) / 2)),        conic-gradient(from 240deg, var(--gauge-color) 240deg, transparent 0);      opacity: .12;    }    /* The fill: the same arc as far as the reading. Its end dots have no radius at nothing, so an empty gauge is an empty track. */    &::after {      --gauge-end: calc(240deg + var(--gauge-share) * 240deg);      --gauge-cap: calc(var(--gauge-width) / 2 * min(1, var(--gauge-share) * 200));      background:        radial-gradient(circle at calc(50% + var(--gauge-radius) * sin(240deg)) calc(50% - var(--gauge-radius) * cos(240deg)), var(--gauge-color) calc(var(--gauge-cap) - .5px), transparent var(--gauge-cap)),        radial-gradient(circle at calc(50% + var(--gauge-radius) * sin(var(--gauge-end))) calc(50% - var(--gauge-radius) * cos(var(--gauge-end))), var(--gauge-color) calc(var(--gauge-cap) - .5px), transparent var(--gauge-cap)),        conic-gradient(from 240deg, var(--gauge-color) calc(var(--gauge-share) * 240deg), transparent 0);    }    /* Without a word under it, the figure stands at the middle of the dial. */    &:not(:has(.gauge-label)) {      align-content: center;      padding-block-start: calc(var(--gauge-size) * .2);    }    &.gauge-sm {      --gauge-size: 5.5rem;    }    &.gauge-lg {      --gauge-size: 10rem;    }    @media (prefers-reduced-motion: reduce) {      transition: none;    }    /* Only the drawings keep their colors: the figure and the word take the system's text. */    @media (forced-colors: active) {      --gauge-color: Highlight;      &::before, &::after {        forced-color-adjust: none;      }      &::before {        --gauge-color: GrayText;        opacity: 1;      }    }  }  /* The reading, in the gauge's own large figures. */  .gauge-value {    position: relative;    z-index: 1;    max-width: 100%;    margin: 0;  }  /* What is measured: one quiet word between the ends of the arc. */  .gauge-label {    position: relative;    z-index: 1;    max-width: 62%;    margin: 0;    overflow: hidden;    color: var(--color-text-muted);    font: 400 .75rem/1rem var(--font-family-sans);    font-feature-settings: inherit;    letter-spacing: 0;    text-overflow: ellipsis;    white-space: nowrap;  }  .gauge-sm .gauge-label {    font-size: .6875rem;    line-height: .875rem;  }  .gauge-lg .gauge-label {    font-size: .8125rem;    line-height: 1.125rem;  }}