Skip to content

Commit 3a6d319

Browse files
use Int instead of slice for better compatibility
1 parent aba320e commit 3a6d319

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

YChartsLib/src/main/java/co/yml/charts/ui/piechart/charts/DonutPieChart.kt

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ fun DonutPieChart(
7070
progressSize.add(sweepAngles[x] + progressSize[x - 1])
7171
}
7272

73-
var activePie: PieChartData.Slice? by rememberSaveable {
74-
mutableStateOf(null)
73+
var activePie by rememberSaveable {
74+
mutableStateOf(-1)
7575
}
7676
val accessibilitySheetState =
7777
rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)
@@ -143,12 +143,11 @@ fun DonutPieChart(
143143
)
144144
progressSize.forEachIndexed { index, item ->
145145
if (clickedAngle <= item) {
146-
val selectedSlice = pieChartData.slices[index]
147-
activePie = if (activePie != selectedSlice)
148-
selectedSlice
146+
activePie = if (activePie != index)
147+
index
149148
else
150-
null
151-
onSliceClick(selectedSlice)
149+
-1
150+
onSliceClick(pieChartData.slices[index])
152151
return@detectTapGestures
153152
}
154153
}
@@ -176,31 +175,29 @@ fun DonutPieChart(
176175
padding = padding,
177176
isDonut = pieChartData.plotType == PlotType.Donut,
178177
strokeWidth = pieChartConfig.strokeWidth,
179-
isActive = activePie == pieChartData.slices[index],
178+
isActive = activePie == index,
180179
pieChartConfig = pieChartConfig
181180
)
182181
sAngle += arcProgress
183182
}
184183
when {
185-
activePie != null && pieChartConfig.labelVisible -> {
184+
activePie != -1 && pieChartConfig.labelVisible -> {
185+
val selectedSlice = pieChartData.slices[activePie]
186186
drawContext.canvas.nativeCanvas.apply {
187187
val fontSize = pieChartConfig.labelFontSize.toPx()
188188
var isValue = false
189189
val textToDraw = when (pieChartConfig.labelType) {
190190
PieChartConfig.LabelType.PERCENTAGE -> "${
191-
proportions[pieChartData.slices.indexOf(
192-
activePie
193-
)].roundToInt()
191+
proportions[activePie].roundToInt()
194192
}%"
195193
PieChartConfig.LabelType.VALUE -> {
196194
isValue = true
197-
// We have already checked that activePie is not null so !! is safe here
198-
activePie!!.value.toString()
195+
selectedSlice.value.toString()
199196
}
200197
}
201198
val labelColor = when (pieChartConfig.labelColorType) {
202199
PieChartConfig.LabelColorType.SPECIFIED_COLOR -> pieChartConfig.labelColor
203-
PieChartConfig.LabelColorType.SLICE_COLOR -> activePie!!.color
200+
PieChartConfig.LabelColorType.SLICE_COLOR -> selectedSlice.color
204201
}
205202
val shouldShowUnit = isValue && pieChartConfig.sumUnit.isNotEmpty()
206203
drawLabel(
@@ -214,7 +211,7 @@ fun DonutPieChart(
214211
)
215212
}
216213
}
217-
activePie == null && pieChartConfig.isSumVisible -> {
214+
activePie == -1 && pieChartConfig.isSumVisible -> {
218215
drawContext.canvas.nativeCanvas.apply {
219216
val fontSize = pieChartConfig.labelFontSize.toPx()
220217
val textToDraw = "$sumOfValues"

0 commit comments

Comments
 (0)