Skip to content

Commit 46310f3

Browse files
committed
add some comments and rename computeChartMaxValue util file name, bump version
1 parent 5b5590f commit 46310f3

File tree

6 files changed

+43
-33
lines changed

6 files changed

+43
-33
lines changed

compose-charts/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mavenPublishing{
1515
coordinates(
1616
groupId = "io.github.ehsannarmani",
1717
artifactId = "compose-charts",
18-
version = "0.1.9"
18+
version = "0.1.10"
1919
)
2020
pom{
2121
name.set("Compose Charts")

compose-charts/src/commonMain/kotlin/ir/ehsannarmani/compose_charts/ColumnChart.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import ir.ehsannarmani.compose_charts.models.Bars
5151
import ir.ehsannarmani.compose_charts.models.DividerProperties
5252
import ir.ehsannarmani.compose_charts.models.GridProperties
5353
import ir.ehsannarmani.compose_charts.models.HorizontalIndicatorProperties
54-
import ir.ehsannarmani.compose_charts.models.IndicatorCount
5554
import ir.ehsannarmani.compose_charts.models.IndicatorPosition
5655
import ir.ehsannarmani.compose_charts.models.LabelHelperProperties
5756
import ir.ehsannarmani.compose_charts.models.LabelProperties
@@ -63,7 +62,7 @@ import ir.ehsannarmani.compose_charts.utils.ImplementRCAnimation
6362
import ir.ehsannarmani.compose_charts.utils.calculateOffset
6463
import ir.ehsannarmani.compose_charts.utils.checkRCMaxValue
6564
import ir.ehsannarmani.compose_charts.utils.checkRCMinValue
66-
import ir.ehsannarmani.compose_charts.utils.rememberComputedMaxValue
65+
import ir.ehsannarmani.compose_charts.utils.rememberComputedChartMaxValue
6766
import kotlinx.coroutines.delay
6867
import kotlinx.coroutines.launch
6968
import kotlin.math.absoluteValue
@@ -130,7 +129,7 @@ fun ColumnChart(
130129
Animatable(0f)
131130
}
132131

133-
val computedMaxValue = rememberComputedMaxValue(minValue, maxValue, indicatorProperties.count)
132+
val computedMaxValue = rememberComputedChartMaxValue(minValue, maxValue, indicatorProperties.count)
134133
val indicators = remember(minValue, computedMaxValue) {
135134
indicatorProperties.indicators.ifEmpty {
136135
split(

compose-charts/src/commonMain/kotlin/ir/ehsannarmani/compose_charts/LineChart.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import ir.ehsannarmani.compose_charts.models.PopupProperties
7171
import ir.ehsannarmani.compose_charts.models.ZeroLineProperties
7272
import ir.ehsannarmani.compose_charts.utils.HorizontalLabels
7373
import ir.ehsannarmani.compose_charts.utils.calculateOffset
74-
import ir.ehsannarmani.compose_charts.utils.rememberComputedMaxValue
74+
import ir.ehsannarmani.compose_charts.utils.rememberComputedChartMaxValue
7575
import kotlinx.coroutines.CoroutineScope
7676
import kotlinx.coroutines.Job
7777
import kotlinx.coroutines.delay
@@ -155,7 +155,7 @@ fun LineChart(
155155
mutableStateListOf<PathData>()
156156
}
157157

158-
val computedMaxValue = rememberComputedMaxValue(minValue, maxValue, indicatorProperties.count)
158+
val computedMaxValue = rememberComputedChartMaxValue(minValue, maxValue, indicatorProperties.count)
159159
val indicators = remember(indicatorProperties.indicators, minValue, maxValue) {
160160
indicatorProperties.indicators.ifEmpty {
161161
split(

compose-charts/src/commonMain/kotlin/ir/ehsannarmani/compose_charts/RowChart.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import ir.ehsannarmani.compose_charts.models.GridProperties
5555
import ir.ehsannarmani.compose_charts.models.IndicatorPosition
5656
import ir.ehsannarmani.compose_charts.models.LabelHelperProperties
5757
import ir.ehsannarmani.compose_charts.models.LabelProperties
58-
import ir.ehsannarmani.compose_charts.models.Line
5958
import ir.ehsannarmani.compose_charts.models.PopupProperties
6059
import ir.ehsannarmani.compose_charts.models.SelectedBar
6160
import ir.ehsannarmani.compose_charts.models.VerticalIndicatorProperties
@@ -65,7 +64,7 @@ import ir.ehsannarmani.compose_charts.utils.VerticalLabels
6564
import ir.ehsannarmani.compose_charts.utils.calculateOffset
6665
import ir.ehsannarmani.compose_charts.utils.checkRCMaxValue
6766
import ir.ehsannarmani.compose_charts.utils.checkRCMinValue
68-
import ir.ehsannarmani.compose_charts.utils.rememberComputedMaxValue
67+
import ir.ehsannarmani.compose_charts.utils.rememberComputedChartMaxValue
6968
import kotlinx.coroutines.delay
7069
import kotlinx.coroutines.launch
7170
import kotlin.math.absoluteValue
@@ -132,7 +131,7 @@ fun RowChart(
132131
Animatable(0f)
133132
}
134133

135-
val computedMaxValue = rememberComputedMaxValue(minValue, maxValue, indicatorProperties.count)
134+
val computedMaxValue = rememberComputedChartMaxValue(minValue, maxValue, indicatorProperties.count)
136135
val indicators = remember(minValue, computedMaxValue) {
137136
indicatorProperties.indicators.ifEmpty {
138137
split(
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ir.ehsannarmani.compose_charts.utils
2+
3+
import androidx.compose.runtime.Composable
4+
import androidx.compose.runtime.remember
5+
import ir.ehsannarmani.compose_charts.models.IndicatorCount
6+
7+
@Composable
8+
fun rememberComputedChartMaxValue(minValue: Double, maxValue: Double, count: IndicatorCount) : Double {
9+
return remember(minValue, maxValue, count) {
10+
computeChartMaxValue(minValue, maxValue, count)
11+
}
12+
}
13+
14+
internal fun computeChartMaxValue(minValue: Double, maxValue: Double, count: IndicatorCount): Double {
15+
return when (count) {
16+
is IndicatorCount.CountBased -> maxValue
17+
is IndicatorCount.StepBased -> {
18+
/**
19+
* Why we are doing this?
20+
* if maxValue-minValue not dividable for `stepBy` (grater than zero remainder),
21+
* the generated numbers for indicators will have inconsistency
22+
* for example: (minValue=0,maxValue=5,stepBy=2) the generated indicator numbers will be: (5,3,1,0)
23+
* we will have inconsistency between 0 and 1 compared to other numbers, so indicators will show incorrect data in the end
24+
* so we check, if maxValue-minValue is not dividable for stepBy, we add the remainder to maxValue to avoid this issue
25+
*/
26+
val span = maxValue - minValue
27+
val remainder = span % count.stepBy
28+
29+
if (remainder == 0.0) {
30+
maxValue
31+
} else {
32+
maxValue + (count.stepBy - remainder)
33+
}
34+
}
35+
}
36+
}

compose-charts/src/commonMain/kotlin/ir/ehsannarmani/compose_charts/utils/RememberComputedMaxValue.kt

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)