Skip to content

Commit

Permalink
Fix remember state of bars
Browse files Browse the repository at this point in the history
  • Loading branch information
mfracx committed Aug 14, 2023
1 parent 8103f94 commit 470b5fb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion JetChart/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ afterEvaluate {
from components.release
groupId = 'io.jetchart'
artifactId = 'JetChart'
version = '1.3.3'
version = '1.3.4'
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions JetChart/src/main/java/io/jetchart/bar/BarChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ import io.jetchart.common.animation.fadeInAnimation
@Composable
fun BarChart(
modifier: Modifier = Modifier,
chars: Bars,
bars: Bars,
animation: AnimationSpec<Float> = fadeInAnimation(),
xAxisDrawer: XAxisDrawer = BarXAxisDrawer(),
yAxisDrawer: YAxisDrawer = BarYAxisWithValueDrawer(),
labelDrawer: BarLabelDrawer = SimpleBarLabelDrawer(),
valueDrawer: BarValueDrawer = SimpleBarValueDrawer(),
barHorizontalMargin: Dp = 3.dp
) {
val transitionAnimation = remember(chars.bars) { Animatable(initialValue = 0f) }
val rectangles = remember { mutableStateMapOf<Bar, Rect>() }
val transitionAnimation = remember(bars.bars) { Animatable(initialValue = 0f) }
val rectangles = remember(bars.bars) { mutableStateMapOf<Bar, Rect>() }
val barDrawer = SimpleBarDrawer()
LaunchedEffect(chars.bars) {
LaunchedEffect(bars.bars) {
transitionAnimation.animateTo(1f, animationSpec = animation)
}

Expand All @@ -60,13 +60,13 @@ fun BarChart(
val (xAxisArea, yAxisArea) = axisAreas(this, size, xAxisDrawer, yAxisDrawer, valueDrawer)
val barDrawableArea = barDrawableArea(xAxisArea)

yAxisDrawer.drawAxisLabels(this, canvas, yAxisArea, chars.minYValue, chars.maxYValue)
yAxisDrawer.drawAxisLabels(this, canvas, yAxisArea, bars.minYValue, bars.maxYValue)

yAxisDrawer.drawAxisLine(this, canvas, yAxisArea)

xAxisDrawer.drawAxisLine(this, canvas, xAxisArea)

chars.forEachWithArea(this, barDrawableArea, transitionAnimation.value, valueDrawer, barHorizontalMargin) { barArea, bar ->
bars.forEachWithArea(this, barDrawableArea, transitionAnimation.value, valueDrawer, barHorizontalMargin) { barArea, bar ->
barDrawer.drawBar(this, canvas, barArea, bar)
labelDrawer.draw(this, canvas, bar.label, barArea, xAxisArea)
valueDrawer.draw(this, canvas, bar.value, barArea, xAxisArea)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ allprojects {
2. Add the dependency
```groovy
dependencies {
implementation 'com.github.fracassi-marco:JetChart:1.3.3'
implementation 'com.github.fracassi-marco:JetChart:1.3.4'
}
```

Expand All @@ -63,7 +63,7 @@ You can:
fun BarChartComposable(text: MutableState<String>) {
val numberOfBars = 8
val width = numberOfBars * 80
BarChart(chars = Bars(
BarChart(bars = Bars(
bars = (1..numberOfBars).map {
Bar(label = "BAR$it", value = Random.nextFloat(), color = JetGreen) {
bar -> text.value = "You clicked on the bar ${bar.label}!"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/io/jetchart/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class MainActivity : ComponentActivity() {
fun BarChartComposable(text: MutableState<String>) {
val numberOfBars = 8
val width = numberOfBars * 80
BarChart(chars = Bars(
BarChart(bars = Bars(
bars = (1..numberOfBars).map {
Bar(label = "BAR$it", value = Random.nextFloat(), color = if(it % 2 == 0) JetGreen else Red) {
bar -> text.value = "You clicked on the bar ${bar.label}!"
Expand Down

0 comments on commit 470b5fb

Please sign in to comment.