# Chart

A lightweight chart component rendered with pure SVG and zero third-party dependencies. It covers bar, line, area, pie and donut forms — bar / area can also be stacked — with a nice-tick scale algorithm, hover tooltips, legend filtering and entrance animation. The fixed viewBox scales vectorially, and --lh-chart-* tokens follow dark mode and the five preset themes.

Chart
<template>
  <div class="demo-col">
    <div class="demo-row" style="gap: 24px; align-items: flex-start; flex-wrap: wrap;">
      <div style="flex: 1; min-width: 320px;">
        <p class="demo-note">Bar chart (click a legend item to hide its series)</p>
        <LhChart :labels="labels" :series="series" />
      </div>
      <div style="flex: 1; min-width: 320px;">
        <p class="demo-note">Stacked bar chart (stack)</p>
        <LhChart :labels="labels" :series="series" stack />
      </div>
    </div>
    <div class="demo-row" style="gap: 24px; align-items: flex-start; margin-top: 16px;">
      <div style="flex: 1; min-width: 320px;">
        <p class="demo-note">Smooth line chart</p>
        <LhChart type="line" :labels="labels" :series="series" smooth />
      </div>
      <div style="flex: 1; min-width: 320px;">
        <p class="demo-note">Stacked area chart (area + smooth)</p>
        <LhChart type="area" :labels="labels" :series="series" stack smooth />
      </div>
    </div>
    <div class="demo-row" style="gap: 24px; align-items: flex-start; margin-top: 16px;">
      <div style="flex: 1; min-width: 320px;">
        <p class="demo-note">Donut chart</p>
        <LhChart type="donut" :labels="pieLabels" :series="pieSeries" />
      </div>
      <div style="flex: 1; min-width: 320px;">
        <p class="demo-note">No data</p>
        <LhChart :labels="emptyLabels" :series="emptySeries" />
      </div>
    </div>
    <p class="demo-note">
      Pure SVG, zero dependencies: viewBox scaling, hover tooltips, --lh-chart-* tokens follow dark mode.
    </p>
  </div>
</template>

<script>
export default {
  data: () => ({
    labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
    series: [
      { name: 'Revenue', data: [12, 28, 35, 22, 48, 40] },
      { name: 'Cost', data: [8, 15, 20, 14, 25, 22] },
    ],
    pieLabels: ['Online', 'Store', 'Dealer', 'Other'],
    pieSeries: [{ name: 'Channel share', data: [30, 25, 20, 25] }],
    emptyLabels: [],
    emptySeries: [],
  }),
}
</script>

# Props

Prop Description Type Default
type Chart type 'bar' \| 'line' \| 'area' \| 'pie' \| 'donut' 'bar'
labels Category labels (x-axis names / slice names); falls back to 1/2/3… when missing string[] —
series Data series (pie / donut uses the first visible series) LhChartSeries[] []
width Logical canvas width (scales vectorially to the container) number 480
height Logical canvas height number 320
showGrid Whether to show grid lines boolean true
showAxis Whether to show axis tick labels boolean true
showLegend Whether to show the legend boolean true
smooth Whether the line / area is smoothed (monotone cubic bezier) boolean false
stack Whether bars / areas are stacked (layers cumulate from the zero baseline) boolean false
animated Whether to enable the entrance animation boolean true
emptyText Empty-state text string i18n message ("No data" by default)
ariaLabel Accessible name string i18n message ("Chart" by default)
sx sx shorthand layout props — —

LhChartSeries shape: { name: string, data: number[], color?: string } — a custom color overrides the palette (any CSS color value).

# Slots

Slot Description
empty Empty-state content (takes priority over emptyText)

# Design notes

  • Zero dependencies: no ECharts / Chart.js — runtime cost is the component itself.
  • No resize listeners: a fixed logical canvas plus width: 100% vector scaling, so no ResizeObserver / resize wiring.
  • Nice ticks: ticks are rounded to 1 / 2 / 5 × 10ⁿ (same algorithm as d3's nice()); bars always start from a zero baseline.
  • Theme-aware: palette, grid, axes and tooltip all use --lh-chart-* tokens, with zero adaptation for dark mode.

# Accessibility

The root carries role="img" + aria-label; a visually hidden data table exposes the raw values to screen readers; bars, points and slices are focusable and reveal the tooltip via keyboard; legend items are button + aria-pressed; entrance animations are skipped under prefers-reduced-motion.

Dernière mise à jour: 04/12/2026 00:49:57

Copyright © 2024-2026 FengMo WenXue