Skip to content

Commit 17a0e9c

Browse files
committed
fix: correct bucketing and windowed ranges for chart rendering
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 39c6061 commit 17a0e9c

2 files changed

Lines changed: 11 additions & 10 deletions

File tree

  • apps/flipcash
    • features/tokens/src/main/kotlin/com/flipcash/app/tokens/internal/components/marketcap
    • shared/tokens/src/main/kotlin/com/flipcash/app/tokens/data

apps/flipcash/features/tokens/src/main/kotlin/com/flipcash/app/tokens/internal/components/marketcap/MarketCapChart.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,23 @@ internal fun MarketCapChart(
8282
mutableStateOf<List<MarketCapPoint>>(emptyList())
8383
}
8484

85-
LaunchedEffect(data) {
85+
// Track the period that corresponds to the current historicalData so we
86+
// don't collapse stale data with a new period (which produces an
87+
// intermediate curve that causes Vico to animate from the bottom).
88+
var dataPeriod by remember { mutableStateOf(selectedPeriod) }
89+
90+
LaunchedEffect(data, selectedPeriod) {
8691
if (data.isNotEmpty()) {
8792
historicalData = data
93+
dataPeriod = selectedPeriod
8894
}
8995
}
9096

9197
val modelProducer = remember { CartesianChartModelProducer() }
9298

93-
val windowedData by remember(historicalData, selectedPeriod) {
99+
val windowedData by remember(historicalData, dataPeriod) {
94100
derivedStateOf {
95-
historicalData.collapse(selectedPeriod, currentValue = currentValue)
101+
historicalData.collapse(dataPeriod, currentValue = currentValue)
96102
}
97103
}
98104

apps/flipcash/shared/tokens/src/main/kotlin/com/flipcash/app/tokens/data/MarketCapPoint.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,8 @@ fun List<MarketCapPoint>.collapse(
2323
Period.All -> minOfOrNull { it.x } ?: now
2424
}
2525

26-
val filtered = filter { it.x >= startTime }.ifEmpty {
27-
// No points fall within the period window — use the closest point
28-
// so the chart still renders with targetPoints entries.
29-
val nearest = minByOrNull { kotlin.math.abs(it.x - startTime) }
30-
?: return emptyList()
31-
listOf(nearest)
32-
}
26+
val filtered = filter { it.x >= startTime }
27+
if (filtered.isEmpty()) return emptyList()
3328

3429
val duration = now - startTime
3530
val intervalMs = (duration / targetPoints).coerceAtLeast(1)

0 commit comments

Comments
 (0)