Charts
Chartsanimated

Histogram

A frequency histogram with a smooth distribution curve drawn over the bins.

Installation

caveui components are copy-paste Jetpack Compose built entirely on Material 3 — there's no caveui dependency to add. Make sure Material 3 is on your classpath (it ships with the Compose BOM), then copy the Usage snippet below into your project.

kotlin
// build.gradle.kts (module)
dependencies {
    implementation(platform("androidx.compose:compose-bom:2025.06.00"))
    implementation("androidx.compose.material3:material3")
}

Usage

kotlin
@Composable
fun Histogram(
    bins: List<Float> = listOf(0.15f, 0.35f, 0.62f, 0.9f, 0.78f, 0.5f, 0.3f, 0.16f),
    accent: Color = Color(0xFF8B5CF6),
    modifier: Modifier = Modifier,
) {
    val grow by animateFloatAsState(1f, tween(900, easing = EaseOutCubic), label = "g")
    val curve = remember { Animatable(0f) }
    LaunchedEffect(Unit) { curve.animateTo(1f, tween(1200, 300, EaseInOutCubic)) }
    Canvas(modifier.fillMaxWidth().height(124.dp)) {
        val bw = size.width / bins.size
        bins.forEachIndexed { i, v ->
            val h = size.height * v * grow
            drawRect(
                accent.copy(alpha = 0.35f),
                Offset(i * bw + 1.dp.toPx(), size.height - h),
                Size(bw - 2.dp.toPx(), h),
            )
        }
        val path = Path().apply {
            bins.forEachIndexed { i, v ->
                val x = i * bw + bw / 2
                val y = size.height * (1f - v)
                if (i == 0) moveTo(x, y) else lineTo(x, y)
            }
        }
        val m = PathMeasure().apply { setPath(path, false) }
        drawPath(
            Path().also { m.getSegment(0f, m.length * curve.value, it, true) },
            accent,
            style = Stroke(2.5.dp.toPx(), cap = StrokeCap.Round),
        )
    }
}