Skip to content

feat: Ability to animate individual slices in PieChart #109

@mslalith

Description

@mslalith

Right now PieChart supports to animate either from beginning (top of circle) to end or no animation at all. Please support to add animations for individual slices which would animate from current value to new value.

Given the current API, I managed to achieve in below way. This is not efficient as there is only one way to provide values with current API. This thread can be used to further opinions/discussion.

val animatables by produceState<List<Animatable<Float, AnimationVector1D>>>(
    initialValue = emptyList()
) {
    snapshotFlow { state.visibleItems.toList() }
        .collectLatest { items ->
            val oldValue = value
            if (oldValue.size == items.size) {
                oldValue.zip(items).forEach { (anim, item) ->
                    launch { anim.animateTo(item.data.toFloat()) }
                }
            } else {
                value = buildList {
                    oldValue.zip(items).forEach { (anim, item) ->
                        add(anim)
                        launch { anim.animateTo(item.data.toFloat()) }
                    }
                    if (items.size > oldValue.size) {
                        items.takeLast(items.size - oldValue.size).forEach { add(Animatable(it.data.toFloat())) }
                    }
                }
            }
        }
}

 PieChart(
    values = animatables.map { it.value },
    ...
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions