網站導覽

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() 之前,請像本頁範例一樣,同時設定 ARIA 屬性與對應的 CSS 自訂屬性。

軌道和填色的兩端都是圓頭,讀值變動時,填色會平順地過渡到新位置。填色長度為零時,只會顯示未填色的軌道,填色的圓頭大小也會縮為零。

自訂範圍

<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-sm錶盤直徑為 88px,預設為 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;  }}