From 65e2e1121391daaf563c1ba96c6a91439681a3a4 Mon Sep 17 00:00:00 2001 From: Ankur Jain Date: Sat, 3 Aug 2024 11:36:00 -0700 Subject: [PATCH] Fix the height of bar charts when a bar is focussed. --- .../dautovicharis/charts/internal/barchart/BarChart.kt | 7 +++---- .../charts/internal/barstackedchart/BarStackedChart.kt | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/charts/src/commonMain/kotlin/io/github/dautovicharis/charts/internal/barchart/BarChart.kt b/charts/src/commonMain/kotlin/io/github/dautovicharis/charts/internal/barchart/BarChart.kt index 066d911..3147297 100644 --- a/charts/src/commonMain/kotlin/io/github/dautovicharis/charts/internal/barchart/BarChart.kt +++ b/charts/src/commonMain/kotlin/io/github/dautovicharis/charts/internal/barchart/BarChart.kt @@ -101,20 +101,19 @@ private fun DrawScope.drawBars( val spacing = style.space.toPx() val barWidth = (size.width - spacing * (dataSize - 1)) / dataSize - val finalBarHeight = size.height * (abs(value) / (maxValue - minValue)) + val selectedBarScale = if (index == selectedIndex) MAX_SCALE else DEFAULT_SCALE + val finalBarHeight = size.height * selectedBarScale * (abs(value) / (maxValue - minValue)) val barHeight = lerp(0f, finalBarHeight.toFloat(), progress[index].value) val top = if (value >= 0) baselineY - barHeight else baselineY val left = (barWidth + spacing) * index - val selectedBarScale = if (index == selectedIndex) MAX_SCALE else DEFAULT_SCALE - drawRect( color = barColor, topLeft = Offset(x = left, y = top.toFloat()), size = Size( width = barWidth * selectedBarScale, - height = barHeight * selectedBarScale + height = barHeight ) ) } diff --git a/charts/src/commonMain/kotlin/io/github/dautovicharis/charts/internal/barstackedchart/BarStackedChart.kt b/charts/src/commonMain/kotlin/io/github/dautovicharis/charts/internal/barstackedchart/BarStackedChart.kt index 96cea33..b6f1e80 100644 --- a/charts/src/commonMain/kotlin/io/github/dautovicharis/charts/internal/barstackedchart/BarStackedChart.kt +++ b/charts/src/commonMain/kotlin/io/github/dautovicharis/charts/internal/barstackedchart/BarStackedChart.kt @@ -98,7 +98,7 @@ private fun DrawScope.drawBars( item.item.points.forEachIndexed { dataIndex, value -> val height = lerp( 0f, - (value.toFloat() / totalMaxValue.toFloat()) * size.height, + (value.toFloat() * selectedBarScale / totalMaxValue.toFloat()) * size.height, progress[index].value ) topOffset -= height @@ -108,7 +108,7 @@ private fun DrawScope.drawBars( topLeft = Offset(x = index * (barWidth + spacing), y = topOffset), size = Size( width = barWidth * selectedBarScale, - height = height * selectedBarScale + height = height ) ) }