Skip to content

Commit 09544bd

Browse files
authored
Merge pull request #59 from yml-org/bug/CM-972
[CM-972]: Fixed the crash combined in graph
2 parents 8c137d5 + b643616 commit 09544bd

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

CODEOWNERS

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Default code owner for this repo
22
* @preetham1316
3-
* @margin-ks
43
* @dkk009
5-
* @krishanjangir-yml
4+
* @kikoso
5+

YChartsLib/src/main/java/co/yml/charts/ui/combinedchart/CombinedChart.kt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import androidx.compose.ui.platform.LocalContext
2121
import androidx.compose.ui.platform.LocalDensity
2222
import androidx.compose.ui.semantics.contentDescription
2323
import androidx.compose.ui.semantics.semantics
24+
import androidx.compose.ui.unit.dp
2425
import co.yml.charts.axis.XAxis
2526
import co.yml.charts.axis.YAxis
2627
import co.yml.charts.chartcontainer.container.ScrollableCanvasContainer
@@ -94,18 +95,18 @@ fun CombinedChart(modifier: Modifier, combinedChartData: CombinedChartData) {
9495
val barPoints = barPlotData.groupBarList.flatMap { bar -> bar.barList.map { it } }
9596
val bgColor = MaterialTheme.colors.surface
9697
val xMin =
97-
minOf(linePoints.minOf { it.x }, (barPlotData.groupBarList.size).toFloat())
98+
minOf(if(linePoints.isEmpty()) 0.0f else linePoints.minOf { it.x }, (barPlotData.groupBarList.size).toFloat())
9899
val xMax =
99-
maxOf(linePoints.maxOf { it.x }, (barPlotData.groupBarList.size).toFloat())
100-
val yMin = minOf(linePoints.minOf { it.y }, barPoints.minOf { it.point.y })
101-
val yMax = maxOf(linePoints.maxOf { it.y }, barPoints.maxOf { it.point.y })
100+
maxOf(if(linePoints.isEmpty()) 0.0f else linePoints.maxOf { it.x }, (barPlotData.groupBarList.size).toFloat())
101+
val yMin = minOf(if(linePoints.isEmpty()) 0.0f else linePoints.minOf { it.y }, if(barPoints.isEmpty())0.0f else barPoints.minOf { it.point.y })
102+
val yMax = maxOf(if(linePoints.isEmpty()) 0.0f else linePoints.maxOf { it.y }, if(barPoints.isEmpty())0.0f else barPoints.maxOf { it.point.y })
102103
val requiredSteps =
103104
maxOf(
104-
linePlotData.lines.map { it.dataPoints.size - 1 }.maxOf { it },
105-
barPlotData.groupBarList.map { it.barList.size - 1 }.maxOf { it }
105+
if(linePlotData.lines.isEmpty()) 0 else linePlotData.lines.map { it.dataPoints.size - 1 }.maxOf { it },
106+
if(barPlotData.groupBarList.isEmpty()) 0 else barPlotData.groupBarList.size
106107
)
107108
val xAxisData = xAxisData.copy(
108-
axisStepSize = ((barPlotData.barStyle.barWidth * barPlotData.groupingSize) +
109+
axisStepSize = if(barPlotData.groupBarList.isEmpty()) 30.dp else((barPlotData.barStyle.barWidth * barPlotData.groupingSize) +
109110
barPlotData.barStyle.paddingBetweenBars),
110111
steps = requiredSteps,
111112
startDrawPadding = LocalDensity.current.run { columnWidth.toDp() },
@@ -153,7 +154,7 @@ fun CombinedChart(modifier: Modifier, combinedChartData: CombinedChartData) {
153154
},
154155
drawXAndYAxis = { scrollOffset, xZoom ->
155156
val axisPoints = mutableListOf<Point>()
156-
for (index in barPlotData.groupBarList.indices) {
157+
for (index in 0 until xMax.toInt()) {
157158
axisPoints.add(Point(index.toFloat(), 0f))
158159
}
159160
XAxis(
@@ -176,7 +177,7 @@ fun CombinedChart(modifier: Modifier, combinedChartData: CombinedChartData) {
176177
},
177178
scrollOffset = scrollOffset,
178179
zoomScale = xZoom,
179-
chartData = axisPoints
180+
chartData = if(barPoints.isEmpty()) linePoints else axisPoints
180181
)
181182
YAxis(
182183
modifier = Modifier
@@ -193,7 +194,7 @@ fun CombinedChart(modifier: Modifier, combinedChartData: CombinedChartData) {
193194
val yBottom = size.height - rowHeight
194195
val yOffset =
195196
((yBottom - yAxisData.axisTopPadding.toPx()) / maxElementInYAxis)
196-
xOffset =
197+
xOffset = if(barPoints.isEmpty())xAxisData.axisStepSize.toPx() * xZoom else
197198
((barPlotData.barStyle.barWidth.toPx() * barPlotData.groupingSize) +
198199
barPlotData.barStyle.paddingBetweenBars.toPx()) * xZoom
199200
val xLeft =
@@ -400,8 +401,8 @@ fun CombinedChart(modifier: Modifier, combinedChartData: CombinedChartData) {
400401
*/
401402
private fun getDataFromType(combinedPlotDataList: List<PlotData>, type: PlotType): PlotData? {
402403
return when (type) {
403-
is PlotType.Line -> combinedPlotDataList.filterIsInstance<LinePlotData>().firstOrNull() as PlotData
404-
is PlotType.Bar -> combinedPlotDataList.filterIsInstance<BarPlotData>().firstOrNull() as PlotData
404+
is PlotType.Line -> combinedPlotDataList.filterIsInstance<LinePlotData?>().firstOrNull()
405+
is PlotType.Bar -> combinedPlotDataList.filterIsInstance<BarPlotData?>().firstOrNull()
405406
else -> null // Handle if required in future.
406407
}
407408
}

0 commit comments

Comments
 (0)