diff --git a/chart/src/commonMain/kotlin/com/aay/compose/radarChart/DrawPolygon.kt b/chart/src/commonMain/kotlin/com/aay/compose/radarChart/DrawPolygon.kt index ac3a87d..a6c775d 100644 --- a/chart/src/commonMain/kotlin/com/aay/compose/radarChart/DrawPolygon.kt +++ b/chart/src/commonMain/kotlin/com/aay/compose/radarChart/DrawPolygon.kt @@ -14,7 +14,7 @@ internal fun DrawScope.drawPolygonShape( scalarValue: Double, center: Offset, scalarSteps: Int -) { +): List { val polygonEndPoints = getPolygonShapeEndPoints(polygon.values, radius, scalarValue, center, scalarSteps) val path = Path().apply { @@ -35,6 +35,8 @@ internal fun DrawScope.drawPolygonShape( alpha = polygon.style.fillColorAlpha ) } + + return polygonEndPoints } private fun Path.drawPolygon(polygonCorners: List) { diff --git a/chart/src/commonMain/kotlin/com/aay/compose/radarChart/DrawScores.kt b/chart/src/commonMain/kotlin/com/aay/compose/radarChart/DrawScores.kt new file mode 100644 index 0000000..4d84259 --- /dev/null +++ b/chart/src/commonMain/kotlin/com/aay/compose/radarChart/DrawScores.kt @@ -0,0 +1,42 @@ +package com.aay.compose.radarChart + +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.graphics.drawscope.DrawScope +import androidx.compose.ui.text.AnnotatedString +import androidx.compose.ui.text.TextMeasurer +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.ExperimentalTextApi +import androidx.compose.ui.text.drawText + +@OptIn(ExperimentalTextApi::class) +internal fun DrawScope.drawScores( + points: List, + values: List, + textStyle: TextStyle, + textMeasurer: TextMeasurer, + unit: String +) { + val centerY = size.height / 2f + + points.forEachIndexed { index, point: Offset -> + val score = "${values[index]} $unit" + val textOffset = 15.toDp().toPx() + val isBottomHalf = point.y > centerY + val textLayoutResult = textMeasurer.measure( + text = AnnotatedString(score), + style = textStyle + ) + val textPosition = Offset( + x = point.x - textLayoutResult.size.width / 2f, + y = if (isBottomHalf) point.y + textOffset + else point.y - textLayoutResult.size.height - textOffset + ) + + drawText( + textMeasurer = textMeasurer, + text = score, + style = textStyle, + topLeft = textPosition + ) + } +} \ No newline at end of file diff --git a/chart/src/commonMain/kotlin/com/aay/compose/radarChart/RadarChart.kt b/chart/src/commonMain/kotlin/com/aay/compose/radarChart/RadarChart.kt index cb4a9b7..e13b323 100644 --- a/chart/src/commonMain/kotlin/com/aay/compose/radarChart/RadarChart.kt +++ b/chart/src/commonMain/kotlin/com/aay/compose/radarChart/RadarChart.kt @@ -22,6 +22,7 @@ import com.aay.compose.radarChart.model.Polygon * @param scalarValue Scalar value to determine the scale of the radar chart. * @param scalarValuesStyle TextStyle for configuring the appearance of scalar value labels. * @param polygons List of polygons to be displayed on the radar chart. + * @param showScores Flag to determine whether to display polygon scores (default is false). * @param modifier Modifier for configuring the layout and appearance of the radar chart. * * @see Polygon @@ -37,6 +38,7 @@ fun RadarChart( scalarValue: Double, scalarValuesStyle: TextStyle, polygons: List, + showScores: Boolean = false, modifier: Modifier = Modifier ) { @@ -55,15 +57,25 @@ fun RadarChart( drawRadarNet(netLinesStyle, radarChartConfig) - polygons.forEach { - drawPolygonShape( + polygons.forEach { polygon -> + val points = drawPolygonShape( this, - it, + polygon, radius, scalarValue, Offset(size.width / 2, size.height / 2), scalarSteps ) + + if (showScores) { + drawScores( + points, + polygon.values, + scalarValuesStyle.copy(fontSize = scalarValuesStyle.fontSize * 0.8f), + textMeasurer, + polygons[0].unit + ) + } } drawAxisData(