Skip to content

Commit

Permalink
Add null point to draw partial line
Browse files Browse the repository at this point in the history
  • Loading branch information
mfracx committed Aug 17, 2023
1 parent b09288c commit 3455e62
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 24 deletions.
2 changes: 1 addition & 1 deletion JetChart/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ afterEvaluate {
from components.release
groupId = 'io.jetchart'
artifactId = 'JetChart'
version = '1.3.5'
version = '1.3.6'
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion JetChart/src/main/java/io/jetchart/line/Line.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.jetchart.line
import io.jetchart.line.renderer.line.LineDrawer

data class Line(
val points: List<Point>,
val points: List<LinePoint>,
val padBy: Float = 20f,
val startAtZero: Boolean = false,
val lineDrawer: LineDrawer,
Expand Down
4 changes: 1 addition & 3 deletions JetChart/src/main/java/io/jetchart/line/LineChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ private fun DrawScope.drawLine(
lineChartData: Line,
chartDrawableArea: Rect
) {

// Draw the chart line.
lineDrawer.drawLine(
drawScope = this,
canvas = canvas,
Expand All @@ -160,7 +158,7 @@ private fun DrawScope.drawLine(
)
)

lineChartData.points.forEachIndexed { index, point ->
lineChartData.points.filterIsInstance<Point>().forEachIndexed { index, point ->
withProgress(
index = index,
lineChartData = lineChartData,
Expand Down
34 changes: 19 additions & 15 deletions JetChart/src/main/java/io/jetchart/line/LineChartUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ object LineChartUtils {
fun calculatePointLocation(
drawableArea: Rect,
lineChartData: Line,
point: Point,
point: LinePoint,
index: Int
): Offset {
val x = (index.toFloat() / (lineChartData.points.size - 1))
Expand Down Expand Up @@ -116,12 +116,15 @@ object LineChartUtils {
transitionProgress = transitionProgress,
lineChartData = lineChartData
) { progress ->
val pointLocation = calculatePointLocation(
drawableArea = drawableArea,
lineChartData = lineChartData,
point = point,
index = index
)
val pointLocation = when(point) {
is Point -> calculatePointLocation(
drawableArea = drawableArea,
lineChartData = lineChartData,
point = point,
index = index
)
is NullPoint -> Offset.Unspecified
}

if (pointLocation.isSpecified) {
if (index == 0) {
Expand Down Expand Up @@ -151,8 +154,6 @@ object LineChartUtils {
lineChartData: Line,
transitionProgress: Float
): Path = Path().apply {

// we start from the bottom left
moveTo(drawableArea.left, drawableArea.bottom)
var prevPointX : Float? = null
var prevPointLocation: Offset? = null
Expand All @@ -162,12 +163,15 @@ object LineChartUtils {
transitionProgress = transitionProgress,
lineChartData = lineChartData
) { progress ->
val pointLocation = calculatePointLocation(
drawableArea = drawableArea,
lineChartData = lineChartData,
point = point,
index = index
)
val pointLocation = when(point) {
is Point -> calculatePointLocation(
drawableArea = drawableArea,
lineChartData = lineChartData,
point = point,
index = index
)
is NullPoint -> Offset.Unspecified
}

if (pointLocation.isSpecified) {
if (index == 0) {
Expand Down
9 changes: 8 additions & 1 deletion JetChart/src/main/java/io/jetchart/line/Point.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
package io.jetchart.line

data class Point(val value: Float, val label: String)
sealed interface LinePoint {
val value: Float
val label: String
}
data class Point(override val value: Float, override val label: String): LinePoint
data class NullPoint(override val label: String): LinePoint {
override val value = 0f
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ allprojects {
2. Add the dependency
```groovy
dependencies {
implementation 'com.github.fracassi-marco:JetChart:1.3.5'
implementation 'com.github.fracassi-marco:JetChart:1.3.6'
}
```

Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/io/jetchart/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import io.jetchart.gauge.GaugeChart
import io.jetchart.gauge.NeedleDrawer
import io.jetchart.line.Line
import io.jetchart.line.LineChart
import io.jetchart.line.NullPoint
import io.jetchart.line.Point
import io.jetchart.line.renderer.line.GradientLineShader
import io.jetchart.line.renderer.line.SolidLineDrawer
Expand Down Expand Up @@ -109,7 +110,8 @@ fun BarChartComposable(text: MutableState<String>) {
fun LineChartComposable() {
LineChart(lines = listOf(
Line(points = points(10), lineDrawer = SolidLineDrawer(thickness = 8.dp, color = Blue)),
Line(points = points(15), lineDrawer = SolidLineDrawer(thickness = 8.dp, color = Red))),
Line(points = points(15), lineDrawer = SolidLineDrawer(thickness = 8.dp, color = Red)),
Line(points = points(6) + nullPoints(2), lineDrawer = SolidLineDrawer(thickness = 8.dp, color = Cyan))),
modifier = Modifier
.horizontalScroll(rememberScrollState())
.width(1000.dp)
Expand All @@ -123,8 +125,8 @@ fun LineChartComposable() {
)
}

@Composable
private fun points(count: Int) = (1..count).map { Point(Random.nextFloat(), "Point$it") }
private fun nullPoints(count: Int) = (1..count).map { NullPoint("Point$it") }

@Composable
fun PieChartComposable() {
Expand Down

0 comments on commit 3455e62

Please sign in to comment.