Skip to content

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.

9:41Chart types

HTML simulated preview (Android / Compose look), not a real-device render

kotlin
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.

kotlin
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).

kotlin
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.

kotlin
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.

kotlin
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).

kotlin
LhTheme(isDark = true) { LhChart(type = LhChartType.bar, data = weekData) }

API ​

Props ​

PropDescriptionTypeDefault
modifierModifierModifierModifier
typeChart typeLhChartTypeLine
dataChart data (labels + series)LhChartDataLhChartData()
heightPlot heightDpchart.height.base(200dp)
smoothLine/area smoothing (monotone Bézier)Booleanfalse
stackStack bars/areas (negatives count as 0)Booleanfalse
roundRounded bar topsBooleanfalse
legendShow the bottom legend rowBooleantrue
tooltipEnable the touch tooltip popupBooleantrue
gridShow y-axis grid & ticks (ignored for pie/donut)Booleantrue
animatedPlay the enter animationBooleantrue
precisionDecimal digits of values (0-100, invalid falls back to 2)Int2
innerRadiusPie/donut inner radius ratio (donut defaults to token)Float?null
loadingLoading (placeholder, no graphics)Booleanfalse
emptyTextEmpty-state text (i18n when unset)String?null
ariaLabelAccessibility summary (auto-generated when unset)String?null
onLegendChangeLegend visibility change callback((List<LhChartLegendState>) -> Unit)?null

LhChartType ​

ValueDescriptionCoordinates
BarGrouped bar chart (multi-series side by side, stack available)Cartesian
LineLine chart (smooth available)Cartesian
AreaArea chart (closed to the zero baseline)Cartesian
PieSolid pie (first visible series ratios)Polar
DonutRing (inner radius defaults to chart.donutInnerRatio)Polar

LhChartData / LhChartSeries ​

FieldDescriptionType
labelsCategory labels (falls back to indices)List<String>
seriesData seriesList<LhChartSeries>
LhChartSeries.nameSeries name (legend & tooltip text)String
LhChartSeries.dataValuesList<Float>
LhChartSeries.colorCustom color (overrides the palette)Color?

Token Reference ​

Tokens consumed by the chart (aligned entry-by-entry with the tokenRefs of specs/chart.json):

TokenUsage
chart.height.baseDefault plot height
chart.padding.{top,right,bottom,left}Plot paddings (room for ticks & labels)
chart.bar.bandRatio / chart.bar.gapBand width ratio (60%) and gap between bars
chart.bar.radius / chart.bar.minWidthBar top corner radius and minimum width
chart.line.strokeWidth / chart.line.dotRadiusLine stroke width and data-point radius
chart.hitRadiusMinimum hit radius for touch (16dp)
chart.legend.{dotSize,gap,fontSize}Legend dot, spacing and font size
chart.axisFontSizeAxis tick and label font size
chart.tooltip.{padding,borderRadius,fontSize}Tooltip padding, corner radius and font size
chart.donutInnerRatio / chart.paletteSizeDonut inner radius ratio (60%) and palette size
color.chart.series1..series6Series palette (custom colors win, cyclic beyond 6)
color.chart.grid / color.chart.axisGrid line and axis label colors
color.chart.tooltip{Bg,Border,Text}Tooltip background/border/text colors
color.chart.legendText / color.chart.emptyTextLegend and empty-state text colors
motion.duration.base / motion.easing.easeInOutEnter animation (X reveal / grow / sweep)

Golden Cases ​

The 12 assertions of cases/chart.json and their local test counterparts:

case idAssertionLocal test
default-type-is-lineDefault line + first series uses series1LhChartDefaultsTest.default_type_is_line_and_uses_series1_token
bar-includes-zero-baselineBar y-domain contains the zero baseline + nice ticksLhChartDefaultsTest.bar_includes_zero_baseline_and_nice_ticks
stack-negatives-count-as-zeroStacked negatives count as 0LhChartDefaultsTest.stack_negatives_count_as_zero
area-closes-to-zero-baselineArea closes to the zero baselineLhChartDefaultsTest.area_closes_to_zero_baseline
donut-default-inner-radiusDonut inner radius uses the tokenLhChartDefaultsTest.donut_default_inner_radius_uses_token
palette-cycles-six-series-colorsSix-color cycle + custom color precedenceLhChartDefaultsTest.palette_cycles_six_series_colors
legend-toggle-emits-onlegendchangeLegend toggle + event payloadLhChartTest.legend_click_toggles_series_and_emits_change(Compose UI Test)
tooltip-shows-hit-valueHit shows the value popupLhChartTest.tooltip_shows_hit_value_on_tap
empty-state-renders-empty-textEmpty-state textLhChartTest.empty_data_renders_placeholder_text + all_hidden_series_renders_empty_placeholder
loading-overrides-contentLoading placeholder, no graphicsLhChartTest.loading_renders_placeholder_and_keeps_legend_hidden
precision-formats-valuesValues formatted by precisionLhChartDefaultsTest.precision_formats_values
dark-colors-come-from-dark-tokensDark mode uses dark chart tokensLhChartDefaultsTest.dark_colors_come_from_dark_tokens
bash
./gradlew :components:chart:test                        # JVM part
./gradlew :components:chart:connectedAndroidTest        # Interaction part (device required)

lh-ui native bloodline · Android (Kotlin + Compose)