Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Feature to Display Scores Beside Polygon Points in RadarChart #120

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal fun DrawScope.drawPolygonShape(
scalarValue: Double,
center: Offset,
scalarSteps: Int
) {
): List<Offset> {
val polygonEndPoints =
getPolygonShapeEndPoints(polygon.values, radius, scalarValue, center, scalarSteps)
val path = Path().apply {
Expand All @@ -35,6 +35,8 @@ internal fun DrawScope.drawPolygonShape(
alpha = polygon.style.fillColorAlpha
)
}

return polygonEndPoints
}

private fun Path.drawPolygon(polygonCorners: List<Offset>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Offset>,
values: List<Double>,
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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,6 +38,7 @@ fun RadarChart(
scalarValue: Double,
scalarValuesStyle: TextStyle,
polygons: List<Polygon>,
showScores: Boolean = false,
modifier: Modifier = Modifier
) {

Expand All @@ -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(
Expand Down