Chart
A lightweight chart component: five types (bar / line / area / pie / donut), pure SVG rendering with zero third-party dependencies. Series colors go through theme CSS variables, so light/dark and preset themes need no adaptation.
Comparison with mainstream solutions
| Solution | Characteristics | Our choice |
|---|---|---|
| ECharts | Feature-complete, mature ecosystem | Large bundle (still hundreds of KB minified), themes re-registered; not used |
| @ant-design/charts | Declarative React wrapper | Heavy G2 runtime, long dependency chain; not used |
| Recharts | Pure declarative React SVG | We adopt its "geometry as data" idea; we do not make every shape a React component — heavy reconciliation on large data. Our geometry is computed by pure functions and written directly to the SVG DOM |
| Chart.js | Good canvas performance | Imperative API, weak a11y, disconnected from the CSS theme system; not used |
| Element Plus / Naive / Arco | None ships charts | Charts are a blank spot in mainstream UI libraries; we treat it as a differentiator |
Our strengths: geometry / ticks / hit-testing are all pure functions in
@lh-ui/share/core/chart (shared and isomorphic across the Vue3 / Vue2 / mini-program builds;
the single test suite lives in the share repo):
- Zero-dependency pure SVG; the engine (types + logic + render) is 1659 lines; d3-style nice ticks (1/2/5×10ⁿ) and monotone cubic Bézier smoothing (Fritsch–Carlson, no overshoot)
- Series colors inline
var(--lh-chart-series-N)tokens: light/dark, the five preset themes and component-levelthemeVarscascade automatically through CSS; the engine never reads styles ResizeObserver+ rAF coalescing, redraw only on width change: tab switching and container show/hide adapt automatically, no manual resize- First-class a11y:
role="img"accessible name plus a visually hidden raw-data table;prefers-reduced-motiondisables animations automatically - Built-in empty and loading placeholders, with locale-driven (10 languages) reactive text
Basic usage
import { LhChart } from '@lh-ui/react'
export default function Demo() {
const data = {
labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
series: [
{ name: 'Visits', data: [120, 200, 150, 80, 170] },
{ name: 'Orders', data: [40, 65, 50, 30, 60] },
],
}
return <LhChart type="line" data={data} />
}
type / data / height / smooth / stack / round / legend / tooltip / grid
/ animated / precision / innerRadius / loading match lh-ui-plus's LhChart props;
the plus @legend-change event maps to the React onLegendChange callback prop.
data is an object reference in props and changes trigger a redraw. If the parent inlines a
fresh data literal on every render, stabilize it with useMemo to avoid recomputing the
chart on unrelated re-renders. Geometry runs in share-core pure functions; data updates fully
rebuild the SVG (cheap for small data, cheaper than diffing).
Pie and donut take the first visible series data, with labels as slice labels (a
single-series share chart); use bar / line / area for multi-series data.
The chart root carries role="img"; a missing ariaLabel falls back to "series names + chart
type". The raw data stays in the DOM as a visually hidden table so screen readers can read
values item by item. For complex numbers pass an explicit ariaLabel.
Chart Props
| Prop | Description | Type | Options | Default |
|---|---|---|---|---|
| type | Chart type | LhChartType | 'bar' | 'line' | 'area' | 'pie' | 'donut' | 'line' |
| data | Chart data (category labels + series; pie / donut use the first series) | LhChartData | — | — |
| height | Canvas height px | number | — | 320 |
| smooth | Line / area smoothing (monotone cubic Bézier, no overshoot) | boolean | — | false |
| stack | Stacked (bar / area only; negatives count as 0) | boolean | — | false |
| round | Rounded bar corners (bar only) | boolean | — | false |
| legend | Show legend (click to toggle a series) | boolean | — | true |
| tooltip | Show hover tooltip | boolean | — | true |
| grid | Show grid lines and axis labels | boolean | — | true |
| animated | Enter animation (first render only; data updates do not replay) | boolean | — | true |
| precision | Max decimal places for values | number | — | 2 |
| innerRadius | Pie inner radius ratio (donut defaults to 0.6, pie to 0, by engine fallback) | number | 0 - 0.95 | — |
| loading | Loading overlay | boolean | — | false |
| ariaLabel | Accessible name (falls back to data) | string | — | — |
| onLegendChange | Legend visibility change callback (uncontrolled; state lives in the engine) | (legend: LhChartLegendItem[]) => void | — | — |
| themeVars | Component-level theme variables (override ConfigProvider and globals) | Record<string, string> | — | — |
| sx | Layout shorthand | SxProps | — | — |
Styles
import '@lh-ui/react/theme.css'
import '@lh-ui/react/style/chart'
Series color tokens are --lh-chart-series-1 ~ -6, plus --lh-chart-grid-color /
--lh-chart-axis-color / --lh-chart-tooltip-bg / --lh-chart-bar-radius and others;
all can be overridden via themeVars.