Skip to content

Commit

Permalink
Implemented GraphUtil.plotPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-0410 committed Dec 10, 2024
1 parent 0e703bd commit 57eeb81
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package com.geeksville.mesh.ui.components

import android.util.Log
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -47,7 +46,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.StrokeCap
Expand All @@ -67,6 +65,7 @@ import com.geeksville.mesh.ui.BatteryInfo
import com.geeksville.mesh.ui.components.CommonCharts.MS_PER_SEC
import com.geeksville.mesh.ui.components.CommonCharts.DATE_TIME_FORMAT
import com.geeksville.mesh.ui.theme.Orange
import com.geeksville.mesh.util.GraphUtil.plotPoint
import java.util.concurrent.TimeUnit

private val DEVICE_METRICS_COLORS = listOf(Color.Green, Color.Magenta, Color.Cyan)
Expand Down Expand Up @@ -197,62 +196,34 @@ private fun DeviceMetricsChart(
val height = size.height
val width = size.width
val dataPointRadius = 2.dp.toPx()
val strokePath = Path().apply {
for (i in telemetries.indices) {
val telemetry = telemetries[i]

/* x-value for all three */
val x1Ratio = (telemetry.time - oldest.time).toFloat() / timeDiff
val x1 = x1Ratio * width

/* Channel Utilization */
val chUtilRatio =
telemetry.deviceMetrics.channelUtilization / MAX_PERCENT_VALUE
val yChUtil = height - (chUtilRatio * height)
drawCircle(
color = DEVICE_METRICS_COLORS[Device.CH_UTIL.ordinal],
radius = dataPointRadius,
center = Offset(x1, yChUtil)
)

/* Air Utilization Transmit */
val airUtilRatio = telemetry.deviceMetrics.airUtilTx / MAX_PERCENT_VALUE
val yAirUtil = height - (airUtilRatio * height)
drawCircle(
color = DEVICE_METRICS_COLORS[Device.AIR_UTIL.ordinal],
radius = dataPointRadius,
center = Offset(x1, yAirUtil)
)

/* Battery line */
val nextTelemetry = telemetries.getOrNull(i + 1) ?: telemetries.last()
val y1Ratio = telemetry.deviceMetrics.batteryLevel / MAX_PERCENT_VALUE
val y1 = height - (y1Ratio * height)

val x2Ratio = (nextTelemetry.time - oldest.time).toFloat() / timeDiff
val x2 = x2Ratio * width

val y2Ratio = nextTelemetry.deviceMetrics.batteryLevel / MAX_PERCENT_VALUE
val y2 = height - (y2Ratio * height)

if (i == 0) {
moveTo(x1, y1)
}
for (i in telemetries.indices) {
val telemetry = telemetries[i]

/* x-value time */
val xRatio = (telemetry.time - oldest.time).toFloat() / timeDiff
val x = xRatio * width

/* Channel Utilization */
plotPoint(
drawContext = drawContext,
color = DEVICE_METRICS_COLORS[Device.CH_UTIL.ordinal],
radius = dataPointRadius,
x = x,
value = telemetry.deviceMetrics.channelUtilization,
divisor = MAX_PERCENT_VALUE
)

quadraticTo(x1, y1, (x1 + x2) / 2f, (y1 + y2) / 2f)
}
/* Air Utilization Transmit */
plotPoint(
drawContext = drawContext,
color = DEVICE_METRICS_COLORS[Device.AIR_UTIL.ordinal],
radius = dataPointRadius,
x = x,
value = telemetry.deviceMetrics.airUtilTx,
divisor = MAX_PERCENT_VALUE
)
}

/* Battery Line */
// drawPath(
// path = strokePath,
// color = DEVICE_METRICS_COLORS[Device.BATTERY.ordinal],
// style = Stroke(
// width = dataPointRadius,
// cap = StrokeCap.Round
// )
// )

// TODO this works to draw lines only according to it's time
// Can this be made into a function that could be reused for other graphs???
var index = 0
Expand All @@ -264,7 +235,7 @@ private fun DeviceMetricsChart(
val nextTelemetry = telemetries.getOrNull(index + 1) ?: telemetries.last()

/* Check to see if we have a significant time break between telemetries. */
if (nextTelemetry.time - telemetry.time > TimeUnit.HOURS.toSeconds(2)) {
if (nextTelemetry.time - telemetry.time > TimeUnit.HOURS.toSeconds(2)) { // TODO constant
timeBreak = true
index++
break
Expand Down
32 changes: 32 additions & 0 deletions app/src/main/java/com/geeksville/mesh/util/GraphUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.geeksville.mesh.util

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.DrawContext

object GraphUtil {

/**
* @param value Must be zero-scaled before passing.
* @param divisor The range for the data set.
*/
fun plotPoint(
drawContext: DrawContext,
color: Color,
radius: Float,
x: Float,
value: Float,
divisor: Float,
) {
val height = drawContext.size.height
val ratio = value / divisor
val y = height - (ratio * height)
drawContext.canvas.drawCircle(
center = Offset(x, y),
radius = radius,
paint = androidx.compose.ui.graphics.Paint().apply { this.color = color }
)
}

// TODO implement drawing line
}

0 comments on commit 57eeb81

Please sign in to comment.