Skip to content

Commit

Permalink
[android] Fix issues with sensor time series graph (project-chip#8933)
Browse files Browse the repository at this point in the history
1. Round values on the Y axis.
2. Make sure the viewport of the graph widget is correctly
   updated with each new sensor measumrent.
  • Loading branch information
Damian-Nordic authored and Nikita committed Sep 23, 2021
1 parent e229c0a commit 0cee9ec
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import kotlinx.android.synthetic.main.sensor_client_fragment.view.*
import kotlinx.coroutines.*
import java.text.SimpleDateFormat
import java.util.*
import kotlin.concurrent.schedule

private typealias ReadCallback = ChipClusters.IntegerAttributeCallback

Expand Down Expand Up @@ -56,15 +55,20 @@ class SensorClientFragment : Fragment() {
watchSensorButtonUnchecked()
}
}
val currentTime = Calendar.getInstance().time.time
sensorGraph.addSeries(sensorData)
sensorGraph.gridLabelRenderer.padding = 30
sensorGraph.viewport.isXAxisBoundsManual = true
sensorGraph.viewport.setMinX(currentTime.toDouble())
sensorGraph.viewport.setMaxX(currentTime.toDouble() + REFRESH_PERIOD_MS * MAX_DATA_POINTS)
sensorGraph.gridLabelRenderer.padding = 20
sensorGraph.gridLabelRenderer.numHorizontalLabels = 4
sensorGraph.gridLabelRenderer.setHorizontalLabelsAngle(150)
sensorGraph.gridLabelRenderer.labelFormatter = object : LabelFormatter {
override fun setViewport(viewport: Viewport?) = Unit
override fun formatLabel(value: Double, isValueX: Boolean): String {
if (!isValueX)
return value.toString()
return SimpleDateFormat("H:m:s").format(Date(value.toLong())).toString()
return "%.2f".format(value)
return SimpleDateFormat("H:mm:ss").format(Date(value.toLong())).toString()
}
}
}
Expand Down Expand Up @@ -151,11 +155,16 @@ class SensorClientFragment : Fragment() {
)

if (addToGraph) {
// Make the graph visible on the first sample
if (sensorData.isEmpty)
sensorGraph.visibility = View.VISIBLE
val isFirstSample = sensorData.isEmpty
val dataPoint = DataPoint(Calendar.getInstance().time, value)
sensorData.appendData(dataPoint, false, MAX_DATA_POINTS)
sensorData.appendData(dataPoint, true, MAX_DATA_POINTS)
if (isFirstSample) {
// Make the graph visible on the first sample. Also, workaround a bug in graphview
// related to calculating the viewport when there is only one data point by
// duplicating the first sample.
sensorData.appendData(dataPoint, true, MAX_DATA_POINTS)
sensorGraph.visibility = View.VISIBLE
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
<com.jjoe64.graphview.GraphView
android:id="@+id/sensorGraph"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_height="150dp"
android:layout_below="@+id/lastValueTv"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
Expand Down
2 changes: 1 addition & 1 deletion src/android/CHIPTool/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<string name="sensor_client_btn_text">Sensor clusters</string>
<string name="sensor_client_read_btn_text">Read</string>
<string name="sensor_client_watch_btn_text">Watch</string>
<string name="sensor_client_last_value_text">Last value: %1$.1f %2$s</string>
<string name="sensor_client_last_value_text">Last value: %1$.2f %2$s</string>
<string name="sensor_client_read_error_text">Failed to read the sensor: %1$s</string>

</resources>

0 comments on commit 0cee9ec

Please sign in to comment.