Skip to content

Commit

Permalink
Fixed. X-Labels/Removed End points for LineChart (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-manshu authored Aug 25, 2022
1 parent fb3f3df commit d8b263b
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 64 deletions.
1 change: 0 additions & 1 deletion app/src/main/java/com/himanshoe/charty/Navigator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
16 changes: 13 additions & 3 deletions app/src/main/java/com/himanshoe/charty/ui/BarChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
)
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/himanshoe/charty/ui/CircleChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/com/himanshoe/charty/ui/CurvedLineChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/himanshoe/charty/ui/LineChartDemo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions charty/src/main/java/com/himanshoe/charty/bar/BarChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -72,7 +72,7 @@ fun BarChart(
modifier = modifier
.drawBehind {
if (axisConfig.showAxis) {
xAxis(axisConfig, maxYValue)
yAxis(axisConfig, maxYValue)
}
}
.padding(horizontal = barDimens.padding)
Expand Down Expand Up @@ -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())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -50,7 +50,7 @@ fun GroupedBarChart(
modifier = modifier
.drawBehind {
if (axisConfig.showAxis) {
xAxis(axisConfig, maxYValue)
yAxis(axisConfig, maxYValue)
}
}
.padding(horizontal = barDimens.padding)
Expand Down Expand Up @@ -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())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.himanshoe.charty.bar

class StackedBarChart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ internal fun getTopRight(
barWidth: MutableState<Float>,
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))
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ 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(
data.xValue.toString(),
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
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.himanshoe.charty.bar.model

data class StackedBarData(val xValue: Any, val yValue: List<Float>)
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -53,7 +53,7 @@ fun CombinedBarChart(
modifier = modifier
.drawBehind {
if (axisConfig.showAxis) {
xAxis(axisConfig, maxYValue)
yAxis(axisConfig, maxYValue)
}
}
.padding(horizontal = chartDimens.padding)
Expand Down
6 changes: 3 additions & 3 deletions charty/src/main/java/com/himanshoe/charty/common/axis/Axis.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ 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)
if (axisConfig.showUnitLabels) {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ fun GroupedHorizontalBarChart(
val maxXValue = maxXValueState.value
val barHeight = remember { mutableStateOf(0F) }
val totalItems: Int = groupedBarData.totalItems()

Canvas(
modifier = modifier
.drawBehind {
Expand Down
64 changes: 56 additions & 8 deletions charty/src/main/java/com/himanshoe/charty/line/CurveLineChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,7 +32,8 @@ import com.himanshoe.charty.line.model.maxYValue
@Composable
fun CurveLineChart(
lineData: List<LineData>,
color: Color,
chartColor: Color,
lineColor: Color,
modifier: Modifier = Modifier,
chartDimens: ChartDimens = ChartDimensDefaults.chartDimesDefaults(),
axisConfig: AxisConfig = AxisConfigDefaults.axisConfigDefaults(),
Expand All @@ -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
Expand All @@ -51,7 +53,50 @@ fun CurveLineChart(
@Composable
fun CurveLineChart(
lineData: List<LineData>,
colors: List<Color>,
chartColor: Color,
lineColor: List<Color>,
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<LineData>,
chartColors: List<Color>,
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<LineData>,
chartColors: List<Color>,
lineColors: List<Color>,
modifier: Modifier = Modifier,
chartDimens: ChartDimens = ChartDimensDefaults.chartDimesDefaults(),
axisConfig: AxisConfig = AxisConfigDefaults.axisConfigDefaults(),
Expand All @@ -69,7 +114,7 @@ fun CurveLineChart(
.padding(horizontal = chartDimens.padding)
.drawBehind {
if (axisConfig.showAxis) {
xAxis(axisConfig, maxYValue)
yAxis(axisConfig, maxYValue)
}
},
onDraw = {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit d8b263b

Please sign in to comment.