Chart
A token-driven, lightweight, self-drawn chart: five types — bar / line / area / pie / donut. Zero third-party dependencies, drawn with Compose Canvas — nice ticks (1/2/5×10ⁿ), monotone cubic Bézier smoothing, grouped & stacked bars — algorithmically aligned function-by-function with the web lineage @lh-ui/share chart core (manually translated, no symbol imports).
Contract source:
lh-ui-core/specs/chart.json· Golden cases:cases/chart.json(12 cases, translated to JVM unit tests + Compose UI Test)
Types
Five types cover the most common mobile statistics: bar grouped columns, line line, area area, pie solid pie, donut ring. Pie/donut take the first visible series for slice ratios, with labels as slice names.
HTML simulated preview (Android / Compose look), not a real-device render
LhChart(type = LhChartType.bar, data = weekData)
LhChart(type = LhChartType.donut, data = shareData)Data Shape
data is a single structured parameter: LhChartData(labels, series); each series carries a name, a value list, and an optional custom color. Missing labels fall back to indices (1/2/3…); non-finite values become 0, short lists are padded with 0, overlong lists truncated — data cleansing is handled by pure functions as a fallback, runtime data is not protected by defaults.
val weekData = LhChartData(
labels = listOf("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"),
series = listOf(
LhChartSeries(name = "Visits", data = listOf(120f, 180f, 90f, 240f, 200f, 160f, 210f)),
// A custom color overrides the palette (default cycles color.chart.series1..6 by index)
LhChartSeries(name = "Conversions", data = listOf(80f, 120f, 150f, 110f, 170f, 130f, 140f)),
),
)Stacked & Rounded
With stack = true, bars/areas accumulate layer by layer from the zero baseline; stacked charts do not carry mixed positive/negative semantics — negative values count as 0 (use grouped bars for positive/negative comparison). round = true adds a chart.bar.radius corner to bar tops. smooth = true on line/area applies monotone cubic Bézier (Fritsch–Carlson, no overshoot).
LhChart(type = LhChartType.bar, data = weekData, stack = true, round = true)
LhChart(type = LhChartType.area, data = weekData, smooth = true)Legend & Tooltip
The legend renders below the plot by default; tapping an item toggles that series' visibility and triggers onLegendChange (payload is the full list of {name, hidden} states); hiding all series shows the empty state. Touch tooltip: tap a bar/data point/slice to show a "label · value" popup; tap the same element again to close. The hit radius floor is chart.hitRadius (16dp) to prevent mis-taps on sparse categories.
LhChart(
type = LhChartType.line,
data = weekData,
onLegendChange = { states -> states.forEach { println("${it.name}: ${it.hidden}") } },
)Empty & Loading
loading = true shows a loading placeholder (reuses the core "Loading…" string); empty labels or all-empty series show the empty state (emptyText falls back to i18n, "No data" by default). Neither state draws any graphics.
LhChart(data = LhChartData(), emptyText = "No visit data")
LhChart(data = weekData, loading = true)Theme
Series colors and grid color take the light/dark pairs of color.chart.*; dimension tokens (paddings, bar width, font sizes) are not overridden in dark mode. Switch on device via LhTheme(isDark = true).
LhTheme(isDark = true) { LhChart(type = LhChartType.bar, data = weekData) }API
Props
| Prop | Description | Type | Default |
|---|---|---|---|
modifier | Modifier | Modifier | Modifier |
type | Chart type | LhChartType | Line |
data | Chart data (labels + series) | LhChartData | LhChartData() |
height | Plot height | Dp | chart.height.base(200dp) |
smooth | Line/area smoothing (monotone Bézier) | Boolean | false |
stack | Stack bars/areas (negatives count as 0) | Boolean | false |
round | Rounded bar tops | Boolean | false |
legend | Show the bottom legend row | Boolean | true |
tooltip | Enable the touch tooltip popup | Boolean | true |
grid | Show y-axis grid & ticks (ignored for pie/donut) | Boolean | true |
animated | Play the enter animation | Boolean | true |
precision | Decimal digits of values (0-100, invalid falls back to 2) | Int | 2 |
innerRadius | Pie/donut inner radius ratio (donut defaults to token) | Float? | null |
loading | Loading (placeholder, no graphics) | Boolean | false |
emptyText | Empty-state text (i18n when unset) | String? | null |
ariaLabel | Accessibility summary (auto-generated when unset) | String? | null |
onLegendChange | Legend visibility change callback | ((List<LhChartLegendState>) -> Unit)? | null |
LhChartType
| Value | Description | Coordinates |
|---|---|---|
Bar | Grouped bar chart (multi-series side by side, stack available) | Cartesian |
Line | Line chart (smooth available) | Cartesian |
Area | Area chart (closed to the zero baseline) | Cartesian |
Pie | Solid pie (first visible series ratios) | Polar |
Donut | Ring (inner radius defaults to chart.donutInnerRatio) | Polar |
LhChartData / LhChartSeries
| Field | Description | Type |
|---|---|---|
labels | Category labels (falls back to indices) | List<String> |
series | Data series | List<LhChartSeries> |
LhChartSeries.name | Series name (legend & tooltip text) | String |
LhChartSeries.data | Values | List<Float> |
LhChartSeries.color | Custom color (overrides the palette) | Color? |
Token Reference
Tokens consumed by the chart (aligned entry-by-entry with the tokenRefs of specs/chart.json):
| Token | Usage |
|---|---|
chart.height.base | Default plot height |
chart.padding.{top,right,bottom,left} | Plot paddings (room for ticks & labels) |
chart.bar.bandRatio / chart.bar.gap | Band width ratio (60%) and gap between bars |
chart.bar.radius / chart.bar.minWidth | Bar top corner radius and minimum width |
chart.line.strokeWidth / chart.line.dotRadius | Line stroke width and data-point radius |
chart.hitRadius | Minimum hit radius for touch (16dp) |
chart.legend.{dotSize,gap,fontSize} | Legend dot, spacing and font size |
chart.axisFontSize | Axis tick and label font size |
chart.tooltip.{padding,borderRadius,fontSize} | Tooltip padding, corner radius and font size |
chart.donutInnerRatio / chart.paletteSize | Donut inner radius ratio (60%) and palette size |
color.chart.series1..series6 | Series palette (custom colors win, cyclic beyond 6) |
color.chart.grid / color.chart.axis | Grid line and axis label colors |
color.chart.tooltip{Bg,Border,Text} | Tooltip background/border/text colors |
color.chart.legendText / color.chart.emptyText | Legend and empty-state text colors |
motion.duration.base / motion.easing.easeInOut | Enter animation (X reveal / grow / sweep) |
Golden Cases
The 12 assertions of cases/chart.json and their local test counterparts:
| case id | Assertion | Local test |
|---|---|---|
default-type-is-line | Default line + first series uses series1 | LhChartDefaultsTest.default_type_is_line_and_uses_series1_token |
bar-includes-zero-baseline | Bar y-domain contains the zero baseline + nice ticks | LhChartDefaultsTest.bar_includes_zero_baseline_and_nice_ticks |
stack-negatives-count-as-zero | Stacked negatives count as 0 | LhChartDefaultsTest.stack_negatives_count_as_zero |
area-closes-to-zero-baseline | Area closes to the zero baseline | LhChartDefaultsTest.area_closes_to_zero_baseline |
donut-default-inner-radius | Donut inner radius uses the token | LhChartDefaultsTest.donut_default_inner_radius_uses_token |
palette-cycles-six-series-colors | Six-color cycle + custom color precedence | LhChartDefaultsTest.palette_cycles_six_series_colors |
legend-toggle-emits-onlegendchange | Legend toggle + event payload | LhChartTest.legend_click_toggles_series_and_emits_change(Compose UI Test) |
tooltip-shows-hit-value | Hit shows the value popup | LhChartTest.tooltip_shows_hit_value_on_tap |
empty-state-renders-empty-text | Empty-state text | LhChartTest.empty_data_renders_placeholder_text + all_hidden_series_renders_empty_placeholder |
loading-overrides-content | Loading placeholder, no graphics | LhChartTest.loading_renders_placeholder_and_keeps_legend_hidden |
precision-formats-values | Values formatted by precision | LhChartDefaultsTest.precision_formats_values |
dark-colors-come-from-dark-tokens | Dark mode uses dark chart tokens | LhChartDefaultsTest.dark_colors_come_from_dark_tokens |
./gradlew :components:chart:test # JVM part
./gradlew :components:chart:connectedAndroidTest # Interaction part (device required)
