diff --git a/app/src/main/java/com/himanshoe/charty/Navigator.kt b/app/src/main/java/com/himanshoe/charty/Navigator.kt index 0d94bd3..528e57e 100644 --- a/app/src/main/java/com/himanshoe/charty/Navigator.kt +++ b/app/src/main/java/com/himanshoe/charty/Navigator.kt @@ -14,7 +14,6 @@ import com.himanshoe.charty.ui.HorizontalBarChartDemo import com.himanshoe.charty.ui.LineChartDemo import com.himanshoe.charty.ui.PieChartDemo import com.himanshoe.charty.ui.PointChartDemo -import com.himanshoe.charty.ui.pcolors @Composable fun RegisterNavigation( diff --git a/app/src/main/java/com/himanshoe/charty/ui/BarChart.kt b/app/src/main/java/com/himanshoe/charty/ui/BarChart.kt index 752bf99..db4f2aa 100644 --- a/app/src/main/java/com/himanshoe/charty/ui/BarChart.kt +++ b/app/src/main/java/com/himanshoe/charty/ui/BarChart.kt @@ -8,7 +8,6 @@ import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp @@ -28,13 +27,24 @@ fun BarChartDemo() { .height(220.dp) .padding(32.dp), onBarClick = {}, - colors = listOf(Color(0xFFFDC830), Color(0xFFF37335)), + colors = colors, barData = listOf( BarData(10F, 35F), BarData(20F, 25F), BarData(10F, 50F), BarData(60F, 10F), BarData(10F, 15F), + BarData(10F, 15F), + BarData(10F, 15F), + BarData(10F, 15F), + BarData(10F, 15F), + BarData(10F, 15F), + BarData(10F, 15F), + BarData(10F, 15F), + BarData(10F, 15F), + BarData(10F, 15F), + BarData(10F, 15F), + BarData(10F, 15F), BarData(50F, 100F), BarData(20F, 25F), ) @@ -58,7 +68,7 @@ fun BarChartDemo() { .height(220.dp) .padding(32.dp), onBarClick = {}, - color = Color(0xFFD2827A), + color = colors.first(), barData = listOf( BarData(10F, 35F), BarData(20F, 25F), diff --git a/app/src/main/java/com/himanshoe/charty/ui/CircleChart.kt b/app/src/main/java/com/himanshoe/charty/ui/CircleChart.kt index 92e2803..e2d05de 100644 --- a/app/src/main/java/com/himanshoe/charty/ui/CircleChart.kt +++ b/app/src/main/java/com/himanshoe/charty/ui/CircleChart.kt @@ -16,7 +16,7 @@ import androidx.compose.ui.unit.sp import com.himanshoe.charty.circle.CircleChart import com.himanshoe.charty.circle.model.CircleData -internal val colors = listOf(Color(0xFFFDC830), Color(0xFFF37335)) +internal val colors = listOf(Color(0xFF20A100), Color(0xFFABEDCD)) @Composable fun CircleChartDemo() { diff --git a/app/src/main/java/com/himanshoe/charty/ui/CombinedBarChart.kt b/app/src/main/java/com/himanshoe/charty/ui/CombinedBarChart.kt index de83dd6..827daac 100644 --- a/app/src/main/java/com/himanshoe/charty/ui/CombinedBarChart.kt +++ b/app/src/main/java/com/himanshoe/charty/ui/CombinedBarChart.kt @@ -28,7 +28,7 @@ fun CombinedBarChartDemo() { .height(300.dp) .padding(32.dp), onClick = {}, - barColors = listOf(Color(0xFFFDC830), Color(0xFFF37335)), + barColors = colors, combinedBarData = listOf( CombinedBarData(10F, 80F, 5F), CombinedBarData(10F, 45F, 40F), diff --git a/app/src/main/java/com/himanshoe/charty/ui/CurvedLineChart.kt b/app/src/main/java/com/himanshoe/charty/ui/CurvedLineChart.kt index 46af810..3169818 100644 --- a/app/src/main/java/com/himanshoe/charty/ui/CurvedLineChart.kt +++ b/app/src/main/java/com/himanshoe/charty/ui/CurvedLineChart.kt @@ -27,7 +27,8 @@ fun CurveLineChartDemo() { modifier = Modifier .fillMaxWidth() .height(height = 300.dp), - colors = colors, + chartColors = colors, + lineColors = pcolors, lineData = listOf( LineData(10F, 35F), LineData(20F, 25F), @@ -52,7 +53,8 @@ fun CurveLineChartDemo() { modifier = Modifier .fillMaxWidth() .height(height = 300.dp), - colors = colors, + chartColors = colors, + lineColors = pcolors, lineData = listOf( LineData(10F, 35F), LineData(20F, 25F), diff --git a/app/src/main/java/com/himanshoe/charty/ui/GroupedHorizontalChartDemo.kt b/app/src/main/java/com/himanshoe/charty/ui/GroupedHorizontalChartDemo.kt index 0752981..8322893 100644 --- a/app/src/main/java/com/himanshoe/charty/ui/GroupedHorizontalChartDemo.kt +++ b/app/src/main/java/com/himanshoe/charty/ui/GroupedHorizontalChartDemo.kt @@ -18,7 +18,7 @@ import com.himanshoe.charty.horizontalbar.config.StartDirection import com.himanshoe.charty.horizontalbar.model.GroupedHorizontalBarData import com.himanshoe.charty.horizontalbar.model.HorizontalBarData -internal val pcolors = listOf(Color(0xFFFDC830), Color(0xFFF37335), Color.LightGray) +internal val pcolors = listOf(Color(0xFF9DB39A), Color(0xFFADBA9A), Color(0xFFBEC196)) @Composable fun GroupedHorizontalBarChartDemo() { diff --git a/app/src/main/java/com/himanshoe/charty/ui/LineChartDemo.kt b/app/src/main/java/com/himanshoe/charty/ui/LineChartDemo.kt index 2be9d02..361b5cd 100644 --- a/app/src/main/java/com/himanshoe/charty/ui/LineChartDemo.kt +++ b/app/src/main/java/com/himanshoe/charty/ui/LineChartDemo.kt @@ -31,6 +31,24 @@ fun LineChartDemo() { lineData = listOf( LineData(10F, 35F), LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), + LineData(20F, 25F), LineData(10F, 50F), LineData(80F, 10F), LineData(10F, 15F), diff --git a/charty/src/main/java/com/himanshoe/charty/bar/BarChart.kt b/charty/src/main/java/com/himanshoe/charty/bar/BarChart.kt index a2d285c..7881910 100644 --- a/charty/src/main/java/com/himanshoe/charty/bar/BarChart.kt +++ b/charty/src/main/java/com/himanshoe/charty/bar/BarChart.kt @@ -24,7 +24,7 @@ import com.himanshoe.charty.bar.model.BarData import com.himanshoe.charty.bar.model.maxYValue import com.himanshoe.charty.common.axis.AxisConfig import com.himanshoe.charty.common.axis.AxisConfigDefaults -import com.himanshoe.charty.common.axis.xAxis +import com.himanshoe.charty.common.axis.yAxis import com.himanshoe.charty.common.dimens.ChartDimens import com.himanshoe.charty.common.dimens.ChartDimensDefaults @@ -72,7 +72,7 @@ fun BarChart( modifier = modifier .drawBehind { if (axisConfig.showAxis) { - xAxis(axisConfig, maxYValue) + yAxis(axisConfig, maxYValue) } } .padding(horizontal = barDimens.padding) @@ -100,7 +100,7 @@ fun BarChart( size = Size(barWidth.value, barHeight) ) // draw label - drawBarLabel(data, barWidth.value, barHeight, topLeft) + drawBarLabel(data, barWidth.value, barHeight, topLeft, barData.count()) } } } diff --git a/charty/src/main/java/com/himanshoe/charty/bar/GroupedBarChart.kt b/charty/src/main/java/com/himanshoe/charty/bar/GroupedBarChart.kt index 8a2869b..969e33a 100644 --- a/charty/src/main/java/com/himanshoe/charty/bar/GroupedBarChart.kt +++ b/charty/src/main/java/com/himanshoe/charty/bar/GroupedBarChart.kt @@ -25,7 +25,7 @@ import com.himanshoe.charty.bar.model.maxYValue import com.himanshoe.charty.bar.model.totalItems import com.himanshoe.charty.common.axis.AxisConfig import com.himanshoe.charty.common.axis.AxisConfigDefaults -import com.himanshoe.charty.common.axis.xAxis +import com.himanshoe.charty.common.axis.yAxis import com.himanshoe.charty.common.dimens.ChartDimens import com.himanshoe.charty.common.dimens.ChartDimensDefaults @@ -50,7 +50,7 @@ fun GroupedBarChart( modifier = modifier .drawBehind { if (axisConfig.showAxis) { - xAxis(axisConfig, maxYValue) + yAxis(axisConfig, maxYValue) } } .padding(horizontal = barDimens.padding) @@ -83,7 +83,7 @@ fun GroupedBarChart( size = Size(barWidth.value, barHeight) ) // draw label - drawBarLabel(data, barWidth.value, barHeight, topLeft) + drawBarLabel(data, barWidth.value, barHeight, topLeft, groupedBarData.count()) } } } diff --git a/charty/src/main/java/com/himanshoe/charty/bar/StackedBarChart.kt b/charty/src/main/java/com/himanshoe/charty/bar/StackedBarChart.kt new file mode 100644 index 0000000..2a1ca9f --- /dev/null +++ b/charty/src/main/java/com/himanshoe/charty/bar/StackedBarChart.kt @@ -0,0 +1,3 @@ +package com.himanshoe.charty.bar + +class StackedBarChart diff --git a/charty/src/main/java/com/himanshoe/charty/bar/common/calculations/BarCalculations.kt b/charty/src/main/java/com/himanshoe/charty/bar/common/calculations/BarCalculations.kt index ff01734..fe4e9a4 100644 --- a/charty/src/main/java/com/himanshoe/charty/bar/common/calculations/BarCalculations.kt +++ b/charty/src/main/java/com/himanshoe/charty/bar/common/calculations/BarCalculations.kt @@ -21,8 +21,8 @@ internal fun getTopRight( barWidth: MutableState, size: Size, barData: BarData, - yChunck: Float + yScaleFactor: Float ) = Offset( x = index.plus(1).times(barWidth.value.times(1.2F)), - y = size.height.minus(barData.yValue.times(yChunck)) + y = size.height.minus(barData.yValue.times(yScaleFactor)) ) diff --git a/charty/src/main/java/com/himanshoe/charty/bar/common/component/Labels.kt b/charty/src/main/java/com/himanshoe/charty/bar/common/component/Labels.kt index e85daaf..f24c5d2 100644 --- a/charty/src/main/java/com/himanshoe/charty/bar/common/component/Labels.kt +++ b/charty/src/main/java/com/himanshoe/charty/bar/common/component/Labels.kt @@ -11,8 +11,11 @@ internal fun DrawScope.drawBarLabel( data: BarData, barWidth: Float, barHeight: Float, - topLeft: Offset + topLeft: Offset, + count: Int ) { + val divisibleFactor = if (count > 10) count else 1 + val textSizeFactor = if (count > 10) 3 else 30 drawIntoCanvas { it.nativeCanvas.apply { drawText( @@ -20,7 +23,7 @@ internal fun DrawScope.drawBarLabel( topLeft.x.plus(barWidth.div(2)), topLeft.y.plus(barHeight.plus(barWidth.div(2))), Paint().apply { - textSize = size.width.div(30) + textSize = size.width.div(textSizeFactor).div(divisibleFactor) textAlign = Paint.Align.CENTER } ) diff --git a/charty/src/main/java/com/himanshoe/charty/bar/model/StackedBarData.kt b/charty/src/main/java/com/himanshoe/charty/bar/model/StackedBarData.kt new file mode 100644 index 0000000..a14dfac --- /dev/null +++ b/charty/src/main/java/com/himanshoe/charty/bar/model/StackedBarData.kt @@ -0,0 +1,3 @@ +package com.himanshoe.charty.bar.model + +data class StackedBarData(val xValue: Any, val yValue: List) diff --git a/charty/src/main/java/com/himanshoe/charty/combined/CombinedBarChart.kt b/charty/src/main/java/com/himanshoe/charty/combined/CombinedBarChart.kt index 90be249..597de45 100644 --- a/charty/src/main/java/com/himanshoe/charty/combined/CombinedBarChart.kt +++ b/charty/src/main/java/com/himanshoe/charty/combined/CombinedBarChart.kt @@ -28,7 +28,7 @@ import com.himanshoe.charty.combined.model.CombinedBarData import com.himanshoe.charty.combined.model.maxYValue import com.himanshoe.charty.common.axis.AxisConfig import com.himanshoe.charty.common.axis.AxisConfigDefaults -import com.himanshoe.charty.common.axis.xAxis +import com.himanshoe.charty.common.axis.yAxis import com.himanshoe.charty.common.dimens.ChartDimens import com.himanshoe.charty.common.dimens.ChartDimensDefaults @@ -53,7 +53,7 @@ fun CombinedBarChart( modifier = modifier .drawBehind { if (axisConfig.showAxis) { - xAxis(axisConfig, maxYValue) + yAxis(axisConfig, maxYValue) } } .padding(horizontal = chartDimens.padding) diff --git a/charty/src/main/java/com/himanshoe/charty/common/axis/Axis.kt b/charty/src/main/java/com/himanshoe/charty/common/axis/Axis.kt index 7196856..8707a2a 100644 --- a/charty/src/main/java/com/himanshoe/charty/common/axis/Axis.kt +++ b/charty/src/main/java/com/himanshoe/charty/common/axis/Axis.kt @@ -8,10 +8,10 @@ import androidx.compose.ui.graphics.drawscope.drawIntoCanvas import androidx.compose.ui.graphics.nativeCanvas import java.text.DecimalFormat -internal fun DrawScope.xAxis(axisConfig: AxisConfig, maxValue: Float) { +internal fun DrawScope.yAxis(axisConfig: AxisConfig, maxValue: Float) { val graphYAxisEndPoint = size.height.div(4) val pathEffect = PathEffect.dashPathEffect(floatArrayOf(40f, 20f), 0f) - val labelChunck = maxValue.div(4) + val labelScaleFactor = maxValue.div(4) repeat(5) { index -> val yAxisEndPoint = graphYAxisEndPoint.times(index) @@ -19,7 +19,7 @@ internal fun DrawScope.xAxis(axisConfig: AxisConfig, maxValue: Float) { drawIntoCanvas { it.nativeCanvas.apply { drawText( - getLabelText(labelChunck.times(4.minus(index))), + getLabelText(labelScaleFactor.times(4.minus(index))), 0F.minus(25), yAxisEndPoint.minus(10), Paint().apply { diff --git a/charty/src/main/java/com/himanshoe/charty/horizontalbar/GroupedHorizontalBarChart.kt b/charty/src/main/java/com/himanshoe/charty/horizontalbar/GroupedHorizontalBarChart.kt index d3e7b6c..80f7ad7 100644 --- a/charty/src/main/java/com/himanshoe/charty/horizontalbar/GroupedHorizontalBarChart.kt +++ b/charty/src/main/java/com/himanshoe/charty/horizontalbar/GroupedHorizontalBarChart.kt @@ -46,7 +46,6 @@ fun GroupedHorizontalBarChart( val maxXValue = maxXValueState.value val barHeight = remember { mutableStateOf(0F) } val totalItems: Int = groupedBarData.totalItems() - Canvas( modifier = modifier .drawBehind { diff --git a/charty/src/main/java/com/himanshoe/charty/line/CurveLineChart.kt b/charty/src/main/java/com/himanshoe/charty/line/CurveLineChart.kt index b0296e4..9655351 100644 --- a/charty/src/main/java/com/himanshoe/charty/line/CurveLineChart.kt +++ b/charty/src/main/java/com/himanshoe/charty/line/CurveLineChart.kt @@ -21,7 +21,7 @@ import androidx.compose.ui.graphics.asComposePath import androidx.compose.ui.graphics.drawscope.Stroke import com.himanshoe.charty.common.axis.AxisConfig import com.himanshoe.charty.common.axis.AxisConfigDefaults -import com.himanshoe.charty.common.axis.xAxis +import com.himanshoe.charty.common.axis.yAxis import com.himanshoe.charty.common.dimens.ChartDimens import com.himanshoe.charty.common.dimens.ChartDimensDefaults import com.himanshoe.charty.line.config.CurveLineConfig @@ -32,7 +32,8 @@ import com.himanshoe.charty.line.model.maxYValue @Composable fun CurveLineChart( lineData: List, - color: Color, + chartColor: Color, + lineColor: Color, modifier: Modifier = Modifier, chartDimens: ChartDimens = ChartDimensDefaults.chartDimesDefaults(), axisConfig: AxisConfig = AxisConfigDefaults.axisConfigDefaults(), @@ -41,7 +42,8 @@ fun CurveLineChart( CurveLineChart( modifier = modifier, lineData = lineData, - colors = listOf(color, color), + chartColors = listOf(chartColor, chartColor), + lineColors = listOf(lineColor, lineColor), chartDimens = chartDimens, axisConfig = axisConfig, curveLineConfig = curveLineConfig @@ -51,7 +53,50 @@ fun CurveLineChart( @Composable fun CurveLineChart( lineData: List, - colors: List, + chartColor: Color, + lineColor: List, + modifier: Modifier = Modifier, + chartDimens: ChartDimens = ChartDimensDefaults.chartDimesDefaults(), + axisConfig: AxisConfig = AxisConfigDefaults.axisConfigDefaults(), + curveLineConfig: CurveLineConfig = CurveLineConfigDefaults.curveLineConfigDefaults() +) { + CurveLineChart( + modifier = modifier, + lineData = lineData, + chartColors = listOf(chartColor, chartColor), + lineColors = lineColor, + chartDimens = chartDimens, + axisConfig = axisConfig, + curveLineConfig = curveLineConfig + ) +} + +@Composable +fun CurveLineChart( + lineData: List, + chartColors: List, + lineColor: Color, + modifier: Modifier = Modifier, + chartDimens: ChartDimens = ChartDimensDefaults.chartDimesDefaults(), + axisConfig: AxisConfig = AxisConfigDefaults.axisConfigDefaults(), + curveLineConfig: CurveLineConfig = CurveLineConfigDefaults.curveLineConfigDefaults() +) { + CurveLineChart( + modifier = modifier, + lineData = lineData, + chartColors = chartColors, + lineColors = listOf(lineColor, lineColor), + chartDimens = chartDimens, + axisConfig = axisConfig, + curveLineConfig = curveLineConfig + ) +} + +@Composable +fun CurveLineChart( + lineData: List, + chartColors: List, + lineColors: List, modifier: Modifier = Modifier, chartDimens: ChartDimens = ChartDimensDefaults.chartDimesDefaults(), axisConfig: AxisConfig = AxisConfigDefaults.axisConfigDefaults(), @@ -69,7 +114,7 @@ fun CurveLineChart( .padding(horizontal = chartDimens.padding) .drawBehind { if (axisConfig.showAxis) { - xAxis(axisConfig, maxYValue) + yAxis(axisConfig, maxYValue) } }, onDraw = { @@ -99,7 +144,8 @@ fun CurveLineChart( val xValues = offsetItems.map { it.x } val pointsPath = Path() offsetItems.forEachIndexed { index, offset -> - val canDrawCircle = curveLineConfig.hasDotMarker && index != 0 && index != offsetItems.size.minus(1) + val canDrawCircle = + curveLineConfig.hasDotMarker && index != 0 && index != offsetItems.size.minus(1) if (canDrawCircle) { drawCircle( color = curveLineConfig.dotColor, @@ -139,13 +185,15 @@ fun CurveLineChart( drawPath( path = backgroundPath, brush = Brush.verticalGradient( - colors = colors, + colors = chartColors, endY = size.height - yScaleFactor ), ) drawPath( path = pointsPath, - color = Color.Black, + brush = Brush.verticalGradient( + colors = lineColors, + ), style = Stroke( width = 5F, cap = StrokeCap.Round diff --git a/charty/src/main/java/com/himanshoe/charty/line/LineChart.kt b/charty/src/main/java/com/himanshoe/charty/line/LineChart.kt index ec1290a..3d8d892 100644 --- a/charty/src/main/java/com/himanshoe/charty/line/LineChart.kt +++ b/charty/src/main/java/com/himanshoe/charty/line/LineChart.kt @@ -21,7 +21,7 @@ import androidx.compose.ui.graphics.drawscope.drawIntoCanvas import androidx.compose.ui.graphics.nativeCanvas import com.himanshoe.charty.common.axis.AxisConfig import com.himanshoe.charty.common.axis.AxisConfigDefaults -import com.himanshoe.charty.common.axis.xAxis +import com.himanshoe.charty.common.axis.yAxis import com.himanshoe.charty.common.dimens.ChartDimens import com.himanshoe.charty.common.dimens.ChartDimensDefaults import com.himanshoe.charty.line.config.LineConfig @@ -65,7 +65,7 @@ fun LineChart( modifier = modifier .drawBehind { if (axisConfig.showAxis) { - xAxis(axisConfig, maxYValue) + yAxis(axisConfig, maxYValue) } } .padding(horizontal = chartDimens.padding) @@ -80,15 +80,11 @@ fun LineChart( moveTo(0f, size.height) } - val lastIndex = lineData.size.minus(1) lineData.forEachIndexed { index, data -> val centerOffset = dataToOffSet(index, lineBound.value, size, data, scaleFactor) - val drawnPath = path.lineTo(centerOffset.x, centerOffset.y) when (index) { - lastIndex -> drawnPath.also { - path.lineTo(size.width, size.height) - } - else -> drawnPath + 0 -> path.moveTo(centerOffset.x, centerOffset.y) + else -> path.lineTo(centerOffset.x, centerOffset.y) } if (lineConfig.hasDotMarker) { drawCircle( @@ -97,25 +93,21 @@ fun LineChart( brush = brush ) } - drawXLabel(data, centerOffset, radius) - } - val stroke = if (lineConfig.hasSmoothCurve) { - Stroke( - width = strokeWidth, - pathEffect = PathEffect.cornerPathEffect(strokeWidth) - ) - } else { - Stroke(width = strokeWidth) + drawXLabel(data, centerOffset, radius, lineData.count()) } + val pathEffect = + if (lineConfig.hasSmoothCurve) PathEffect.cornerPathEffect(strokeWidth) else null drawPath( path = path, brush = brush, - style = stroke, + style = Stroke(width = strokeWidth, pathEffect = pathEffect), ) } } -private fun DrawScope.drawXLabel(data: LineData, centerOffset: Offset, radius: Float) { +private fun DrawScope.drawXLabel(data: LineData, centerOffset: Offset, radius: Float, count: Int) { + val divisibleFactor = if (count > 10) count else 1 + val textSizeFactor = if (count > 10) 3 else 30 drawIntoCanvas { it.nativeCanvas.apply { drawText( @@ -123,7 +115,7 @@ private fun DrawScope.drawXLabel(data: LineData, centerOffset: Offset, radius: F centerOffset.x, size.height.plus(radius.times(4)), Paint().apply { - textSize = size.width.div(30) + textSize = size.width.div(textSizeFactor).div(divisibleFactor) textAlign = Paint.Align.CENTER } ) @@ -136,10 +128,10 @@ private fun dataToOffSet( bound: Float, size: Size, data: LineData, - yChunck: Float + yScaleFactor: Float ): Offset { val startX = index.times(bound.times(1.2F)) val endX = index.plus(1).times(bound.times(1.2F)) - val y = size.height.minus(data.yValue.times(yChunck)) + val y = size.height.minus(data.yValue.times(yScaleFactor)) return Offset(((startX.plus(endX)).div(2F)), y) } diff --git a/charty/src/main/java/com/himanshoe/charty/point/PointChart.kt b/charty/src/main/java/com/himanshoe/charty/point/PointChart.kt index 52b773a..e6b1890 100644 --- a/charty/src/main/java/com/himanshoe/charty/point/PointChart.kt +++ b/charty/src/main/java/com/himanshoe/charty/point/PointChart.kt @@ -20,7 +20,7 @@ import androidx.compose.ui.graphics.drawscope.drawIntoCanvas import androidx.compose.ui.graphics.nativeCanvas import com.himanshoe.charty.common.axis.AxisConfig import com.himanshoe.charty.common.axis.AxisConfigDefaults -import com.himanshoe.charty.common.axis.xAxis +import com.himanshoe.charty.common.axis.yAxis import com.himanshoe.charty.common.dimens.ChartDimens import com.himanshoe.charty.common.dimens.ChartDimensDefaults import com.himanshoe.charty.point.cofig.PointConfig @@ -46,18 +46,18 @@ fun PointChart( modifier = modifier .drawBehind { if (axisConfig.showAxis) { - xAxis(axisConfig, maxYValue) + yAxis(axisConfig, maxYValue) } } .padding(horizontal = chartDimens.padding) ) { pointBound.value = size.width.div(pointData.count().times(1.2F)) - val yChunck = size.height.div(maxYValue) + val yScaleFactor = size.height.div(maxYValue) val brush = Brush.linearGradient(colors) val radius = size.width.div(70) pointData.forEachIndexed { index, data -> - val centerOffset = dataToOffSet(index, pointBound.value, size, data, yChunck) + val centerOffset = dataToOffSet(index, pointBound.value, size, data, yScaleFactor) val style = when (pointConfig.pointType) { is PointType.Stroke -> Stroke(width = size.width.div(100)) else -> Fill @@ -70,12 +70,14 @@ fun PointChart( brush = brush ) // draw label - drawXLabel(data, centerOffset, radius) + drawXLabel(data, centerOffset, radius, pointData.count()) } } } -private fun DrawScope.drawXLabel(data: PointData, centerOffset: Offset, radius: Float) { +private fun DrawScope.drawXLabel(data: PointData, centerOffset: Offset, radius: Float, count: Int) { + val divisibleFactor = if (count > 10) count else 1 + val textSizeFactor = if (count > 10) 3 else 30 drawIntoCanvas { it.nativeCanvas.apply { drawText( @@ -83,7 +85,7 @@ private fun DrawScope.drawXLabel(data: PointData, centerOffset: Offset, radius: centerOffset.x, size.height.plus(radius.times(4)), Paint().apply { - textSize = size.width.div(30) + textSize = size.width.div(textSizeFactor).div(divisibleFactor) textAlign = Paint.Align.CENTER } ) @@ -115,10 +117,10 @@ private fun dataToOffSet( bound: Float, size: Size, data: PointData, - yChunck: Float + yScaleFactor: Float ): Offset { val startX = index.times(bound.times(1.2F)) val endX = index.plus(1).times(bound.times(1.2F)) - val y = size.height.minus(data.yValue.times(yChunck)) + val y = size.height.minus(data.yValue.times(yScaleFactor)) return Offset(((startX.plus(endX)).div(2F)), y) }