|
| 1 | +package com.himanshoe.charty.radar |
| 2 | + |
| 3 | +import android.graphics.Paint |
| 4 | +import android.graphics.PointF |
| 5 | +import androidx.compose.foundation.Canvas |
| 6 | +import androidx.compose.foundation.layout.size |
| 7 | +import androidx.compose.runtime.Composable |
| 8 | +import androidx.compose.runtime.getValue |
| 9 | +import androidx.compose.runtime.mutableStateOf |
| 10 | +import androidx.compose.runtime.remember |
| 11 | +import androidx.compose.ui.Modifier |
| 12 | +import androidx.compose.ui.draw.drawBehind |
| 13 | +import androidx.compose.ui.geometry.Offset |
| 14 | +import androidx.compose.ui.graphics.Brush |
| 15 | +import androidx.compose.ui.graphics.Color |
| 16 | +import androidx.compose.ui.graphics.Path |
| 17 | +import androidx.compose.ui.graphics.drawscope.DrawScope |
| 18 | +import androidx.compose.ui.graphics.drawscope.Stroke |
| 19 | +import androidx.compose.ui.graphics.nativeCanvas |
| 20 | +import androidx.compose.ui.graphics.toArgb |
| 21 | +import androidx.compose.ui.tooling.preview.Preview |
| 22 | +import androidx.compose.ui.unit.dp |
| 23 | +import androidx.compose.ui.util.fastForEach |
| 24 | +import androidx.compose.ui.util.fastForEachIndexed |
| 25 | +import com.himanshoe.charty.common.ChartDataCollection |
| 26 | +import com.himanshoe.charty.common.config.AxisConfig |
| 27 | +import com.himanshoe.charty.common.config.ChartDefaults |
| 28 | +import com.himanshoe.charty.common.maxYValue |
| 29 | +import com.himanshoe.charty.common.minYValue |
| 30 | +import com.himanshoe.charty.radar.config.RadarChartColors |
| 31 | +import com.himanshoe.charty.radar.config.RadarChartDefaults |
| 32 | +import com.himanshoe.charty.radar.config.RadarConfig |
| 33 | +import com.himanshoe.charty.radar.model.RadarData |
| 34 | +import kotlin.math.abs |
| 35 | +import kotlin.math.cos |
| 36 | +import kotlin.math.sin |
| 37 | + |
| 38 | +/** |
| 39 | + * A composable function that displays a radar chart. |
| 40 | + * |
| 41 | + * @param dataCollection The collection of chart data points. |
| 42 | + * @param modifier The modifier for the chart. |
| 43 | + * @param axisConfig The configuration for the chart's axes. |
| 44 | + * @param radarConfig The configuration for the polygon in the chart. |
| 45 | + * @param chartColors The colors used in the chart. |
| 46 | + * @param radiusScale The scale factor for the radius of the data points. |
| 47 | + */ |
| 48 | +@Composable |
| 49 | +fun RadarChart( |
| 50 | + dataCollection: ChartDataCollection, |
| 51 | + modifier: Modifier = Modifier, |
| 52 | + axisConfig: AxisConfig = ChartDefaults.axisConfigDefaults(), |
| 53 | + radarConfig: RadarConfig = RadarChartDefaults.defaultConfig(), |
| 54 | + chartColors: RadarChartColors = RadarChartDefaults.defaultColor(), |
| 55 | + radiusScale: Float = 0.02f, |
| 56 | +) { |
| 57 | + val maxYValue by remember { mutableStateOf(dataCollection.maxYValue()) } |
| 58 | + val minYValue by remember { mutableStateOf(dataCollection.minYValue()) } |
| 59 | + val diff = abs(maxYValue * 0.1F) |
| 60 | + val dataRange = (minYValue - diff)..(maxYValue + diff) |
| 61 | + val partitionAngle = (2 * Math.PI / dataCollection.data.size).toFloat() |
| 62 | + |
| 63 | + Canvas( |
| 64 | + modifier = modifier |
| 65 | + .drawBehind { |
| 66 | + drawRadarAxis( |
| 67 | + dataCollection = dataCollection, |
| 68 | + dataRange = dataRange, |
| 69 | + radius = size.minDimension / 3, |
| 70 | + center = PointF(size.width / 2, size.height / 2), |
| 71 | + axisConfig = axisConfig, |
| 72 | + ) |
| 73 | + } |
| 74 | + ) { |
| 75 | + val radius = size.minDimension / 3 |
| 76 | + val centerX = size.width / 2 |
| 77 | + val centerY = size.height / 2 |
| 78 | + |
| 79 | + val scaleFactor = radius / (dataRange.endInclusive - dataRange.start) |
| 80 | + |
| 81 | + val lineColor = Brush.linearGradient(chartColors.lineColor) |
| 82 | + val dotColor = Brush.linearGradient(chartColors.dotColor) |
| 83 | + |
| 84 | + val radarPolygonVertices = mutableListOf<PointF>() |
| 85 | + |
| 86 | + Path().apply { |
| 87 | + dataCollection.data.fastForEachIndexed { index, chartData -> |
| 88 | + val angle = index * partitionAngle |
| 89 | + val x = centerX + scaleFactor * (chartData.yValue - dataRange.start) * cos(angle) |
| 90 | + val y = centerY + scaleFactor * (chartData.yValue - dataRange.start) * sin(angle) |
| 91 | + if (index == 0) { |
| 92 | + moveTo(x, y) |
| 93 | + } else { |
| 94 | + lineTo(x, y) |
| 95 | + } |
| 96 | + |
| 97 | + radarPolygonVertices.add(PointF(x, y)) |
| 98 | + } |
| 99 | + close() |
| 100 | + |
| 101 | + drawPath( |
| 102 | + path = this, |
| 103 | + brush = lineColor, |
| 104 | + style = Stroke(width = radarConfig.strokeSize) |
| 105 | + ) |
| 106 | + if (radarConfig.fillPolygon) { |
| 107 | + drawPath( |
| 108 | + path = this, |
| 109 | + brush = Brush.linearGradient(chartColors.fillColor), |
| 110 | + ) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (radarConfig.hasDotMarker) { |
| 115 | + radarPolygonVertices.fastForEach { |
| 116 | + drawCircle( |
| 117 | + brush = dotColor, |
| 118 | + radius = radiusScale * size.width, |
| 119 | + center = Offset(it.x, it.y) |
| 120 | + ) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +private fun DrawScope.drawRadarAxis( |
| 127 | + dataCollection: ChartDataCollection, |
| 128 | + dataRange: ClosedFloatingPointRange<Float>, |
| 129 | + radius: Float, |
| 130 | + center: PointF, |
| 131 | + axisConfig: AxisConfig, |
| 132 | +) { |
| 133 | + val partitionAngle = (2 * Math.PI / dataCollection.data.size).toFloat() |
| 134 | + val axisPolygons = if (axisConfig.showGridLines) { |
| 135 | + listOf(Path(), Path(), Path(), Path()) |
| 136 | + } else { |
| 137 | + emptyList() |
| 138 | + } |
| 139 | + |
| 140 | + for (spokeIndex in 0 until dataCollection.data.size) { |
| 141 | + val angle = spokeIndex * partitionAngle |
| 142 | + |
| 143 | + if (axisConfig.showAxes) { |
| 144 | + drawLine( |
| 145 | + start = Offset(center.x, center.y), |
| 146 | + end = Offset(center.x + radius * cos(angle), center.y + radius * sin(angle)), |
| 147 | + color = axisConfig.axisColor, |
| 148 | + strokeWidth = axisConfig.axisStroke |
| 149 | + ) |
| 150 | + } |
| 151 | + |
| 152 | + drawContext.canvas.nativeCanvas.drawText( |
| 153 | + dataCollection.data[spokeIndex].xValue.toString(), |
| 154 | + center.x + (radius + 30F) * cos(angle), |
| 155 | + center.y + (radius + 30F) * sin(angle), |
| 156 | + Paint().apply { |
| 157 | + this.color = axisConfig.axisColor.toArgb() |
| 158 | + this.textSize = size.width / 30 |
| 159 | + this.textAlign = if (angle > Math.PI / 2 && angle < 3 * Math.PI / 2) { |
| 160 | + Paint.Align.RIGHT |
| 161 | + } else { |
| 162 | + Paint.Align.LEFT |
| 163 | + } |
| 164 | + } |
| 165 | + ) |
| 166 | + |
| 167 | + axisPolygons.fastForEachIndexed { i, path -> |
| 168 | + val scale = 1F - 0.25F * i |
| 169 | + if (spokeIndex == 0) { |
| 170 | + path.moveTo(center.x + scale * radius * cos(angle), center.y + scale * radius * sin(angle)) |
| 171 | + } else { |
| 172 | + path.lineTo(center.x + scale * radius * cos(angle), center.y + scale * radius * sin(angle)) |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + if (!axisConfig.showGridLines) return |
| 178 | + |
| 179 | + axisPolygons.fastForEach { |
| 180 | + it.close() |
| 181 | + drawPath( |
| 182 | + path = it, |
| 183 | + color = axisConfig.gridColor, |
| 184 | + style = Stroke(width = axisConfig.axisStroke) |
| 185 | + ) |
| 186 | + } |
| 187 | + |
| 188 | + if (!axisConfig.showGridLabel) return |
| 189 | + |
| 190 | + drawGridLabels( |
| 191 | + radius = radius, |
| 192 | + center = center, |
| 193 | + dataRange = dataRange, |
| 194 | + angle = (partitionAngle / 2), |
| 195 | + labelCount = axisPolygons.size, |
| 196 | + labelColor = axisConfig.gridColor |
| 197 | + ) |
| 198 | +} |
| 199 | + |
| 200 | +private fun DrawScope.drawGridLabels( |
| 201 | + radius: Float, |
| 202 | + center: PointF, |
| 203 | + dataRange: ClosedFloatingPointRange<Float>, |
| 204 | + angle: Float, |
| 205 | + labelCount: Int, |
| 206 | + labelColor: Color |
| 207 | +) { |
| 208 | + val reduceBy = 1F / labelCount |
| 209 | + for (i in 0 until labelCount) { |
| 210 | + val scale = 1F - reduceBy * i |
| 211 | + drawContext.canvas.nativeCanvas.drawText( |
| 212 | + (dataRange.start + (dataRange.endInclusive - dataRange.start) * scale).toString(), |
| 213 | + center.x + scale * radius * cos(angle), |
| 214 | + center.y + scale * radius * sin(angle), |
| 215 | + Paint().apply { |
| 216 | + this.color = labelColor.toArgb() |
| 217 | + this.textSize = size.width / 30 |
| 218 | + this.textAlign = Paint.Align.CENTER |
| 219 | + } |
| 220 | + ) |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +@Preview(showBackground = true) |
| 225 | +@Composable |
| 226 | +private fun RadarChartPreview() { |
| 227 | + RadarChart( |
| 228 | + dataCollection = ChartDataCollection( |
| 229 | + listOf( |
| 230 | + RadarData(30f, "AAAAAA"), |
| 231 | + RadarData(25f, "BBBBBB"), |
| 232 | + RadarData(20f, "CCCCCC"), |
| 233 | + RadarData(15f, "DDDDDD"), |
| 234 | + RadarData(10f, "EEEEEE"), |
| 235 | + ) |
| 236 | + ), |
| 237 | + modifier = Modifier.size(350.dp), |
| 238 | + axisConfig = ChartDefaults.axisConfigDefaults(), |
| 239 | + radarConfig = RadarChartDefaults.defaultConfig().copy( |
| 240 | + fillPolygon = true |
| 241 | + ) |
| 242 | + ) |
| 243 | +} |
0 commit comments