Skip to content

Commit 15a487b

Browse files
authored
fix: ranges of diagrams make it hard to identify single values (#2603)
1 parent 1231ae1 commit 15a487b

12 files changed

+171
-900
lines changed

app/src/main/java/io/pslab/sensors/AbstractSensorActivity.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.pslab.sensors;
22

3+
import android.graphics.Color;
4+
import android.graphics.Typeface;
35
import android.os.Bundle;
46
import android.os.Handler;
57
import android.os.HandlerThread;
@@ -21,6 +23,13 @@
2123
import androidx.appcompat.app.AppCompatActivity;
2224
import androidx.appcompat.widget.Toolbar;
2325

26+
import com.github.mikephil.charting.charts.LineChart;
27+
import com.github.mikephil.charting.components.Legend;
28+
import com.github.mikephil.charting.components.XAxis;
29+
import com.github.mikephil.charting.components.YAxis;
30+
import com.github.mikephil.charting.data.LineData;
31+
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
32+
2433
import java.util.Locale;
2534

2635
import io.pslab.R;
@@ -242,6 +251,72 @@ protected long getStartTime() {
242251
return startTime;
243252
}
244253

254+
/**
255+
* Update data of a chart.
256+
*
257+
* @param chart chart to update
258+
* @param timeElapsed time elapsed since last update of chart
259+
* @param dataSet data set which contains data to display
260+
*/
261+
protected static void updateChart(LineChart chart, float timeElapsed, ILineDataSet dataSet) {
262+
updateChart(chart, timeElapsed, new LineData(dataSet));
263+
}
264+
265+
/**
266+
* Update data of a chart.
267+
*
268+
* @param chart chart to update
269+
* @param timeElapsed time elapsed since last update of chart
270+
* @param dataSets data sets which contains data to display
271+
*/
272+
protected static void updateChart(LineChart chart, float timeElapsed, ILineDataSet... dataSets) {
273+
updateChart(chart, timeElapsed, new LineData(dataSets));
274+
}
275+
276+
private static void updateChart(LineChart chart, float timeElapsed, LineData data) {
277+
data.setValueTextSize(0f); // turn off text
278+
chart.setData(data);
279+
chart.notifyDataSetChanged();
280+
chart.setVisibleXRangeMaximum(10);
281+
chart.moveViewToX(timeElapsed);
282+
}
283+
284+
protected void initChart(LineChart chart) {
285+
XAxis x = chart.getXAxis();
286+
YAxis y = chart.getAxisLeft();
287+
YAxis y2 = chart.getAxisRight();
288+
289+
chart.setTouchEnabled(true);
290+
chart.setHighlightPerDragEnabled(true);
291+
chart.setDragEnabled(true);
292+
chart.setScaleEnabled(true);
293+
chart.setDrawGridBackground(false);
294+
chart.setPinchZoom(true);
295+
chart.setScaleYEnabled(false);
296+
chart.setBackgroundColor(Color.BLACK);
297+
chart.getDescription().setEnabled(false);
298+
chart.setAutoScaleMinMaxEnabled(true);
299+
chart.setNoDataText(getString(R.string.no_data_fetched));
300+
chart.setNoDataTextColor(Color.YELLOW);
301+
chart.setNoDataTextTypeface(Typeface.MONOSPACE);
302+
303+
Legend l = chart.getLegend();
304+
l.setForm(Legend.LegendForm.LINE);
305+
l.setTextColor(Color.WHITE);
306+
307+
x.setTextColor(Color.WHITE);
308+
x.setDrawGridLines(true);
309+
x.setAvoidFirstLastClipping(true);
310+
311+
y.setTextColor(Color.WHITE);
312+
y.setDrawGridLines(true);
313+
y.setLabelCount(10, true);
314+
y.setGranularity(0.1f);
315+
316+
y2.setDrawGridLines(false);
317+
318+
}
319+
245320
@Override
246321
public boolean onOptionsItemSelected(MenuItem item) {
247322
if (item.getItemId() == android.R.id.home) {

app/src/main/java/io/pslab/sensors/SensorADS1115.java

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.pslab.sensors;
22

3-
import android.graphics.Color;
43
import android.os.Bundle;
54
import android.util.Log;
65
import android.widget.Spinner;
@@ -10,11 +9,7 @@
109
import androidx.annotation.Nullable;
1110

1211
import com.github.mikephil.charting.charts.LineChart;
13-
import com.github.mikephil.charting.components.Legend;
14-
import com.github.mikephil.charting.components.XAxis;
15-
import com.github.mikephil.charting.components.YAxis;
1612
import com.github.mikephil.charting.data.Entry;
17-
import com.github.mikephil.charting.data.LineData;
1813
import com.github.mikephil.charting.data.LineDataSet;
1914

2015
import java.io.IOException;
@@ -67,39 +62,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
6762
if (sensorADS1115 != null) {
6863
sensorADS1115.setDataRate(Integer.parseInt(spinnerSensorADS1115Rate.getSelectedItem().toString()));
6964
}
70-
XAxis x = mChart.getXAxis();
71-
YAxis y = mChart.getAxisLeft();
72-
YAxis y2 = mChart.getAxisRight();
73-
74-
mChart.setTouchEnabled(true);
75-
mChart.setHighlightPerDragEnabled(true);
76-
mChart.setDragEnabled(true);
77-
mChart.setScaleEnabled(true);
78-
mChart.setDrawGridBackground(false);
79-
mChart.setPinchZoom(true);
80-
mChart.setScaleYEnabled(false);
81-
mChart.setBackgroundColor(Color.BLACK);
82-
mChart.getDescription().setEnabled(false);
83-
84-
LineData data = new LineData();
85-
data.setValueTextColor(Color.WHITE);
86-
mChart.setData(data);
87-
88-
Legend l = mChart.getLegend();
89-
l.setForm(Legend.LegendForm.LINE);
90-
l.setTextColor(Color.WHITE);
91-
92-
x.setTextColor(Color.WHITE);
93-
x.setDrawGridLines(true);
94-
x.setAvoidFirstLastClipping(true);
95-
96-
y.setTextColor(Color.WHITE);
97-
y.setAxisMaximum(6.15f);
98-
y.setAxisMinimum(-6.15f);
99-
y.setDrawGridLines(true);
100-
y.setLabelCount(10);
101-
102-
y2.setDrawGridLines(false);
65+
66+
initChart(mChart);
10367

10468
if (savedInstanceState == null) {
10569
entries = new ArrayList<>();
@@ -148,12 +112,8 @@ public void updateUi() {
148112
}
149113

150114
LineDataSet dataSet = new LineDataSet(entries, getString(R.string.bx));
151-
dataSet.setDrawCircles(true);
152-
LineData data = new LineData(dataSet);
153-
mChart.setData(data);
154-
mChart.notifyDataSetChanged();
155-
mChart.setVisibleXRangeMaximum(10);
156-
mChart.moveViewToX(timeElapsed);
115+
116+
updateChart(mChart, timeElapsed, dataSet);
157117
}
158118
}
159119

app/src/main/java/io/pslab/sensors/SensorAPDS9960.java

Lines changed: 6 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.pslab.sensors;
22

3-
import android.graphics.Color;
43
import android.os.Bundle;
54
import android.util.Log;
65
import android.widget.Spinner;
@@ -10,11 +9,7 @@
109
import androidx.annotation.Nullable;
1110

1211
import com.github.mikephil.charting.charts.LineChart;
13-
import com.github.mikephil.charting.components.Legend;
14-
import com.github.mikephil.charting.components.XAxis;
15-
import com.github.mikephil.charting.components.YAxis;
1612
import com.github.mikephil.charting.data.Entry;
17-
import com.github.mikephil.charting.data.LineData;
1813
import com.github.mikephil.charting.data.LineDataSet;
1914

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

81-
XAxis xLux = mChartLux.getXAxis();
82-
YAxis yLux = mChartLux.getAxisLeft();
83-
YAxis yLux2 = mChartLux.getAxisRight();
84-
85-
XAxis xProximity = mChartProximity.getXAxis();
86-
YAxis yProximity = mChartProximity.getAxisLeft();
87-
YAxis yProximity2 = mChartProximity.getAxisRight();
88-
89-
mChartLux.setTouchEnabled(true);
90-
mChartLux.setHighlightPerDragEnabled(true);
91-
mChartLux.setDragEnabled(true);
92-
mChartLux.setScaleEnabled(true);
93-
mChartLux.setDrawGridBackground(false);
94-
mChartLux.setPinchZoom(true);
95-
mChartLux.setScaleYEnabled(false);
96-
mChartLux.setBackgroundColor(Color.BLACK);
97-
mChartLux.getDescription().setEnabled(false);
98-
99-
LineData data = new LineData();
100-
data.setValueTextColor(Color.WHITE);
101-
mChartLux.setData(data);
102-
103-
Legend l = mChartLux.getLegend();
104-
l.setForm(Legend.LegendForm.LINE);
105-
l.setTextColor(Color.WHITE);
106-
107-
xLux.setTextColor(Color.WHITE);
108-
xLux.setDrawGridLines(true);
109-
xLux.setAvoidFirstLastClipping(true);
110-
111-
yLux.setTextColor(Color.WHITE);
112-
yLux.setAxisMaximum(10000f);
113-
yLux.setAxisMinimum(0);
114-
yLux.setDrawGridLines(true);
115-
yLux.setLabelCount(10);
116-
117-
yLux2.setDrawGridLines(false);
118-
119-
mChartProximity.setTouchEnabled(true);
120-
mChartProximity.setHighlightPerDragEnabled(true);
121-
mChartProximity.setDragEnabled(true);
122-
mChartProximity.setScaleEnabled(true);
123-
mChartProximity.setDrawGridBackground(false);
124-
mChartProximity.setPinchZoom(true);
125-
mChartProximity.setScaleYEnabled(false);
126-
mChartProximity.setBackgroundColor(Color.BLACK);
127-
mChartProximity.getDescription().setEnabled(false);
128-
129-
LineData data2 = new LineData();
130-
data.setValueTextColor(Color.WHITE);
131-
mChartProximity.setData(data2);
132-
133-
Legend l2 = mChartProximity.getLegend();
134-
l2.setForm(Legend.LegendForm.LINE);
135-
l2.setTextColor(Color.WHITE);
136-
137-
xProximity.setTextColor(Color.WHITE);
138-
xProximity.setDrawGridLines(true);
139-
xProximity.setAvoidFirstLastClipping(true);
140-
141-
yProximity.setTextColor(Color.WHITE);
142-
yProximity.setAxisMaximum(256f);
143-
yProximity.setAxisMinimum(0f);
144-
yProximity.setDrawGridLines(true);
145-
yProximity.setLabelCount(10);
146-
147-
yProximity2.setDrawGridLines(false);
76+
initChart(mChartLux);
77+
initChart(mChartProximity);
14878

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

232-
LineDataSet dataSet1 = new LineDataSet(entriesLux, getString(R.string.light_lux));
233-
LineDataSet dataSet2 = new LineDataSet(entriesProximity, getString(R.string.proximity));
162+
LineDataSet dataSetLux = new LineDataSet(entriesLux, getString(R.string.light_lux));
163+
LineDataSet dataSetProximity = new LineDataSet(entriesProximity, getString(R.string.proximity));
234164

235-
dataSet1.setDrawCircles(true);
236-
dataSet2.setDrawCircles(true);
165+
updateChart(mChartLux, timeElapsed, dataSetLux);
166+
updateChart(mChartProximity, timeElapsed, dataSetProximity);
237167

238-
LineData data = new LineData(dataSet1);
239-
mChartLux.setData(data);
240-
mChartLux.notifyDataSetChanged();
241-
mChartLux.setVisibleXRangeMaximum(10);
242-
mChartLux.moveViewToX(timeElapsed);
243-
244-
LineData data2 = new LineData(dataSet2);
245-
mChartProximity.setData(data2);
246-
mChartProximity.notifyDataSetChanged();
247-
mChartProximity.setVisibleXRangeMaximum(10);
248-
mChartProximity.moveViewToX(timeElapsed);
249168
} else if (isSensorDataAcquired()) {
250169
switch (dataAPDS9960Gesture) {
251170
case 1:

0 commit comments

Comments
 (0)