Mỗi ví dụ là một file Vue hoàn chỉnh. Chép vào app đã cài registry, hoặc mở trong StackBlitz. Các biểu đồ là SVG thường đặt trong ChartContainer; thư viện biểu đồ cũng dùng y như vậy.
Giá trị theo thời gian
Hai series số liệu với chú giải. Rê chuột lên vùng vẽ để xem giá trị của một giờ. Câu bên dưới biểu đồ ghi tổng số, nên tooltip không phải cách duy nhất để đọc số liệu.
Số lần đồng bộ đơn mỗi giờ
Từ 08:00 tới 15:00: 772 lần hoàn tất, 25 lần lỗi. Số lần lỗi cao nhất là 9, lúc 14:00.
ChartLine.vue
<script setup lang="ts">
import type { ChartConfig } from '@/components/ui/chart'
import { useElementSize } from '@vueuse/core'
import { computed, ref } from 'vue'
import { ChartContainer, ChartLegendContent, ChartTooltipContent } from '@/components/ui/chart'
// Số lần đồng bộ đơn mỗi giờ của một cửa hàng online. Màu là các role visualization.metric.
const data = [
{ hour: '08:00', completed: 64, failed: 1 },
{ hour: '09:00', completed: 92, failed: 2 },
{ hour: '10:00', completed: 118, failed: 3 },
{ hour: '11:00', completed: 104, failed: 2 },
{ hour: '12:00', completed: 86, failed: 1 },
{ hour: '13:00', completed: 97, failed: 4 },
{ hour: '14:00', completed: 110, failed: 9 },
{ hour: '15:00', completed: 101, failed: 3 },
]
const config = {
completed: { label: 'Hoàn tất', color: 'var(--bh-visualization-metric-completed)' },
failed: { label: 'Lỗi', color: 'var(--bh-visualization-metric-failed)' },
} satisfies ChartConfig
const series = ['completed', 'failed'] as const
const ticks = [0, 40, 80, 120]
const max = 120
// Đo kích thước vùng vẽ, để nét vẽ luôn tính bằng pixel CSS ở mọi độ rộng.
const plot = ref<HTMLElement | null>(null)
const { width, height } = useElementSize(plot)
const inset = { top: 8, right: 8, bottom: 20, left: 32 } // chỗ cho nhãn trục
const x = (i: number) => inset.left + (i * (width.value - inset.left - inset.right)) / (data.length - 1)
const y = (v: number) => inset.top + (1 - v / max) * (height.value - inset.top - inset.bottom)
const path = (key: (typeof series)[number]) => data.map((d, i) => `${i ? 'L' : 'M'}${x(i)},${y(d[key])}`).join(' ')
const active = ref<number | null>(null)
function onMove(event: PointerEvent) {
const box = (event.currentTarget as SVGElement).getBoundingClientRect()
const step = (width.value - inset.left - inset.right) / (data.length - 1)
const i = Math.round((event.clientX - box.left - inset.left) / step)
active.value = Math.min(data.length - 1, Math.max(0, i))
}
// Tooltip luôn nằm trong biểu đồ: ở nửa trái thì đặt bên phải điểm, ở nửa phải thì đặt bên trái.
const tooltipStyle = computed(() => {
if (active.value === null) return {}
const px = x(active.value)
return px < width.value / 2 ? { left: `${px + 12}px` } : { right: `${width.value - px + 12}px` }
})
const totals = computed(() => ({
completed: data.reduce((sum, d) => sum + d.completed, 0),
failed: data.reduce((sum, d) => sum + d.failed, 0),
}))
</script>
<template>
<div class="grid w-full max-w-xl gap-(--bh-space-100)">
<div class="[font:var(--bh-font-heading-xsmall)] text-(--bh-color-text)">Số lần đồng bộ đơn mỗi giờ</div>
<ChartContainer :config="config" cursor>
<div ref="plot" data-chart-plot class="relative">
<svg
v-if="width"
:width="width"
:height="height"
:viewBox="`0 0 ${width} ${height}`"
role="img"
aria-label="Biểu đồ đường số lần đồng bộ đơn hoàn tất và lỗi mỗi giờ, từ 08:00 tới 15:00."
@pointermove="onMove"
@pointerleave="active = null"
>
<g v-for="tick in ticks" :key="tick">
<line :x1="inset.left" :x2="width - inset.right" :y1="y(tick)" :y2="y(tick)" class="stroke-(--bh-color-border) [stroke-width:var(--bh-visualization-grid-width)]" />
<text :x="inset.left - 8" :y="y(tick)" text-anchor="end" dominant-baseline="middle" class="fill-(--bh-color-text-subtle)">{{ tick }}</text>
</g>
<text v-for="(d, i) in data" :key="d.hour" :x="x(i)" :y="height - 4" text-anchor="middle" class="fill-(--bh-color-text-subtle)">{{ d.hour }}</text>
<line v-if="active !== null" :x1="x(active)" :x2="x(active)" :y1="inset.top" :y2="height - inset.bottom" class="stroke-(--bh-color-border-bold) [stroke-width:var(--bh-visualization-grid-width)]" />
<g v-for="key in series" :key="key">
<path :d="path(key)" fill="none" class="[stroke-width:var(--bh-visualization-line-width)]" :style="{ stroke: `var(--color-${key})` }" />
<circle
v-for="(d, i) in data"
:key="d.hour"
:cx="x(i)"
:cy="y(d[key])"
:class="active === i ? '[r:var(--bh-visualization-hover-point-radius)]' : '[r:var(--bh-visualization-point-radius)]'"
:style="{ fill: `var(--color-${key})` }"
/>
</g>
</svg>
<div v-if="active !== null" class="pointer-events-none absolute top-0" :style="tooltipStyle">
<ChartTooltipContent :payload="{ completed: data[active].completed, failed: data[active].failed }" :config="config" :x="active" :label-formatter="(i) => data[Number(i)].hour" />
</div>
</div>
<ChartLegendContent />
</ChartContainer>
<p class="m-0 [font:var(--bh-font-body-small)] text-(--bh-color-text-subtle)">
Từ 08:00 tới 15:00: {{ totals.completed.toLocaleString('vi-VN') }} lần hoàn tất, {{ totals.failed }} lần lỗi. Số lần lỗi cao nhất là 9, lúc 14:00.
</p>
</div>
</template>Các phần của một tổng
Loại job là các thanh có tên, mỗi thanh kèm số lượng và tỷ lệ. Màu loại job là nhóm, không phải trạng thái.
Job hôm nay theo loại tổng 2.480
Lịch sử đồng bộ1.136 · 46%
Job polling742 · 30%
Trạng thái hàng đợi410 · 17%
Job khác192 · 8%
ChartShare.vue
<script setup lang="ts">
import type { ChartConfig } from '@/components/ui/chart'
import { ChartContainer } from '@/components/ui/chart'
// Job hôm nay theo loại, là các phần của tổng. Loại job là nhóm, không phải trạng thái: thanh nào cũng có tên
// và số lượng, nên màu không bao giờ là dấu hiệu duy nhất (role visualization.job-type).
const total = 2480
const config = {
queue: { label: 'Trạng thái hàng đợi', color: 'var(--bh-visualization-job-type-queue-status)' },
sync: { label: 'Lịch sử đồng bộ', color: 'var(--bh-visualization-job-type-sync-history)' },
polling: { label: 'Job polling', color: 'var(--bh-visualization-job-type-polling-jobs)' },
generic: { label: 'Job khác', color: 'var(--bh-visualization-job-type-generic-jobs)' },
} satisfies ChartConfig
const rows = [
{ key: 'sync', count: 1136 },
{ key: 'polling', count: 742 },
{ key: 'queue', count: 410 },
{ key: 'generic', count: 192 },
] as const
const share = (count: number) => Math.round((count / total) * 100)
</script>
<template>
<div class="grid w-full max-w-md gap-(--bh-space-150)">
<div class="[font:var(--bh-font-heading-xsmall)] text-(--bh-color-text)">
Job hôm nay theo loại <span class="[font:var(--bh-font-body-small)] text-(--bh-color-text-subtle)">tổng {{ total.toLocaleString('vi-VN') }}</span>
</div>
<ChartContainer :config="config">
<div class="grid gap-(--bh-space-150)">
<div v-for="row in rows" :key="row.key" class="grid gap-(--bh-space-050)">
<div class="flex justify-between gap-(--bh-space-200)">
<span class="text-(--bh-color-text)">{{ config[row.key].label }}</span>
<span class="tabular-nums">{{ row.count.toLocaleString('vi-VN') }} · {{ share(row.count) }}%</span>
</div>
<div class="h-(--bh-visualization-activity-track-height) overflow-hidden rounded-(--bh-radius-025) bg-(--bh-color-background-neutral)" aria-hidden="true">
<div class="h-full" :style="{ width: `${share(row.count)}%`, backgroundColor: `var(--color-${row.key})` }" />
</div>
</div>
</div>
</ChartContainer>
</div>
</template>Sparkline cạnh một giá trị
Một ô KPI. Giá trị và mức thay đổi đứng trước; sparkline chỉ cho thấy xu hướng.
Tỷ lệ đồng bộ đơn thành công
97,1%Tăng 1,9 điểm trong 8 giờ
ChartSparkline.vue
<script setup lang="ts">
import type { ChartConfig } from '@/components/ui/chart'
import { useElementSize } from '@vueuse/core'
import { ref } from 'vue'
import { ChartContainer } from '@/components/ui/chart'
// Ô KPI: giá trị đứng trước, sparkline chỉ đặt cạnh giá trị (trang Data visualization).
// Role của sparkline: sparkline-compact-height, sparkline-width, sparkline-point-radius ở điểm cuối.
const rates = [95.2, 96.0, 95.6, 96.4, 96.1, 96.8, 96.5, 97.1]
const config = {
rate: { label: 'Tỷ lệ thành công', color: 'var(--bh-visualization-metric-success-rate)' },
} satisfies ChartConfig
const plot = ref<HTMLElement | null>(null)
const { width, height } = useElementSize(plot)
const min = 94
const max = 98
const x = (i: number) => 3 + (i * (width.value - 6)) / (rates.length - 1)
const y = (v: number) => 3 + (1 - (v - min) / (max - min)) * (height.value - 6)
const line = () => rates.map((v, i) => `${i ? 'L' : 'M'}${x(i)},${y(v)}`).join(' ')
</script>
<template>
<div class="grid w-full max-w-64 gap-(--bh-space-100) rounded-(--bh-radius-card) border-(length:--bh-border-width-default) border-(--bh-color-border) bg-(--bh-color-surface) p-(--bh-space-card)">
<div class="[font:var(--bh-font-label-default)] text-(--bh-color-text-subtle)">Tỷ lệ đồng bộ đơn thành công</div>
<div class="flex items-end justify-between gap-(--bh-space-200)">
<div class="grid">
<span class="[font:var(--bh-font-heading-large)] tabular-nums text-(--bh-color-text)">97,1%</span>
<span class="[font:var(--bh-font-body-small)] text-(--bh-color-text-subtle)">Tăng 1,9 điểm trong 8 giờ</span>
</div>
<ChartContainer :config="config" class="w-24">
<div ref="plot" class="h-(--bh-visualization-sparkline-compact-height) w-full" aria-hidden="true">
<svg v-if="width" :width="width" :height="height" :viewBox="`0 0 ${width} ${height}`">
<path :d="line()" fill="none" class="[stroke-width:var(--bh-visualization-sparkline-width)]" :style="{ stroke: 'var(--color-rate)' }" />
<circle :cx="x(rates.length - 1)" :cy="y(rates[rates.length - 1])" class="[r:var(--bh-visualization-sparkline-point-radius)]" :style="{ fill: 'var(--color-rate)' }" />
</svg>
</div>
</ChartContainer>
</div>
</div>
</template>