From 0d4624e0efe08e897bc5a13dac84285b4b9d6481 Mon Sep 17 00:00:00 2001 From: kkaze Date: Sun, 26 Nov 2023 01:54:51 +0400 Subject: [PATCH 1/2] Fix conversion of degree to radian angle --- .../com/aay/compose/donutChart/component/PiePedigreeChart.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/chart/src/commonMain/kotlin/com/aay/compose/donutChart/component/PiePedigreeChart.kt b/chart/src/commonMain/kotlin/com/aay/compose/donutChart/component/PiePedigreeChart.kt index 812c79b..9cab499 100644 --- a/chart/src/commonMain/kotlin/com/aay/compose/donutChart/component/PiePedigreeChart.kt +++ b/chart/src/commonMain/kotlin/com/aay/compose/donutChart/component/PiePedigreeChart.kt @@ -17,6 +17,7 @@ import com.aay.compose.donutChart.model.ChartTypes import kotlin.math.cos import kotlin.math.roundToInt import kotlin.math.sin +import kotlin.math.PI @OptIn(ExperimentalTextApi::class) internal fun DrawScope.drawPedigreeChart( @@ -132,7 +133,7 @@ internal fun DrawScope.drawPedigreeChart( } private val Float.degreeToAngle - get() = (this * (22/7) / 180f) + get() = this * PI / 180f private fun calculateAngle(dataLength: Float, totalLength: Float, progress: Float): Float = -360F * dataLength * progress / totalLength From e24b82afaec50fd827b045ff95b312edd3576ff5 Mon Sep 17 00:00:00 2001 From: kkaze Date: Sat, 2 Dec 2023 00:19:03 +0400 Subject: [PATCH 2/2] Fix crashing and prevent allocating arc ui when chart data value is zero --- .../aay/compose/donutChart/component/PiePedigreeChart.kt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/chart/src/commonMain/kotlin/com/aay/compose/donutChart/component/PiePedigreeChart.kt b/chart/src/commonMain/kotlin/com/aay/compose/donutChart/component/PiePedigreeChart.kt index 9cab499..21a483a 100644 --- a/chart/src/commonMain/kotlin/com/aay/compose/donutChart/component/PiePedigreeChart.kt +++ b/chart/src/commonMain/kotlin/com/aay/compose/donutChart/component/PiePedigreeChart.kt @@ -35,8 +35,14 @@ internal fun DrawScope.drawPedigreeChart( val outerCircularRadius = (minValue / 2) + (arcWidth / 1.2f) var startArc = -90F var startArcWithoutAnimation = -90f + + // If totalSum equals zero, there's nothing to show + if (totalSum == 0f) return + pieValueWithRatio.forEachIndexed { index, _ -> + if (pieChartData[index].data == 0.0) return@forEachIndexed + val arcWithAnimation = calculateAngle( dataLength = pieChartData[index].data.toFloat(), totalLength = totalSum, progress = transitionProgress.value )