Skip to content

Commit

Permalink
fix: ranges of diagrams make it hard to identify single values (#2603)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcnause authored Dec 31, 2024
1 parent 1231ae1 commit 15a487b
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 900 deletions.
75 changes: 75 additions & 0 deletions app/src/main/java/io/pslab/sensors/AbstractSensorActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.pslab.sensors;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
Expand All @@ -21,6 +23,13 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.Locale;

import io.pslab.R;
Expand Down Expand Up @@ -242,6 +251,72 @@ protected long getStartTime() {
return startTime;
}

/**
* Update data of a chart.
*
* @param chart chart to update
* @param timeElapsed time elapsed since last update of chart
* @param dataSet data set which contains data to display
*/
protected static void updateChart(LineChart chart, float timeElapsed, ILineDataSet dataSet) {
updateChart(chart, timeElapsed, new LineData(dataSet));
}

/**
* Update data of a chart.
*
* @param chart chart to update
* @param timeElapsed time elapsed since last update of chart
* @param dataSets data sets which contains data to display
*/
protected static void updateChart(LineChart chart, float timeElapsed, ILineDataSet... dataSets) {
updateChart(chart, timeElapsed, new LineData(dataSets));
}

private static void updateChart(LineChart chart, float timeElapsed, LineData data) {
data.setValueTextSize(0f); // turn off text
chart.setData(data);
chart.notifyDataSetChanged();
chart.setVisibleXRangeMaximum(10);
chart.moveViewToX(timeElapsed);
}

protected void initChart(LineChart chart) {
XAxis x = chart.getXAxis();
YAxis y = chart.getAxisLeft();
YAxis y2 = chart.getAxisRight();

chart.setTouchEnabled(true);
chart.setHighlightPerDragEnabled(true);
chart.setDragEnabled(true);
chart.setScaleEnabled(true);
chart.setDrawGridBackground(false);
chart.setPinchZoom(true);
chart.setScaleYEnabled(false);
chart.setBackgroundColor(Color.BLACK);
chart.getDescription().setEnabled(false);
chart.setAutoScaleMinMaxEnabled(true);
chart.setNoDataText(getString(R.string.no_data_fetched));
chart.setNoDataTextColor(Color.YELLOW);
chart.setNoDataTextTypeface(Typeface.MONOSPACE);

Legend l = chart.getLegend();
l.setForm(Legend.LegendForm.LINE);
l.setTextColor(Color.WHITE);

x.setTextColor(Color.WHITE);
x.setDrawGridLines(true);
x.setAvoidFirstLastClipping(true);

y.setTextColor(Color.WHITE);
y.setDrawGridLines(true);
y.setLabelCount(10, true);
y.setGranularity(0.1f);

y2.setDrawGridLines(false);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Expand Down
48 changes: 4 additions & 44 deletions app/src/main/java/io/pslab/sensors/SensorADS1115.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.pslab.sensors;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Spinner;
Expand All @@ -10,11 +9,7 @@
import androidx.annotation.Nullable;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.io.IOException;
Expand Down Expand Up @@ -67,39 +62,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
if (sensorADS1115 != null) {
sensorADS1115.setDataRate(Integer.parseInt(spinnerSensorADS1115Rate.getSelectedItem().toString()));
}
XAxis x = mChart.getXAxis();
YAxis y = mChart.getAxisLeft();
YAxis y2 = mChart.getAxisRight();

mChart.setTouchEnabled(true);
mChart.setHighlightPerDragEnabled(true);
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
mChart.setDrawGridBackground(false);
mChart.setPinchZoom(true);
mChart.setScaleYEnabled(false);
mChart.setBackgroundColor(Color.BLACK);
mChart.getDescription().setEnabled(false);

LineData data = new LineData();
data.setValueTextColor(Color.WHITE);
mChart.setData(data);

Legend l = mChart.getLegend();
l.setForm(Legend.LegendForm.LINE);
l.setTextColor(Color.WHITE);

x.setTextColor(Color.WHITE);
x.setDrawGridLines(true);
x.setAvoidFirstLastClipping(true);

y.setTextColor(Color.WHITE);
y.setAxisMaximum(6.15f);
y.setAxisMinimum(-6.15f);
y.setDrawGridLines(true);
y.setLabelCount(10);

y2.setDrawGridLines(false);

initChart(mChart);

if (savedInstanceState == null) {
entries = new ArrayList<>();
Expand Down Expand Up @@ -148,12 +112,8 @@ public void updateUi() {
}

LineDataSet dataSet = new LineDataSet(entries, getString(R.string.bx));
dataSet.setDrawCircles(true);
LineData data = new LineData(dataSet);
mChart.setData(data);
mChart.notifyDataSetChanged();
mChart.setVisibleXRangeMaximum(10);
mChart.moveViewToX(timeElapsed);

updateChart(mChart, timeElapsed, dataSet);
}
}

Expand Down
93 changes: 6 additions & 87 deletions app/src/main/java/io/pslab/sensors/SensorAPDS9960.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.pslab.sensors;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Spinner;
Expand All @@ -10,11 +9,7 @@
import androidx.annotation.Nullable;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.io.IOException;
Expand Down Expand Up @@ -78,73 +73,8 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
mChartLux = findViewById(R.id.chart_sensor_apds9960_lux);
mChartProximity = findViewById(R.id.chart_sensor_apds9960_proximity);

XAxis xLux = mChartLux.getXAxis();
YAxis yLux = mChartLux.getAxisLeft();
YAxis yLux2 = mChartLux.getAxisRight();

XAxis xProximity = mChartProximity.getXAxis();
YAxis yProximity = mChartProximity.getAxisLeft();
YAxis yProximity2 = mChartProximity.getAxisRight();

mChartLux.setTouchEnabled(true);
mChartLux.setHighlightPerDragEnabled(true);
mChartLux.setDragEnabled(true);
mChartLux.setScaleEnabled(true);
mChartLux.setDrawGridBackground(false);
mChartLux.setPinchZoom(true);
mChartLux.setScaleYEnabled(false);
mChartLux.setBackgroundColor(Color.BLACK);
mChartLux.getDescription().setEnabled(false);

LineData data = new LineData();
data.setValueTextColor(Color.WHITE);
mChartLux.setData(data);

Legend l = mChartLux.getLegend();
l.setForm(Legend.LegendForm.LINE);
l.setTextColor(Color.WHITE);

xLux.setTextColor(Color.WHITE);
xLux.setDrawGridLines(true);
xLux.setAvoidFirstLastClipping(true);

yLux.setTextColor(Color.WHITE);
yLux.setAxisMaximum(10000f);
yLux.setAxisMinimum(0);
yLux.setDrawGridLines(true);
yLux.setLabelCount(10);

yLux2.setDrawGridLines(false);

mChartProximity.setTouchEnabled(true);
mChartProximity.setHighlightPerDragEnabled(true);
mChartProximity.setDragEnabled(true);
mChartProximity.setScaleEnabled(true);
mChartProximity.setDrawGridBackground(false);
mChartProximity.setPinchZoom(true);
mChartProximity.setScaleYEnabled(false);
mChartProximity.setBackgroundColor(Color.BLACK);
mChartProximity.getDescription().setEnabled(false);

LineData data2 = new LineData();
data.setValueTextColor(Color.WHITE);
mChartProximity.setData(data2);

Legend l2 = mChartProximity.getLegend();
l2.setForm(Legend.LegendForm.LINE);
l2.setTextColor(Color.WHITE);

xProximity.setTextColor(Color.WHITE);
xProximity.setDrawGridLines(true);
xProximity.setAvoidFirstLastClipping(true);

yProximity.setTextColor(Color.WHITE);
yProximity.setAxisMaximum(256f);
yProximity.setAxisMinimum(0f);
yProximity.setDrawGridLines(true);
yProximity.setLabelCount(10);

yProximity2.setDrawGridLines(false);
initChart(mChartLux);
initChart(mChartProximity);

if (savedInstanceState == null) {
entriesLux = new ArrayList<>();
Expand Down Expand Up @@ -229,23 +159,12 @@ public void updateUi() {
tvSensorAPDS9960Proximity.setText(DataFormatter.formatDouble(dataAPDS9960Proximity, DataFormatter.HIGH_PRECISION_FORMAT));
}

LineDataSet dataSet1 = new LineDataSet(entriesLux, getString(R.string.light_lux));
LineDataSet dataSet2 = new LineDataSet(entriesProximity, getString(R.string.proximity));
LineDataSet dataSetLux = new LineDataSet(entriesLux, getString(R.string.light_lux));
LineDataSet dataSetProximity = new LineDataSet(entriesProximity, getString(R.string.proximity));

dataSet1.setDrawCircles(true);
dataSet2.setDrawCircles(true);
updateChart(mChartLux, timeElapsed, dataSetLux);
updateChart(mChartProximity, timeElapsed, dataSetProximity);

LineData data = new LineData(dataSet1);
mChartLux.setData(data);
mChartLux.notifyDataSetChanged();
mChartLux.setVisibleXRangeMaximum(10);
mChartLux.moveViewToX(timeElapsed);

LineData data2 = new LineData(dataSet2);
mChartProximity.setData(data2);
mChartProximity.notifyDataSetChanged();
mChartProximity.setVisibleXRangeMaximum(10);
mChartProximity.moveViewToX(timeElapsed);
} else if (isSensorDataAcquired()) {
switch (dataAPDS9960Gesture) {
case 1:
Expand Down
Loading

0 comments on commit 15a487b

Please sign in to comment.