On this page

Chart

Live examples of Chart, with the exact Vue file that renders each one.

Every example is a complete Vue file. Copy it into an app that has the registry installed, or open it in StackBlitz. The charts are plain SVG inside ChartContainer; a chart library works the same way.

Values over time

Two metric series with a legend. Hover the plot to see the values of one hour. The sentence under the chart gives the totals, so the tooltip is not the only way to read them.

Order sync runs per hour

08:00 to 15:00: 772 completed, 25 failed. Failed runs peaked at 9 at 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'

// Order sync runs per hour for one online store. Colours are the visualization.metric roles.
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: 'Completed', color: 'var(--bh-visualization-metric-completed)' },
  failed: { label: 'Failed', color: 'var(--bh-visualization-metric-failed)' },
} satisfies ChartConfig
const series = ['completed', 'failed'] as const
const ticks = [0, 40, 80, 120]
const max = 120

// The plot is measured, so strokes stay in CSS pixels at any width.
const plot = ref<HTMLElement | null>(null)
const { width, height } = useElementSize(plot)
const inset = { top: 8, right: 8, bottom: 20, left: 32 } // room for the axis labels
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))
}
// The tooltip stays inside the chart: it sits right of the point in the left half, left of it in the right half.
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)">Order sync runs per hour</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="Line chart of completed and failed order sync runs per hour, 08:00 to 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)">
      08:00 to 15:00: {{ totals.completed.toLocaleString() }} completed, {{ totals.failed }} failed. Failed runs peaked at 9 at 14:00.
    </p>
  </div>
</template>

Parts of a total

Job types as named bars, each with its count and share. Job-type colours are categories, not statuses.

Jobs today by type 2,480 in total
Sync history1,136 · 46%
Polling jobs742 · 30%
Queue status410 · 17%
Other jobs192 · 8%
ChartShare.vue
<script setup lang="ts">
import type { ChartConfig } from '@/components/ui/chart'
import { ChartContainer } from '@/components/ui/chart'

// Today's jobs by type, as parts of the total. Job types are categories, not statuses: every bar is named
// and shows its count, so colour is never the only cue (visualization.job-type roles).
const total = 2480
const config = {
  queue: { label: 'Queue status', color: 'var(--bh-visualization-job-type-queue-status)' },
  sync: { label: 'Sync history', color: 'var(--bh-visualization-job-type-sync-history)' },
  polling: { label: 'Polling jobs', color: 'var(--bh-visualization-job-type-polling-jobs)' },
  generic: { label: 'Other jobs', 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)">
      Jobs today by type <span class="[font:var(--bh-font-body-small)] text-(--bh-color-text-subtle)">{{ total.toLocaleString() }} in total</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() }} · {{ 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 beside a value

A KPI tile. The value and its change come first; the sparkline only shows the trend.

Order sync success rate
97.1%Up 1.9 points in 8 hours
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'

// A KPI tile: the value first, the sparkline only beside it (Data visualization page).
// Sparkline roles: sparkline-compact-height, sparkline-width, sparkline-point-radius at the last sample.
const rates = [95.2, 96.0, 95.6, 96.4, 96.1, 96.8, 96.5, 97.1]
const config = {
  rate: { label: 'Success rate', 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)">Order sync success rate</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)">Up 1.9 points in 8 hours</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>