圖表
用單一 SVG 呈現數值,適應任何寬度,支援從折線圖到旭日圖等二十多種圖表。
本頁內容
依讀者想了解的問題選擇圖表形式,例如折線圖、長條圖、圓環圖、直方圖或漏斗圖,總共有二十多種。每種圖表都以單一 SVG 配合可用寬度。刻度上的圖形以百分比定位,繪圖區無論拉得多寬,線條粗細和文字大小都維持原樣。單一資料序列使用前景色,如同進度條以此色填滿。有多個資料序列時,則使用類別配色。每個資料點都是清單項目,並以文字說明其數值。
圖表
<div class="card max-w(560) mx-auto"> <div class="card-header"> <h3 class="card-title">Requests</h3> <p class="card-description">Millions per month, by region</p> </div> <figure class="chart" id="requests"></figure></div><script>addEventListener('DOMContentLoaded', function () {MasterUI.chart(document.getElementById('requests'), { type: 'area', label: 'Requests per month by region, in millions', labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], series: [ { name: 'Tokyo', values: [32, 36, 34, 41, 46, 44, 52, 56, 54, 61, 68, 72] }, { name: 'Frankfurt', values: [12, 14, 18, 17, 22, 26, 25, 30, 34, 33, 39, 42] } ]})})</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { Chart } from './ui/chart'<div className="card max-w(560) mx-auto"> <div className="card-header"> <h3 className="card-title">Requests</h3> <p className="card-description">Millions per month, by region</p> </div> <Chart id="requests" type="area" label="Requests per month by region, in millions" labels={['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']} series={[ { name: 'Tokyo', values: [32, 36, 34, 41, 46, 44, 52, 56, 54, 61, 68, 72] }, { name: 'Frankfurt', values: [12, 14, 18, 17, 22, 26, 25, 30, 34, 33, 39, 42] } ]} /></div><script> import Chart from './ui/Chart.svelte'</script><div class="card max-w(560) mx-auto"> <div class="card-header"> <h3 class="card-title">Requests</h3> <p class="card-description">Millions per month, by region</p> </div> <Chart id="requests" type="area" label="Requests per month by region, in millions" labels={['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']} series={[ { name: 'Tokyo', values: [32, 36, 34, 41, 46, 44, 52, 56, 54, 61, 68, 72] }, { name: 'Frankfurt', values: [12, 14, 18, 17, 22, 26, 25, 30, 34, 33, 39, 42] } ]} /></div><script setup>import Chart from './ui/Chart.vue'</script><template> <div class="card max-w(560) mx-auto"> <div class="card-header"> <h3 class="card-title">Requests</h3> <p class="card-description">Millions per month, by region</p> </div> <Chart id="requests" type="area" label="Requests per month by region, in millions" :labels="['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']" :series="[ { name: 'Tokyo', values: [32, 36, 34, 41, 46, 44, 52, 56, 54, 61, 68, 72] }, { name: 'Frankfurt', values: [12, 14, 18, 17, 22, 26, 25, 30, 34, 33, 39, 42] } ]" /> </div></template>#繪製方式
繪圖區使用沒有 viewBox 的 <svg>,寬度填滿可用空間,高度由 --chart-height 設定,預設為 14rem。極細線、刻度線、長條和圓點都以繪圖區的百分比定位,所以 x="42%" 的長條在任何寬度下都位於 42% 的位置,圓角半徑和文字大小也維持原樣。
只有必須用路徑表示的圖形,也就是線條、區域或扇形,才會繪製在 chart-canvas 裡。這是一個座標範圍為 100 × 100 的巢狀 SVG,會拉伸至填滿繪圖區。筆畫粗細不隨之縮放,所以無論卡片寬窄,線寬都是 --chart-stroke。
寬度改變時,圖表無須測量尺寸或重新繪製,因此伺服器、框架元件和行為都會產生相同的標記。圖表會填滿可用寬度。若將它和其他項目並排,請指定寬度或讓它伸展以填滿剩餘空間。
#繪製圖表
圖表行為會將圖表寫入帶有 chart 類別的元素。label 說明圖表呈現的內容與單位,labels 為刻度上的各個位置命名,而每個資料序列在每個位置各有一個數值。
import { chart } from '@master/ui/behaviors/chart.js'chart(document.querySelector('.chart'), { type: 'line', label: 'Latency by region, in milliseconds', labels: ['Jan', 'Feb', 'Mar', 'Apr'], series: [ { name: 'Tokyo', values: [42, 44, 41, 47] }, { name: 'Frankfurt', values: [61, 58, 63, 66] }, ],})在伺服器端,chartMarkup() 會以字串傳回相同的圖表,其中的文字均已跳脫。將字串放入元素後,讀者在任何腳本執行前就能閱讀頁面上的圖表。chartLayout() 會傳回繪圖所用的資料,包括刻度範圍與刻度、各資料序列的路徑,以及每個資料點的圖形標記與描述文字。
import { chartMarkup } from '@master/ui/behaviors/chart.js'const html = `<figure class="chart">${chartMarkup({ type: 'bar', label, labels, series })}</figure>`對該元素呼叫 chart() 且不傳入資料序列,即可讓既有的圖表標記具備互動行為。@master/ui/behaviors 的 attach() 則會為頁面上的所有圖表加上互動行為。
#只載入頁面使用的圖表形式
chart.js 包含所有形式,壓縮程式碼並以 gzip 壓縮後約為 16 kB。若頁面只繪製一兩種圖表,可以只載入需要的形式。chart-core.js 約為 6 kB,本身未包含任何圖表形式。各形式各有模組,可傳給 chartForms()。折線圖、區域圖和長條圖共用刻度,三者合計增加約 2 kB,其餘形式各增加不到 1 kB。
import { chartCore, chartForms } from '@master/ui/behaviors/chart-core.js'import { chartCartesian } from '@master/ui/behaviors/chart-cartesian.js'chartForms(chartCartesian)chartCore(document.querySelector('.chart'), { type: 'line', label, labels, series })| 模組 | 繪製內容 |
|---|---|
chart-cartesian.js | line, area, bar |
chart-scatter.js | scatter |
chart-pie.js | pie, donut |
chart-rings.js, chart-radar.js, chart-sunburst.js | rings, radar, sunburst |
chart-waterfall.js, chart-range.js | waterfall; range, dumbbell |
chart-slope.js, chart-bump.js | slope, bump |
chart-marimekko.js, chart-funnel.js, chart-waffle.js, chart-treemap.js | marimekko, funnel, waffle, treemap |
chart-histogram.js, chart-box.js, chart-heatmap.js | histogram, box, heatmap |
若指定了尚未載入的圖表類型,就會立即擲出錯誤,指出應匯入的模組。
#圖表形式
每種圖表都有專屬頁面,提供範例與相關選項。
| 形式 | type | 用途 |
|---|---|---|
| 折線圖 | line | 以線段依序連接資料點,呈現數值變化。 |
| 面積圖 | area | 在折線下方填上同色的淡色,可單獨呈現,也可堆疊以顯示總量。 |
| 斜率圖 | slope | 從線條的斜率與交會看出兩個時間點之間的上升、下降與相對位置變化。 |
| 排名變化圖 | bump | 以線條呈現各項目的排名變化,線條交會處表示排名互換。 |
| 長條圖 | bar | 用共用基準線的長條比較數量,可並排、堆疊或橫向排列。 |
| 雷達圖 | radar | 沿共用指標比較多個項目,每個項目各以環繞同一個中心的輪廓表示。 |
| 瀑布圖 | waterfall | 用逐步增加與減少的數值,呈現起始值如何變成最終值。 |
| 區間圖 | range | 呈現各區間的起點與終點。 |
| 啞鈴圖 | dumbbell | 以線段連接各項目的兩個數值,凸顯兩者之間的差距。 |
| 圓餅圖 | pie | 用扇形呈現整體的組成。 |
| 圓環圖 | donut | 在中央數值周圍呈現整體的各個部分。 |
| 多環圖 | rings | 每個讀值各用一個環,以填滿的比例表示讀值占總量的多少。 |
| 馬賽克圖 | marimekko | 各長條的寬度反映總量,條內再依組成比例分割。 |
| 漏斗圖 | funnel | 呈現流程的各個階段,寬度逐階縮小。 |
| 華夫格圖 | waffle | 以一百個方格呈現整體,每格代表 1%。 |
| 樹狀矩形圖 | treemap | 用矩形區塊呈現整體的各個部分,區塊面積反映各自的占比。 |
| 旭日圖 | sunburst | 用兩層圓環呈現整體、各個部分,以及各部分的細項。 |
| 直方圖 | histogram | 用易讀的數值作為組界,將讀值分入各組距,並以柱高表示各組的筆數,以呈現分布。 |
| 盒鬚圖 | box | 每組以五個統計量呈現分布,畫出盒體、中位數與兩端的鬚線。 |
| 熱圖 | heatmap | 在兩個維度上呈現資料模式,以格子的色深表示數值。 |
| 散佈圖 | scatter | 比較兩項指標之間的關係,每個項目以一個點表示。 |
折線圖、區域圖和長條圖可以共用刻度並放在同一張圖表中。為資料序列指定自己的 type,就會按指定形式繪製,例如在長條上疊加折線。
#數值提示與鍵盤操作
chart-tip 是數值提示。它先顯示資料點名稱,再逐列顯示各資料序列的色線、名稱與數值。提示會出現在資料點旁空間較多的一側,並依自身高度調整垂直位置,因此能留在繪圖區內,無須由腳本定位。互動行為會從資料點的圖形標記取得內容,並透過 --chart-x、--chart-y 和 --chart-reach 傳遞資料點的位置。
無論有多少資料點,整個繪圖區在 Tab 鍵巡覽順序中都只占一站。按方向鍵可逐點移動並查看數值提示,也可以用手指沿著圖表滑動。
| 按鍵 | 動作 |
|---|---|
| Tab | 進入繪圖區時,焦點落在上次停留的資料點,沒有時則落在第一個資料點,並顯示其數值提示;接著移到圖例的核取方塊 |
| ArrowRight, ArrowDown | 移到下一個資料點並顯示其數值提示 |
| ArrowLeft, ArrowUp | 移到前一個資料點 |
| Home, End | 移到第一個或最後一個資料點 |
| Escape | 隱藏數值提示,焦點留在該資料點上 |
| Space | 在圖例的核取方塊上,顯示或隱藏對應的序列 |
#圖例
有兩個以上資料序列的圖表會顯示圖例,讓使用者也能透過名稱辨識資料序列。每個圖例項目都是包住核取方塊的標籤,可按 Tab 移入焦點,再按空白鍵切換。取消勾選後,對應的資料序列會從圖表隱藏,刻度會配合剩餘資料調整,其他序列則保留原有色彩。單一資料序列的圖表由標題說明繪製內容,因此省略圖例。
toggle: false 會將圖例顯示為一般清單。若圖表周圍的文字已說明各資料序列的名稱,可用 legend: false 省略圖例。圖表的值是隱藏的資料序列名稱。可從 chart() 的回傳結果讀取這個值,也可從外部設定它來隱藏資料序列。
#色彩
單一資料序列使用前景色 --chart-color。多個資料序列或整體的各個部分,依固定順序使用類別色:藍、橘、藍綠、紫、琥珀、粉紅、綠和天藍。無論淺色或深色配置,常見色覺辨識障礙的使用者仍能區分相鄰的色彩。超過八個就會循環使用配色,可將其餘項目合併為「其他」,或另畫一張圖表。
若同一項目在各處都要使用同一色彩,請為資料序列指定 category。例如東京無論排在清單的哪個位置,在圖表、徽章和表格列中都使用藍色。資料序列不使用狀態色。若某個序列的表現不佳,請用文字說明。
#刻度與數值
刻度從零起算,有負值時則從零以下起算,並以高於最大值的易讀數值作為上限。預設約分成四個間距。可用 min 和 max 固定兩端,再用 ticks 指定其他間距數。長條圖的刻度應從零開始,因為長條的長度代表數值。
format 設定數值提示與長條末端的數值格式,tickFormat 設定刻度的數字格式。刻度在 10,000 以下顯示完整數字,超過時採用精簡格式。兩者均接收數字並傳回字串。locale 是用於數字格式的 BCP 47 語言標籤,預設為 en-US。由伺服器產生圖表時,請指定此選項,讓伺服器與用戶端使用相同的格式。
| 選項 | 預設 | |
|---|---|---|
type | 'bar' | 圖表形式中列出的其中一種類型 |
label | 無 | 繪圖區的無障礙名稱,說明圖表呈現的內容與單位 |
labels | [] | 刻度各處、雷達圖周圍或熱圖各欄上方的名稱 |
series | [] | 每個資料序列都有 name,並依圖表形式提供 values、value、points、ranges 或 children。也可選填 category、type、low、high,以及環形量表的 max |
stacked | false | 堆疊長條或區域。'percent' 將各組堆疊調整為 100%,'stream' 使堆疊區域沿中線置中 |
horizontal | false | 橫向繪製長條、範圍、啞鈴與盒鬚 |
mark | 'bar' | 'lollipop' 用末端帶圓點的線段表示長條 |
curve | 'smooth' | smooth、linear 或 step |
min, max, ticks | 自動配合資料、4 | 刻度的兩端與約略的間距數。散佈圖的橫軸刻度使用 xMin、xMax 和 xTicks |
axis | true | 是否顯示刻度數字。用於區塊內的小圖表時,可設為 false 以只保留極細線 |
format, tickFormat | 依語言標籤 | 數值與刻度的顯示格式。散佈圖的橫軸刻度使用 xFormat |
dots, values | false | 標示折線上的每個資料點,以及顯示各長條的數值 |
current | 無 | 標有 aria-current 的資料點索引 |
legend, toggle | 兩個以上資料序列時顯示、true | 是否顯示圖例,以及圖例項目是否使用核取方塊 |
hole, center | .7、無 | 圓環圖的中空比例,以及圓環圖或環形量表的中央內容 |
total | 無 | 瀑布圖最後一個長條的名稱 |
bins | 依資料筆數 | 直方圖將讀值分成的約略組數 |
ascending | false | 在排名變化圖中,數值最低者排第一 |
words | 英文 | 盒鬚圖五個統計量的名稱:min、q1、median、q3 和 max |
hidden | [] | 隱藏的資料序列名稱 |
locale | 'en-US' | 用於數字格式的 BCP 47 語言標籤 |
draw | 無 | 由自行繪製圖表的渲染器接手,呼叫時會傳入隱藏的資料序列名稱 |
#無須腳本
伺服器產生的標記已包含完整圖表,無須執行腳本。螢幕閱讀器仍可讀出各資料點的數值,而樣式表即可回應指標,顯示極細線與圓點,並淡化其他長條。腳本會加上數值提示、鍵盤操作和圖例切換功能。
以標記撰寫
<figure class="chart max-w(400) mx-auto" style="--chart-height: 10rem"> <div class="chart-body" style="--chart-gutter: 2"> <svg class="chart-plot" role="list" aria-label="Deployments per day, Monday to Friday"> <g aria-hidden="true"> <line class="chart-grid chart-grid-base" x1="0" y1="100%" x2="100%" y2="100%" /> <line class="chart-grid" x1="0" y1="75%" x2="100%" y2="75%" /> <line class="chart-grid" x1="0" y1="50%" x2="100%" y2="50%" /> <line class="chart-grid" x1="0" y1="25%" x2="100%" y2="25%" /> <line class="chart-grid" x1="0" y1="0%" x2="100%" y2="0%" /> <text class="chart-tick chart-tick-y" x="0" y="100%">0</text> <text class="chart-tick chart-tick-y" x="0" y="75%">20</text> <text class="chart-tick chart-tick-y" x="0" y="50%">40</text> <text class="chart-tick chart-tick-y" x="0" y="25%">60</text> <text class="chart-tick chart-tick-y" x="0" y="0%">80</text> <text class="chart-tick chart-tick-x" x="10%" y="100%">Mon</text> <text class="chart-tick chart-tick-x" x="30%" y="100%">Tue</text> <text class="chart-tick chart-tick-x" x="50%" y="100%">Wed</text> <text class="chart-tick chart-tick-x" x="70%" y="100%">Thu</text> <text class="chart-tick chart-tick-x" x="90%" y="100%">Fri</text> </g> <g class="chart-point" role="listitem" aria-label="Mon: 42"> <rect class="chart-hit" x="0%" y="0" width="20%" height="100%" /> <rect class="chart-bar" x="3.8%" y="47.5%" width="12.4%" height="52.5%" /> </g> <g class="chart-point" role="listitem" aria-label="Tue: 58"> <rect class="chart-hit" x="20%" y="0" width="20%" height="100%" /> <rect class="chart-bar" x="23.8%" y="27.5%" width="12.4%" height="72.5%" /> </g> <g class="chart-point" role="listitem" aria-label="Wed: 36"> <rect class="chart-hit" x="40%" y="0" width="20%" height="100%" /> <rect class="chart-bar" x="43.8%" y="55%" width="12.4%" height="45%" /> </g> <g class="chart-point" role="listitem" aria-label="Thu: 71"> <rect class="chart-hit" x="60%" y="0" width="20%" height="100%" /> <rect class="chart-bar" x="63.8%" y="11.25%" width="12.4%" height="88.75%" /> </g> <g class="chart-point" role="listitem" aria-label="Fri: 64"> <rect class="chart-hit" x="80%" y="0" width="20%" height="100%" /> <rect class="chart-bar" x="83.8%" y="20%" width="12.4%" height="80%" /> </g> </svg> </div></figure><figure className="chart max-w(400) mx-auto" style={{ '--chart-height': '10rem' } as React.CSSProperties}> <div className="chart-body" style={{ '--chart-gutter': '2' } as React.CSSProperties}> <svg className="chart-plot" role="list" aria-label="Deployments per day, Monday to Friday"> <g aria-hidden="true"> <line className="chart-grid chart-grid-base" x1="0" y1="100%" x2="100%" y2="100%" /> <line className="chart-grid" x1="0" y1="75%" x2="100%" y2="75%" /> <line className="chart-grid" x1="0" y1="50%" x2="100%" y2="50%" /> <line className="chart-grid" x1="0" y1="25%" x2="100%" y2="25%" /> <line className="chart-grid" x1="0" y1="0%" x2="100%" y2="0%" /> <text className="chart-tick chart-tick-y" x="0" y="100%">0</text> <text className="chart-tick chart-tick-y" x="0" y="75%">20</text> <text className="chart-tick chart-tick-y" x="0" y="50%">40</text> <text className="chart-tick chart-tick-y" x="0" y="25%">60</text> <text className="chart-tick chart-tick-y" x="0" y="0%">80</text> <text className="chart-tick chart-tick-x" x="10%" y="100%">Mon</text> <text className="chart-tick chart-tick-x" x="30%" y="100%">Tue</text> <text className="chart-tick chart-tick-x" x="50%" y="100%">Wed</text> <text className="chart-tick chart-tick-x" x="70%" y="100%">Thu</text> <text className="chart-tick chart-tick-x" x="90%" y="100%">Fri</text> </g> <g className="chart-point" role="listitem" aria-label="Mon: 42"> <rect className="chart-hit" x="0%" y="0" width="20%" height="100%" /> <rect className="chart-bar" x="3.8%" y="47.5%" width="12.4%" height="52.5%" /> </g> <g className="chart-point" role="listitem" aria-label="Tue: 58"> <rect className="chart-hit" x="20%" y="0" width="20%" height="100%" /> <rect className="chart-bar" x="23.8%" y="27.5%" width="12.4%" height="72.5%" /> </g> <g className="chart-point" role="listitem" aria-label="Wed: 36"> <rect className="chart-hit" x="40%" y="0" width="20%" height="100%" /> <rect className="chart-bar" x="43.8%" y="55%" width="12.4%" height="45%" /> </g> <g className="chart-point" role="listitem" aria-label="Thu: 71"> <rect className="chart-hit" x="60%" y="0" width="20%" height="100%" /> <rect className="chart-bar" x="63.8%" y="11.25%" width="12.4%" height="88.75%" /> </g> <g className="chart-point" role="listitem" aria-label="Fri: 64"> <rect className="chart-hit" x="80%" y="0" width="20%" height="100%" /> <rect className="chart-bar" x="83.8%" y="20%" width="12.4%" height="80%" /> </g> </svg> </div></figure><figure class="chart max-w(400) mx-auto" style="--chart-height: 10rem"> <div class="chart-body" style="--chart-gutter: 2"> <svg class="chart-plot" role="list" aria-label="Deployments per day, Monday to Friday"> <g aria-hidden="true"> <line class="chart-grid chart-grid-base" x1="0" y1="100%" x2="100%" y2="100%" /> <line class="chart-grid" x1="0" y1="75%" x2="100%" y2="75%" /> <line class="chart-grid" x1="0" y1="50%" x2="100%" y2="50%" /> <line class="chart-grid" x1="0" y1="25%" x2="100%" y2="25%" /> <line class="chart-grid" x1="0" y1="0%" x2="100%" y2="0%" /> <text class="chart-tick chart-tick-y" x="0" y="100%">0</text> <text class="chart-tick chart-tick-y" x="0" y="75%">20</text> <text class="chart-tick chart-tick-y" x="0" y="50%">40</text> <text class="chart-tick chart-tick-y" x="0" y="25%">60</text> <text class="chart-tick chart-tick-y" x="0" y="0%">80</text> <text class="chart-tick chart-tick-x" x="10%" y="100%">Mon</text> <text class="chart-tick chart-tick-x" x="30%" y="100%">Tue</text> <text class="chart-tick chart-tick-x" x="50%" y="100%">Wed</text> <text class="chart-tick chart-tick-x" x="70%" y="100%">Thu</text> <text class="chart-tick chart-tick-x" x="90%" y="100%">Fri</text> </g> <g class="chart-point" role="listitem" aria-label="Mon: 42"> <rect class="chart-hit" x="0%" y="0" width="20%" height="100%" /> <rect class="chart-bar" x="3.8%" y="47.5%" width="12.4%" height="52.5%" /> </g> <g class="chart-point" role="listitem" aria-label="Tue: 58"> <rect class="chart-hit" x="20%" y="0" width="20%" height="100%" /> <rect class="chart-bar" x="23.8%" y="27.5%" width="12.4%" height="72.5%" /> </g> <g class="chart-point" role="listitem" aria-label="Wed: 36"> <rect class="chart-hit" x="40%" y="0" width="20%" height="100%" /> <rect class="chart-bar" x="43.8%" y="55%" width="12.4%" height="45%" /> </g> <g class="chart-point" role="listitem" aria-label="Thu: 71"> <rect class="chart-hit" x="60%" y="0" width="20%" height="100%" /> <rect class="chart-bar" x="63.8%" y="11.25%" width="12.4%" height="88.75%" /> </g> <g class="chart-point" role="listitem" aria-label="Fri: 64"> <rect class="chart-hit" x="80%" y="0" width="20%" height="100%" /> <rect class="chart-bar" x="83.8%" y="20%" width="12.4%" height="80%" /> </g> </svg> </div></figure><template> <figure class="chart max-w(400) mx-auto" style="--chart-height: 10rem"> <div class="chart-body" style="--chart-gutter: 2"> <svg class="chart-plot" role="list" aria-label="Deployments per day, Monday to Friday"> <g aria-hidden="true"> <line class="chart-grid chart-grid-base" x1="0" y1="100%" x2="100%" y2="100%" /> <line class="chart-grid" x1="0" y1="75%" x2="100%" y2="75%" /> <line class="chart-grid" x1="0" y1="50%" x2="100%" y2="50%" /> <line class="chart-grid" x1="0" y1="25%" x2="100%" y2="25%" /> <line class="chart-grid" x1="0" y1="0%" x2="100%" y2="0%" /> <text class="chart-tick chart-tick-y" x="0" y="100%">0</text> <text class="chart-tick chart-tick-y" x="0" y="75%">20</text> <text class="chart-tick chart-tick-y" x="0" y="50%">40</text> <text class="chart-tick chart-tick-y" x="0" y="25%">60</text> <text class="chart-tick chart-tick-y" x="0" y="0%">80</text> <text class="chart-tick chart-tick-x" x="10%" y="100%">Mon</text> <text class="chart-tick chart-tick-x" x="30%" y="100%">Tue</text> <text class="chart-tick chart-tick-x" x="50%" y="100%">Wed</text> <text class="chart-tick chart-tick-x" x="70%" y="100%">Thu</text> <text class="chart-tick chart-tick-x" x="90%" y="100%">Fri</text> </g> <g class="chart-point" role="listitem" aria-label="Mon: 42"> <rect class="chart-hit" x="0%" y="0" width="20%" height="100%" /> <rect class="chart-bar" x="3.8%" y="47.5%" width="12.4%" height="52.5%" /> </g> <g class="chart-point" role="listitem" aria-label="Tue: 58"> <rect class="chart-hit" x="20%" y="0" width="20%" height="100%" /> <rect class="chart-bar" x="23.8%" y="27.5%" width="12.4%" height="72.5%" /> </g> <g class="chart-point" role="listitem" aria-label="Wed: 36"> <rect class="chart-hit" x="40%" y="0" width="20%" height="100%" /> <rect class="chart-bar" x="43.8%" y="55%" width="12.4%" height="45%" /> </g> <g class="chart-point" role="listitem" aria-label="Thu: 71"> <rect class="chart-hit" x="60%" y="0" width="20%" height="100%" /> <rect class="chart-bar" x="63.8%" y="11.25%" width="12.4%" height="88.75%" /> </g> <g class="chart-point" role="listitem" aria-label="Fri: 64"> <rect class="chart-hit" x="80%" y="0" width="20%" height="100%" /> <rect class="chart-bar" x="83.8%" y="20%" width="12.4%" height="80%" /> </g> </svg> </div> </figure></template>#動態效果
圖形標記初次顯示時會逐步繪出。長條依序從基準線升起,折線從起點延伸,扇形與雷達圖的圖形從中心展開,環形量表則逐步填滿。數值提示會在資料點之間滑動,極細線、圓點和長條淡化等醒目效果則會立即跟上使用者的操作。啟用減少動態效果時,圖形會直接以完成狀態顯示,數值提示也會立即換位。
#瀏覽器功能
- 長條的圓角與升起效果以 SVG 圖形上的
clip-path實作。若瀏覽器不支援以 CSS 形狀裁切 SVG 圖形,長條會以直角和完整長度顯示。 - 區域的漸淡效果由路徑上的
mask-image實作。缺少此功能時,整塊區域會以均勻的淡色顯示。 - 圓點半徑使用 CSS 的
r屬性,由--chart-dot設定。缺少此功能時,圓點會保留標記中指定的半徑。 - 減少標籤數量由繪圖區主體的容器查詢控制。缺少容器查詢時,會保留所有標籤。
- 初次繪出的效果使用
@starting-style。缺少此功能時,圖形標記會直接以完成狀態顯示。
#圖表的適用範圍
圖表適合呈現數百個圖形標記,每個標記都是讀者可存取的元素。若要處理數千個資料點、串流數值、縮放、框選、地圖或 K 線圖,請使用在 Canvas 上繪製的圖表引擎。若只需要呈現少量資訊,也有更精簡的元件可用。用走勢圖在句子中呈現趨勢,用統計數值呈現單一數字,用量表或儀表呈現單一讀值,用組成長條在一根長條上呈現總量的組成,或用熱圖呈現許多小數值形成的模式。請參閱圖表指南選擇合適的元件。
#無障礙
- 為
label撰寫一句話,說明測量的內容和單位。這句話是繪圖區的名稱,而繪圖區會以清單呈現。 - 每個資料點都是以其數值命名的清單項目,例如「週四:東京 71、大阪 49」。無須腳本或表格,螢幕閱讀器就能像讀取清單一樣讀取圖表。極細線、刻度線和數值提示會對螢幕閱讀器隱藏,因為資料點已提供相同資訊。
- 數值提示方便讀者直接查看數值。將指標停在資料點上、讓資料點取得焦點或用手指觸碰時都會顯示,按 Esc 即可隱藏。
- 除了色彩,圖例也會標示資料序列的名稱,數值提示則會並列顯示名稱與數值。類別色不代表狀態。
- 資料點取得焦點時,焦點外框會沿著其帶狀區域、圓點、扇形或圓環的輪廓繪製。
- 在強制色彩模式下,前景色與極細線會採用系統色,焦點則使用系統的醒目提示色。各資料序列會保留指定色彩,請在圖例中標示其名稱。
- 無論使用哪種語言,刻度都從左側開始。名稱、數值與圖例遵循頁面的書寫方向。
#React、Svelte 與 Vue
chart 元件依 chartLayout() 產生圖表,因此伺服器首次送出的 HTML 就包含圖表。元件也會加上互動行為,以填入數值提示並回應按鍵。元件以上述選項作為 props,其值為隱藏的資料序列名稱。使用 npx master-ui add chart 將元件複製到專案。請參閱 React、Svelte 與 Vue 了解如何建置與使用元件。
若複製的元件只繪製折線圖、面積圖或長條圖,可將原本從 chart.js 的匯入改成圖表核心與 Cartesian 圖形。所需型別同樣從 chart-core.js 匯入,並在模組層級註冊圖形。
import { chartCore as chart, chartForms, chartLayout } from '@master/ui/behaviors/chart-core.js'import { chartCartesian } from '@master/ui/behaviors/chart-cartesian.js'chartForms(chartCartesian)匯入別名讓複製的元件沿用原本的行為掛載。伺服器用 chartLayout() 繪製,掛載後可用方向鍵在資料點間移動,並用 Escape 關閉讀值提示。
#類別
| 類別 | 說明 |
|---|---|
chart | 圖表本身:先是繪圖區主體,下方接著圖例 |
chart-quiet | 長條為淡色,標有 aria-current 的資料點為墨色搭配 chart 使用 |
chart-split | 圖例排成一欄名稱與數字,立在繪圖區旁;空間不足時移到繪圖區下方搭配 chart 使用 |
chart-body | 繪圖區所在的位置:數值提示以它為基準定位,--chart-gutter 則在旁邊為刻度數字保留空間 |
chart-plot | SVG 本身,寬度與所在空間相同,高度為 --chart-height。請為它設定 role="list",並提供說明呈現內容及單位的名稱。 |
chart-plot-radial | 圓餅圖、圓環圖、環形進度圖、旭日圖或華夫格圖皆採正方形,邊長為 --chart-size。搭配 chart-plot 使用 |
chart-plot-radar | 雷達圖:位於繪圖區中央的正方形,旁邊留有空間放置環繞它的名稱搭配 chart-plot 使用 |
chart-canvas | 巢狀 SVG 路徑可寫在兩種座標框內。一般圖表使用 100 × 100 的座標框,透過 preserveAspectRatio="none" 拉伸至整個繪圖區。放射狀圖表使用以 0 為中心且維持原比例的座標框。若某種圖表以路徑呈現資料點,這些路徑也放在該座標框內。 |
chart-grid | 刻度的細線,以百分比定位 |
chart-grid-base | 基線:比其他細線明顯一階搭配 chart-grid 使用 |
chart-tick | 刻度數字,或沿著刻度排列的名稱 |
chart-tick-y | 位於繪圖區旁,對齊所屬的細線搭配 chart-tick 使用 |
chart-tick-x | 位於繪圖區下方,以所在位置為中心搭配 chart-tick 使用 |
chart-tick-around | 環繞雷達圖:從繪圖區中央移到其正方形中 --chart-x 和 --chart-y 所指的位置搭配 chart-tick 使用 |
chart-tick-start | 從所在位置開始,而非以該位置為中心:用於從一側延伸到另一側的折線的第一個名稱搭配 chart-tick 使用 |
chart-tick-end | 結束於所在位置搭配 chart-tick 使用 |
chart-tick-scale | 雷達圖的刻度數字:字級較小,位於第一條輻線旁搭配 chart-tick 使用 |
chart-tick-minor | 繪圖區窄於 24rem 時會省略的名稱搭配 chart-tick 使用 |
chart-line | 以折線呈現的序列;無論畫布伸展多寬,線寬都是 --chart-stroke |
chart-area | 折線下方的面積:一層向下漸淡的同色淡色底 |
chart-area-stacked | 堆疊面積中的一層:會遮住下層的淡色搭配 chart-area 使用 |
chart-point | 單一刻度位置、扇形、圓環、矩形區塊、斜率圖的資料序列或圓點,以及其對應的標記。請設定 role="listitem" 並提供說明其數值的無障礙名稱。數值提示會使用此項目的 --chart-x、--chart-y 和 --chart-reach。 |
chart-hit | 指標的感應範圍大於圖形本身時所用的區域:刻度上一格的整個範圍、圓點周圍的空間,或雷達圖的楔形區域。焦點外框會畫在它上面。 |
chart-hit-line | 沿著線條加上一道較寬的透明筆畫,擴大指標可命中的範圍。搭配 chart-hit 使用 |
chart-cursor | 穿過資料點的細線,在指標移到該資料點或該資料點取得焦點時顯示 |
chart-bar | 以百分比定位的長條,遠離基線的一端為圓角 |
chart-bar-inner | 在堆疊中位於另一根長條之下:方角,並與下一根長條相隔 --chart-gap 的間隙搭配 chart-bar 使用 |
chart-bar-down | 小於零的數值:圓角位於下端;在橫向長條中,則位於起始端搭配 chart-bar 使用 |
chart-bar-across | 橫向延伸的長條,圓角位於遠端搭配 chart-bar 使用 |
chart-bar-floating | 長條橫跨兩個數值,兩端皆不以基線為起點,且兩端均為圓角。搭配 chart-bar 使用 |
chart-bar-bin | 直方圖中代表各組區間的長條彼此相接,只用一條與背景同色的細線分隔。搭配 chart-bar 使用 |
chart-bar-rise | 瀑布圖的上升階使用成功色。搭配 chart-bar 使用 |
chart-bar-fall | 瀑布圖的下降階使用危險色。搭配 chart-bar 使用 |
chart-dot | 折線圖、雷達圖或散佈圖的資料點,以所屬顏色的圓點呈現,外圍加上一圈與背景同色的描邊。 |
chart-dot-quiet | 只在指標移到所屬資料點或該資料點取得焦點時顯示搭配 chart-dot 使用 |
chart-dot-bubble | 氣泡以所屬顏色描邊,內部填上同色的淡色,大小由該氣泡上的 --chart-dot 決定。搭配 chart-dot 使用 |
chart-dot-outlier | 盒鬚圖中遠離其他數值的離群值,以小型空心標記呈現。搭配 chart-dot 使用 |
chart-value | 繪圖區上的文字標記,包括長條末端的數值、折線最後一個資料點旁的名稱,以及矩形區塊的名稱 |
chart-value-across | 位於橫向長條的末端之後搭配 chart-value 使用 |
chart-value-before | 位於繪圖區前方,延伸至繪圖區邊緣搭配 chart-value 使用 |
chart-value-tile | 從矩形區塊的起始角顯示區塊名稱搭配 chart-value 使用 |
chart-value-quiet | 在矩形區塊名稱下方顯示數值,降低視覺強調搭配 chart-value 使用 |
chart-slice | 圓餅圖或圓環圖的一個扇形 |
chart-track | 圓環的軌道:代表數值所對照的整體,使用所屬顏色的淡色 |
chart-arc | 圓環的數值,從頂端開始,兩端為圓頭。請為它設定 pathLength="1",讓它在首次出現時逐漸填滿。 |
chart-web | 雷達圖的網格:每個刻度標記各有一圈,每個名稱各有一條輻線 |
chart-shape | 雷達圖的序列:輪廓線加上同色的淡色底 |
chart-band | 折線數值的可能範圍,以介於上下限之間的帶狀區域呈現 |
chart-stem | 棒棒糖圖中從基準線連到圓點的線段,以及啞鈴圖中連接兩端的線段 |
chart-whisker | 從數值的可能下限延伸至上限,穿過長條末端或盒鬚圖的盒體 |
chart-box | 盒鬚圖的盒體,從第一四分位數延伸至第三四分位數 |
chart-median | 中位數以一條與背景同色的線穿過盒體來標示 |
chart-link | 瀑布圖中從一根長條末端連到下一根長條的細線 |
chart-knot | 斜率圖或排名變化圖的資料點,以長度為零的圓端路徑繪製,在畫布拉伸時仍保持圓形 |
chart-cell | 熱圖儲存格的色彩強度由 --chart-tone 決定 |
chart-unit | 華夫格圖的每一格代表整體的 1% |
chart-stage | 漏斗圖各階段的色調比前一階段淺一級 |
chart-tile | 樹狀矩形圖的矩形區塊採用淡色填滿,並在區塊內顯示名稱。 |
chart-center | 圓環圖中央或環形進度圖內側顯示的內容 |
chart-center-value | 中央的數字 |
chart-center-label | 該數字所計量的項目 |
chart-tip | 指標所在資料點旁的數值提示,在行為填入內容前保持隱藏 |
chart-tip-title | 資料點的名稱 |
chart-tip-row | 數值提示中對應單一資料序列的項目,會套用該序列的色彩類別 class。 |
chart-tip-key | 一道序列顏色的短線 |
chart-tip-name | 序列的名稱 |
chart-tip-value | 序列在該資料點的數值 |
chart-legend | 各序列的名稱,旁邊各有一個所屬顏色的標示 |
chart-key | 圖例中的一個項目,會套用該資料序列的色彩類別 class。透過 label 包住的核取方塊,可切換序列的顯示與隱藏。 |
chart-key-input | 項目的核取方塊:畫面上看不見,但仍在 Tab 順序中,序列顯示時為勾選狀態 |
chart-key-mark | 項目的標示:一個小方塊,對應長條、面積或扇形的外觀 |
chart-key-line | 一道短線,對應折線的外觀搭配 chart-key-mark 使用 |
chart-key-dot | 一個圓點,對應散佈圖資料點的外觀搭配 chart-key-mark 使用 |
chart-key-value | 名稱之後的數字:扇形的占比,或圓環的數值 |
#自訂屬性
| 屬性 | 預設值 | 說明 |
|---|---|---|
--chart-height | 14rem | 繪圖區的高度;雷達圖的正方形不會高過此值 |
--chart-size | 12rem | 圓餅圖、圓環圖、環形進度圖、旭日圖或華夫格圖的寬度 |
--chart-color | var(--color-surface-inverse) | 未指定色彩類別的標記所用的前景色 |
--chart-stroke | 2px | 折線及雷達圖輪廓線的寬度 |
--chart-dot | 4px | 圓點的半徑。氣泡則使用各自的半徑 |
--chart-bar-radius | .25rem | 長條圓角的半徑 |
--chart-gap | 2px | 堆疊中各長條之間的間隙 |
--chart-surface | var(--surface-color, var(--color-surface-base)) | 圖表背景色,用於混合圓點外圈的顏色與標記的淡色 |
--chart-gutter | 繪圖區旁的數字占幾位數的寬度;0 表示不保留空間設定於 chart-body | |
--chart-tail | 繪圖區後方的標籤寬度,以數字字元數為單位。適用於橫條末端的數值和斜率圖線段末端的名稱設定於 chart-body | |
--chart-x | 數值提示在繪圖區橫向上的位置,介於 0 到 1;資料點帶有自己的值,供行為轉交給數值提示,環繞雷達圖的名稱則用它表示自己在正方形中的位置設定於 chart-tip | |
--chart-y | 數值提示在繪圖區縱向上的位置,從頂端的 0 到 1設定於 chart-tip | |
--chart-reach | 資料點的圖形距離數值提示的位置多遠,以占繪圖區的比例表示,讓數值提示能避開這些圖形:指左右兩側的距離,在橫向長條中則為上下的距離設定於 chart-tip | |
--chart-tone | 方格、漏斗圖階段或外環扇形在混色時採用所屬顏色的比例,以百分比表示,預設為 100%。設定於 chart-cell | |
--chart-index | 資料點的順序,用來錯開各標記的繪製動畫。設定於 chart-point |
#相關內容
- 迷你趨勢線用一條細線呈現趨勢,小巧到能放進句子、表格列或統計數值元件。
- 統計數值呈現單一數值、它衡量的項目及其變動:低調的標籤、大字的數值和變化幅度。
- 儀表以圓弧的填滿程度呈現讀值。
- 占比分布以單一長條呈現整體的組成,各區段的寬度反映其占比。
- 熱圖用一格格深淺不同的色塊呈現大量較小的數值,數值越高,顏色越深。
- 分類色標示資料屬於哪個分類的具名色彩。