Chart 統計圖
LhChart 是零第三方依賴的自研統計圖元件:折線、柱狀、面積、餅、環五種類型, 底層為框架無關的純 SVG 算繪引擎(@lh-ui/share/core/chart),序列色走 --lh-chart-* CSS 變數, 與明暗主題、ConfigProvider 元件級覆寫天然統一。體積約為 ECharts 按需方案的 6%。
基礎用法(折線圖)
折線圖 type=「line」:類目標籤 + 多系列
vue
<script setup lang="ts">
import { LhChart } from '@lh-ui/vue3'
</script>
<template>
<LhChart
type="line"
:data="{
labels: ['一月', '二月', '三月', '四月', '五月', '六月'],
series: [
{ name: '营收', data: [30, 60, 45, 80, 65, 95] },
{ name: '成本', data: [20, 40, 35, 50, 45, 60] },
],
}"
/>
</template>vue
<script>
export default {}
</script>
<template>
<LhChart
type="line"
:data="{
labels: ['一月', '二月', '三月', '四月', '五月', '六月'],
series: [
{ name: '营收', data: [30, 60, 45, 80, 65, 95] },
{ name: '成本', data: [20, 40, 35, 50, 45, 60] },
],
}"
/>
</template>柱狀圖與堆疊
柱狀圖 type=「bar」:stack 開啟堆疊、round 開啟圓角柱
vue
<script setup lang="ts">
import { LhChart } from '@lh-ui/vue3'
</script>
<template>
<LhChart
type="bar"
stack
round
:data="{
labels: ['Q1', 'Q2', 'Q3', 'Q4'],
series: [
{ name: '华东', data: [120, 150, 140, 180] },
{ name: '华北', data: [80, 95, 110, 130] },
],
}"
/>
</template>vue
<script>
export default {}
</script>
<template>
<LhChart
type="bar"
stack
round
:data="{
labels: ['Q1', 'Q2', 'Q3', 'Q4'],
series: [
{ name: '华东', data: [120, 150, 140, 180] },
{ name: '华北', data: [80, 95, 110, 130] },
],
}"
/>
</template>面積圖與平滑曲線
面積圖 type=「area」+ smooth:單調三次貝塞爾,防過衝
vue
<script setup lang="ts">
import { LhChart } from '@lh-ui/vue3'
</script>
<template>
<LhChart
type="area"
smooth
:data="{
labels: ['周一', '周二', '周三', '周四', '周五'],
series: [{ name: '活跃用户', data: [820, 932, 901, 1034, 1290] }],
}"
/>
</template>vue
<script>
export default {}
</script>
<template>
<LhChart
type="area"
smooth
:data="{
labels: ['周一', '周二', '周三', '周四', '周五'],
series: [{ name: '活跃用户', data: [820, 932, 901, 1034, 1290] }],
}"
/>
</template>餅圖與環圖
餅圖 / 環圖 type=「pie」「donut」:取首系列,扇區佔比可點圖例隱藏
vue
<script setup lang="ts">
import { LhChart } from '@lh-ui/vue3'
</script>
<template>
<LhChart
type="donut"
:data="{
labels: ['直接访问', '搜索引擎', '外部链接', '其他'],
series: [{ name: '访问来源', data: [335, 310, 234, 135] }],
}"
/>
</template>vue
<script>
export default {}
</script>
<template>
<LhChart
type="donut"
:data="{
labels: ['直接访问', '搜索引擎', '外部链接', '其他'],
series: [{ name: '访问来源', data: [335, 310, 234, 135] }],
}"
/>
</template>空態與載入
loading 載入遮罩;空資料渲染空態佔位
vue
<script setup lang="ts">
import { ref } from 'vue'
import { LhChart } from '@lh-ui/vue3'
const loading = ref(true)
const data = ref({ labels: [], series: [] })
</script>
<template>
<LhChart loading :data="data" />
</template>vue
<script>
export default {
data() {
return { loading: true, data: { labels: [], series: [] } }
},
}
</script>
<template>
<LhChart :loading="loading" :data="data" />
</template>API
Props
| 屬性 | 說明 | 類型 | 預設值 |
|---|---|---|---|
| type | 圖表類型 | 'line' | 'bar' | 'area' | 'pie' | 'donut' | 'line' |
| data | 圖表資料(類目標籤 + 系列;pie / donut 取首個系列) | LhChartData | — |
| height | 畫布高度 px | number | 320 |
| smooth | 折線 / 面積平滑(單調貝塞爾,防過衝) | boolean | false |
| stack | 堆疊(僅 bar / area;負值按 0 計入) | boolean | false |
| round | 柱圓角(僅 bar) | boolean | false |
| legend | 圖例(點擊可顯隱系列) | boolean | true |
| tooltip | 懸浮提示層(類目吸附) | boolean | true |
| grid | 網格線與座標軸標籤 | boolean | true |
| animated | 入場動畫(僅首次算繪) | boolean | true |
| precision | 數值小數位上限 | number | 2 |
| innerRadius | 餅圖內半徑比例(donut 預設 0.6) | number | 0 |
| loading | 載入遮罩 | boolean | false |
| ariaLabel | 無障礙可達名稱(預設由資料兜底) | string | — |
| themeVars | 元件級主題變數(鍵自動加 --lh- 前綴) | Record<string, string> | — |
| sx | sx 快速版面配置屬性 | LhSxProps | — |
Events
| 事件名 | 說明 | 回呼參數 |
|---|---|---|
| legend-change | 圖例點擊切換系列顯隱時觸發 | (legend: { name: string; hidden: boolean }[]) |
data 資料結構
ts
interface LhChartData {
/** 類目標籤;缺失時按索引兜底 */
labels?: string[]
/** 資料序列;pie / donut 取首個系列 */
series: { name: string; data: number[]; color?: string }[]
}無障礙
- 根節點
role="img"+aria-label(ariaLabel預設時由圖表類型 + 系列名兜底) - 內建視覺隱藏的原始資料表(
.lh-chart__sr-table),螢幕閱讀器可直接讀取全部數值 - 圖例項為原生
button+aria-pressed,鍵盤可操作 - 系統開啟「減弱動態效果」時自動跳過入場動畫(
prefers-reduced-motion)

