From 15a487b69c9485e81349461dcd61e437771b49b6 Mon Sep 17 00:00:00 2001 From: Marc Nause Date: Tue, 31 Dec 2024 08:53:34 +0100 Subject: [PATCH] fix: ranges of diagrams make it hard to identify single values (#2603) --- .../pslab/sensors/AbstractSensorActivity.java | 75 +++++++++ .../java/io/pslab/sensors/SensorADS1115.java | 48 +----- .../java/io/pslab/sensors/SensorAPDS9960.java | 93 +---------- .../java/io/pslab/sensors/SensorBMP180.java | 144 ++---------------- .../java/io/pslab/sensors/SensorCCS811.java | 94 +----------- .../java/io/pslab/sensors/SensorHMC5883L.java | 67 ++------ .../java/io/pslab/sensors/SensorMLX90614.java | 94 +----------- .../java/io/pslab/sensors/SensorMPU6050.java | 122 ++------------- .../java/io/pslab/sensors/SensorMPU925X.java | 123 ++------------- .../java/io/pslab/sensors/SensorSHT21.java | 94 +----------- .../java/io/pslab/sensors/SensorTSL2561.java | 69 ++------- .../java/io/pslab/sensors/SensorVL53L0X.java | 48 +----- 12 files changed, 171 insertions(+), 900 deletions(-) diff --git a/app/src/main/java/io/pslab/sensors/AbstractSensorActivity.java b/app/src/main/java/io/pslab/sensors/AbstractSensorActivity.java index 7b1928366..7e6e74d4b 100644 --- a/app/src/main/java/io/pslab/sensors/AbstractSensorActivity.java +++ b/app/src/main/java/io/pslab/sensors/AbstractSensorActivity.java @@ -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; @@ -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; @@ -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) { diff --git a/app/src/main/java/io/pslab/sensors/SensorADS1115.java b/app/src/main/java/io/pslab/sensors/SensorADS1115.java index 047546fc5..b5b9221b3 100644 --- a/app/src/main/java/io/pslab/sensors/SensorADS1115.java +++ b/app/src/main/java/io/pslab/sensors/SensorADS1115.java @@ -1,6 +1,5 @@ package io.pslab.sensors; -import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.widget.Spinner; @@ -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; @@ -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<>(); @@ -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); } } diff --git a/app/src/main/java/io/pslab/sensors/SensorAPDS9960.java b/app/src/main/java/io/pslab/sensors/SensorAPDS9960.java index 7ef54a118..ca35782cd 100644 --- a/app/src/main/java/io/pslab/sensors/SensorAPDS9960.java +++ b/app/src/main/java/io/pslab/sensors/SensorAPDS9960.java @@ -1,6 +1,5 @@ package io.pslab.sensors; -import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.widget.Spinner; @@ -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; @@ -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<>(); @@ -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: diff --git a/app/src/main/java/io/pslab/sensors/SensorBMP180.java b/app/src/main/java/io/pslab/sensors/SensorBMP180.java index a4c824cce..f9c65afac 100644 --- a/app/src/main/java/io/pslab/sensors/SensorBMP180.java +++ b/app/src/main/java/io/pslab/sensors/SensorBMP180.java @@ -9,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; @@ -69,107 +65,9 @@ public void onCreate(@Nullable Bundle savedInstanceState) { mChartAltitude = findViewById(R.id.chart_alt_bmp180); mChartPressure = findViewById(R.id.chart_pre_bmp180); - XAxis xTemperature = mChartTemperature.getXAxis(); - YAxis yTemperature = mChartTemperature.getAxisLeft(); - YAxis yTemperature2 = mChartTemperature.getAxisRight(); - - XAxis xAltitude = mChartAltitude.getXAxis(); - YAxis yAltitude = mChartAltitude.getAxisLeft(); - YAxis yAltitude2 = mChartAltitude.getAxisRight(); - - XAxis xPressure = mChartPressure.getXAxis(); - YAxis yPressure = mChartPressure.getAxisLeft(); - YAxis yPressure2 = mChartPressure.getAxisRight(); - - mChartTemperature.setTouchEnabled(true); - mChartTemperature.setHighlightPerDragEnabled(true); - mChartTemperature.setDragEnabled(true); - mChartTemperature.setScaleEnabled(true); - mChartTemperature.setDrawGridBackground(false); - mChartTemperature.setPinchZoom(true); - mChartTemperature.setScaleYEnabled(false); - mChartTemperature.setBackgroundColor(Color.BLACK); - mChartTemperature.getDescription().setEnabled(false); - - LineData data = new LineData(); - data.setValueTextColor(Color.WHITE); - mChartTemperature.setData(data); - - Legend l = mChartTemperature.getLegend(); - l.setForm(Legend.LegendForm.LINE); - l.setTextColor(Color.WHITE); - - xTemperature.setTextColor(Color.WHITE); - xTemperature.setDrawGridLines(true); - xTemperature.setAvoidFirstLastClipping(true); - - yTemperature.setTextColor(Color.WHITE); - yTemperature.setAxisMaximum(70f); - yTemperature.setAxisMinimum(0f); - yTemperature.setDrawGridLines(true); - yTemperature.setLabelCount(10); - - yTemperature2.setDrawGridLines(false); - - mChartAltitude.setTouchEnabled(true); - mChartAltitude.setHighlightPerDragEnabled(true); - mChartAltitude.setDragEnabled(true); - mChartAltitude.setScaleEnabled(true); - mChartAltitude.setDrawGridBackground(false); - mChartAltitude.setPinchZoom(true); - mChartAltitude.setScaleYEnabled(false); - mChartAltitude.setBackgroundColor(Color.BLACK); - mChartAltitude.getDescription().setEnabled(false); - - LineData data2 = new LineData(); - data.setValueTextColor(Color.WHITE); - mChartAltitude.setData(data2); - - Legend l2 = mChartAltitude.getLegend(); - l2.setForm(Legend.LegendForm.LINE); - l2.setTextColor(Color.WHITE); - - xAltitude.setTextColor(Color.WHITE); - xAltitude.setDrawGridLines(true); - xAltitude.setAvoidFirstLastClipping(true); - - yAltitude.setTextColor(Color.WHITE); - yAltitude.setAxisMaximum(3000f); - yAltitude.setAxisMinimum(0f); - yAltitude.setDrawGridLines(true); - yAltitude.setLabelCount(10); - - yAltitude2.setDrawGridLines(false); - - mChartPressure.setTouchEnabled(true); - mChartPressure.setHighlightPerDragEnabled(true); - mChartPressure.setDragEnabled(true); - mChartPressure.setScaleEnabled(true); - mChartPressure.setDrawGridBackground(false); - mChartPressure.setPinchZoom(true); - mChartPressure.setScaleYEnabled(false); - mChartPressure.setBackgroundColor(Color.BLACK); - mChartPressure.getDescription().setEnabled(false); - - LineData data3 = new LineData(); - data.setValueTextColor(Color.WHITE); - mChartTemperature.setData(data3); - - Legend l3 = mChartTemperature.getLegend(); - l3.setForm(Legend.LegendForm.LINE); - l3.setTextColor(Color.WHITE); - - xPressure.setTextColor(Color.WHITE); - xPressure.setDrawGridLines(true); - xPressure.setAvoidFirstLastClipping(true); - - yPressure.setTextColor(Color.WHITE); - yPressure.setAxisMaximum(1000000f); - yPressure.setAxisMinimum(0f); - yPressure.setDrawGridLines(true); - yPressure.setLabelCount(10); - - yPressure2.setDrawGridLines(false); + initChart(mChartTemperature); + initChart(mChartAltitude); + initChart(mChartPressure); if (savedInstanceState == null) { entriesTemperature = new ArrayList<>(); @@ -231,31 +129,17 @@ public void updateUi() { tvSensorBMP180Pressure.setText(DataFormatter.formatDouble(dataBMP180[2], DataFormatter.HIGH_PRECISION_FORMAT)); } - LineDataSet dataSet1 = new LineDataSet(entriesTemperature, getString(R.string.temperature)); - LineDataSet dataSet2 = new LineDataSet(entriesAltitude, getString(R.string.altitude)); - LineDataSet dataSet3 = new LineDataSet(entriesPressure, getString(R.string.pressure)); - - dataSet1.setColor(Color.BLUE); - dataSet2.setColor(Color.GREEN); - dataSet3.setColor(Color.RED); - - LineData data = new LineData(dataSet1); - mChartTemperature.setData(data); - mChartTemperature.notifyDataSetChanged(); - mChartTemperature.setVisibleXRangeMaximum(10); - mChartTemperature.moveViewToX(timeElapsed); - - LineData data2 = new LineData(dataSet2); - mChartAltitude.setData(data2); - mChartAltitude.notifyDataSetChanged(); - mChartAltitude.setVisibleXRangeMaximum(10); - mChartAltitude.moveViewToX(timeElapsed); - - LineData data3 = new LineData(dataSet3); - mChartPressure.setData(data3); - mChartPressure.notifyDataSetChanged(); - mChartPressure.setVisibleXRangeMaximum(10); - mChartPressure.moveViewToX(timeElapsed); + LineDataSet dataSetTemperature = new LineDataSet(entriesTemperature, getString(R.string.temperature)); + LineDataSet dataSetAltitude = new LineDataSet(entriesAltitude, getString(R.string.altitude)); + LineDataSet dataSetPressure = new LineDataSet(entriesPressure, getString(R.string.pressure)); + + dataSetTemperature.setColor(Color.BLUE); + dataSetAltitude.setColor(Color.GREEN); + dataSetPressure.setColor(Color.RED); + + updateChart(mChartTemperature, timeElapsed, dataSetTemperature); + updateChart(mChartAltitude, timeElapsed, dataSetAltitude); + updateChart(mChartPressure, timeElapsed, dataSetPressure); } } diff --git a/app/src/main/java/io/pslab/sensors/SensorCCS811.java b/app/src/main/java/io/pslab/sensors/SensorCCS811.java index 468879f2e..76f7ef4f9 100644 --- a/app/src/main/java/io/pslab/sensors/SensorCCS811.java +++ b/app/src/main/java/io/pslab/sensors/SensorCCS811.java @@ -1,6 +1,5 @@ package io.pslab.sensors; -import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.widget.TextView; @@ -9,11 +8,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; @@ -61,73 +56,8 @@ public void onCreate(@Nullable Bundle savedInstanceState) { mCharteCO2 = findViewById(R.id.chart_eCO2_ccs811); mChartTVOC = findViewById(R.id.chart_TVOC_ccs811); - XAxis xeCO2 = mCharteCO2.getXAxis(); - YAxis yeCO2 = mCharteCO2.getAxisLeft(); - YAxis yeCO22 = mCharteCO2.getAxisRight(); - - XAxis xTVOC = mChartTVOC.getXAxis(); - YAxis yTVOC = mChartTVOC.getAxisLeft(); - YAxis yTVOC2 = mChartTVOC.getAxisRight(); - - mCharteCO2.setTouchEnabled(true); - mCharteCO2.setHighlightPerDragEnabled(true); - mCharteCO2.setDragEnabled(true); - mCharteCO2.setScaleEnabled(true); - mCharteCO2.setDrawGridBackground(false); - mCharteCO2.setPinchZoom(true); - mCharteCO2.setScaleYEnabled(false); - mCharteCO2.setBackgroundColor(Color.BLACK); - mCharteCO2.getDescription().setEnabled(false); - - LineData data = new LineData(); - data.setValueTextColor(Color.WHITE); - mCharteCO2.setData(data); - - Legend l = mCharteCO2.getLegend(); - l.setForm(Legend.LegendForm.LINE); - l.setTextColor(Color.WHITE); - - xeCO2.setTextColor(Color.WHITE); - xeCO2.setDrawGridLines(true); - xeCO2.setAvoidFirstLastClipping(true); - - yeCO2.setTextColor(Color.WHITE); - yeCO2.setAxisMaximum(10000f); - yeCO2.setAxisMinimum(0); - yeCO2.setDrawGridLines(true); - yeCO2.setLabelCount(10); - - yeCO22.setDrawGridLines(false); - - mChartTVOC.setTouchEnabled(true); - mChartTVOC.setHighlightPerDragEnabled(true); - mChartTVOC.setDragEnabled(true); - mChartTVOC.setScaleEnabled(true); - mChartTVOC.setDrawGridBackground(false); - mChartTVOC.setPinchZoom(true); - mChartTVOC.setScaleYEnabled(false); - mChartTVOC.setBackgroundColor(Color.BLACK); - mChartTVOC.getDescription().setEnabled(false); - - LineData data2 = new LineData(); - data.setValueTextColor(Color.WHITE); - mChartTVOC.setData(data2); - - Legend l2 = mChartTVOC.getLegend(); - l2.setForm(Legend.LegendForm.LINE); - l2.setTextColor(Color.WHITE); - - xTVOC.setTextColor(Color.WHITE); - xTVOC.setDrawGridLines(true); - xTVOC.setAvoidFirstLastClipping(true); - - yTVOC.setTextColor(Color.WHITE); - yTVOC.setAxisMaximum(2000f); - yTVOC.setAxisMinimum(0f); - yTVOC.setDrawGridLines(true); - yTVOC.setLabelCount(10); - - yTVOC2.setDrawGridLines(false); + initChart(mCharteCO2); + initChart(mChartTVOC); if (savedInstanceState == null) { entrieseCO2 = new ArrayList<>(); @@ -184,23 +114,11 @@ public void updateUi() { tvSensorCCS811TVOC.setText(DataFormatter.formatDouble(dataCCS811TVOC, DataFormatter.HIGH_PRECISION_FORMAT)); } - LineDataSet dataSet1 = new LineDataSet(entrieseCO2, getString(R.string.eCO2)); - LineDataSet dataSet2 = new LineDataSet(entriesTVOC, getString(R.string.eTVOC)); + LineDataSet dataSeteCO2 = new LineDataSet(entrieseCO2, getString(R.string.eCO2)); + LineDataSet dataSetTVOC = new LineDataSet(entriesTVOC, getString(R.string.eTVOC)); - dataSet1.setDrawCircles(true); - dataSet2.setDrawCircles(true); - - LineData data = new LineData(dataSet1); - mCharteCO2.setData(data); - mCharteCO2.notifyDataSetChanged(); - mCharteCO2.setVisibleXRangeMaximum(10); - mCharteCO2.moveViewToX(timeElapsed); - - LineData data2 = new LineData(dataSet2); - mChartTVOC.setData(data2); - mChartTVOC.notifyDataSetChanged(); - mChartTVOC.setVisibleXRangeMaximum(10); - mChartTVOC.moveViewToX(timeElapsed); + updateChart(mCharteCO2, timeElapsed, dataSeteCO2); + updateChart(mChartTVOC, timeElapsed, dataSetTVOC); } } diff --git a/app/src/main/java/io/pslab/sensors/SensorHMC5883L.java b/app/src/main/java/io/pslab/sensors/SensorHMC5883L.java index efff0c98b..205dca4ac 100644 --- a/app/src/main/java/io/pslab/sensors/SensorHMC5883L.java +++ b/app/src/main/java/io/pslab/sensors/SensorHMC5883L.java @@ -9,13 +9,8 @@ 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 com.github.mikephil.charting.interfaces.datasets.ILineDataSet; import java.io.IOException; import java.util.ArrayList; @@ -65,39 +60,8 @@ public void onCreate(@Nullable Bundle savedInstanceState) { tvSensorHMC5883Lby = findViewById(R.id.tv_sensor_hmc5883l_by); tvSensorHMC5883Lbz = findViewById(R.id.tv_sensor_hmc5883l_bz); mChart = findViewById(R.id.chart_hmc5883l); - 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(10f); - y.setAxisMinimum(-10f); - y.setDrawGridLines(true); - y.setLabelCount(10); - - y2.setDrawGridLines(false); + + initChart(mChart); if (savedInstanceState == null) { entriesBx = new ArrayList<>(); @@ -159,28 +123,15 @@ public void updateUi() { tvSensorHMC5883Lbz.setText(DataFormatter.formatDouble(dataHMC5883L.get(2), DataFormatter.HIGH_PRECISION_FORMAT)); } - LineDataSet dataset1 = new LineDataSet(entriesBx, getString(R.string.bx)); - LineDataSet dataSet2 = new LineDataSet(entriesBy, getString(R.string.by)); - LineDataSet dataSet3 = new LineDataSet(entriesBz, getString(R.string.bz)); - - dataset1.setColor(Color.BLUE); - dataSet2.setColor(Color.GREEN); - dataSet3.setColor(Color.RED); - - dataset1.setDrawCircles(true); - dataSet2.setDrawCircles(true); - dataSet3.setDrawCircles(true); + LineDataSet dataSetBx = new LineDataSet(entriesBx, getString(R.string.bx)); + LineDataSet dataSetBy = new LineDataSet(entriesBy, getString(R.string.by)); + LineDataSet dataSetBz = new LineDataSet(entriesBz, getString(R.string.bz)); - List dataSets = new ArrayList<>(); - dataSets.add(dataset1); - dataSets.add(dataSet2); - dataSets.add(dataSet3); + dataSetBx.setColor(Color.BLUE); + dataSetBy.setColor(Color.GREEN); + dataSetBz.setColor(Color.RED); - LineData data = new LineData(dataSets); - mChart.setData(data); - mChart.notifyDataSetChanged(); - mChart.setVisibleXRangeMaximum(10); - mChart.moveViewToX(timeElapsed); + updateChart(mChart, timeElapsed, dataSetBx, dataSetBy, dataSetBz); } } diff --git a/app/src/main/java/io/pslab/sensors/SensorMLX90614.java b/app/src/main/java/io/pslab/sensors/SensorMLX90614.java index 3fe9ef0f6..6feb65779 100644 --- a/app/src/main/java/io/pslab/sensors/SensorMLX90614.java +++ b/app/src/main/java/io/pslab/sensors/SensorMLX90614.java @@ -2,7 +2,6 @@ import android.annotation.SuppressLint; import android.content.SharedPreferences; -import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; @@ -17,11 +16,7 @@ import androidx.appcompat.app.AlertDialog; 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; @@ -76,73 +71,8 @@ public void onCreate(@Nullable Bundle savedInstanceState) { mChartObjectTemperature = findViewById(R.id.chart_obj_temp_mlx); mChartAmbientTemperature = findViewById(R.id.chart_amb_temp_mlx); - XAxis xObjectTemperature = mChartObjectTemperature.getXAxis(); - YAxis yObjectTemperature = mChartObjectTemperature.getAxisLeft(); - YAxis yObjectTemperature2 = mChartObjectTemperature.getAxisRight(); - - XAxis xAmbientTemperature = mChartAmbientTemperature.getXAxis(); - YAxis yAmbientTemperature = mChartAmbientTemperature.getAxisLeft(); - YAxis yAmbientTemperature2 = mChartAmbientTemperature.getAxisRight(); - - mChartObjectTemperature.setTouchEnabled(true); - mChartObjectTemperature.setHighlightPerDragEnabled(true); - mChartObjectTemperature.setDragEnabled(true); - mChartObjectTemperature.setScaleEnabled(true); - mChartObjectTemperature.setDrawGridBackground(false); - mChartObjectTemperature.setPinchZoom(true); - mChartObjectTemperature.setScaleYEnabled(false); - mChartObjectTemperature.setBackgroundColor(Color.BLACK); - mChartObjectTemperature.getDescription().setEnabled(false); - - LineData data = new LineData(); - data.setValueTextColor(Color.WHITE); - mChartObjectTemperature.setData(data); - - Legend l = mChartObjectTemperature.getLegend(); - l.setForm(Legend.LegendForm.LINE); - l.setTextColor(Color.WHITE); - - xObjectTemperature.setTextColor(Color.WHITE); - xObjectTemperature.setDrawGridLines(true); - xObjectTemperature.setAvoidFirstLastClipping(true); - - yObjectTemperature.setTextColor(Color.WHITE); - yObjectTemperature.setAxisMaximum(125f); - yObjectTemperature.setAxisMinimum(-40f); - yObjectTemperature.setDrawGridLines(true); - yObjectTemperature.setLabelCount(10); - - yObjectTemperature2.setDrawGridLines(false); - - mChartAmbientTemperature.setTouchEnabled(true); - mChartAmbientTemperature.setHighlightPerDragEnabled(true); - mChartAmbientTemperature.setDragEnabled(true); - mChartAmbientTemperature.setScaleEnabled(true); - mChartAmbientTemperature.setDrawGridBackground(false); - mChartAmbientTemperature.setPinchZoom(true); - mChartAmbientTemperature.setScaleYEnabled(false); - mChartAmbientTemperature.setBackgroundColor(Color.BLACK); - mChartAmbientTemperature.getDescription().setEnabled(false); - - LineData data2 = new LineData(); - data.setValueTextColor(Color.WHITE); - mChartAmbientTemperature.setData(data2); - - Legend l2 = mChartAmbientTemperature.getLegend(); - l2.setForm(Legend.LegendForm.LINE); - l2.setTextColor(Color.WHITE); - - xAmbientTemperature.setTextColor(Color.WHITE); - xAmbientTemperature.setDrawGridLines(true); - xAmbientTemperature.setAvoidFirstLastClipping(true); - - yAmbientTemperature.setTextColor(Color.WHITE); - yAmbientTemperature.setAxisMaximum(380f); - yAmbientTemperature.setAxisMinimum(-70f); - yAmbientTemperature.setDrawGridLines(true); - yAmbientTemperature.setLabelCount(10); - - yAmbientTemperature2.setDrawGridLines(false); + initChart(mChartObjectTemperature); + initChart(mChartAmbientTemperature); if (savedInstanceState == null) { entriesObjectTemperature = new ArrayList<>(); @@ -236,23 +166,11 @@ public void updateUi() { tvSensorMLX90614AmbientTemp.setText(DataFormatter.formatDouble(dataMLX90614AmbientTemp, DataFormatter.HIGH_PRECISION_FORMAT)); } - LineDataSet dataSet1 = new LineDataSet(entriesObjectTemperature, getString(R.string.object_temp)); - LineDataSet dataSet2 = new LineDataSet(entriesAmbientTemperature, getString(R.string.ambient_temp)); + LineDataSet dataSetObjectTemperature = new LineDataSet(entriesObjectTemperature, getString(R.string.object_temp)); + LineDataSet dataSetAmbientTemperature = new LineDataSet(entriesAmbientTemperature, getString(R.string.ambient_temp)); - dataSet1.setDrawCircles(true); - dataSet2.setDrawCircles(true); - - LineData data1 = new LineData(dataSet1); - mChartObjectTemperature.setData(data1); - mChartObjectTemperature.notifyDataSetChanged(); - mChartObjectTemperature.setVisibleXRangeMaximum(10); - mChartObjectTemperature.moveViewToX(timeElapsed); - - LineData data2 = new LineData(dataSet2); - mChartAmbientTemperature.setData(data2); - mChartAmbientTemperature.notifyDataSetChanged(); - mChartAmbientTemperature.setVisibleXRangeMaximum(10); - mChartAmbientTemperature.moveViewToX(timeElapsed); + updateChart(mChartObjectTemperature, timeElapsed, dataSetObjectTemperature); + updateChart(mChartAmbientTemperature, timeElapsed, dataSetAmbientTemperature); } } diff --git a/app/src/main/java/io/pslab/sensors/SensorMPU6050.java b/app/src/main/java/io/pslab/sensors/SensorMPU6050.java index cfa3a1241..e58fd15b2 100644 --- a/app/src/main/java/io/pslab/sensors/SensorMPU6050.java +++ b/app/src/main/java/io/pslab/sensors/SensorMPU6050.java @@ -10,13 +10,8 @@ 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 com.github.mikephil.charting.interfaces.datasets.ILineDataSet; import java.io.IOException; import java.util.ArrayList; @@ -101,73 +96,8 @@ public void onCreate(@Nullable Bundle savedInstanceState) { mChartAcceleration = findViewById(R.id.chart_sensor_mpu6050_accelerometer); mChartGyroscope = findViewById(R.id.chart_sensor_mpu6050_gyroscope); - XAxis xAccelerometer = mChartAcceleration.getXAxis(); - YAxis yAccelerometer = mChartAcceleration.getAxisLeft(); - YAxis yAccelerometer2 = mChartAcceleration.getAxisRight(); - - XAxis xGyroscope = mChartGyroscope.getXAxis(); - YAxis yGyroscope = mChartGyroscope.getAxisLeft(); - YAxis yGyroscope2 = mChartGyroscope.getAxisRight(); - - mChartAcceleration.setTouchEnabled(true); - mChartAcceleration.setHighlightPerDragEnabled(true); - mChartAcceleration.setDragEnabled(true); - mChartAcceleration.setScaleEnabled(true); - mChartAcceleration.setDrawGridBackground(false); - mChartAcceleration.setPinchZoom(true); - mChartAcceleration.setScaleYEnabled(false); - mChartAcceleration.setBackgroundColor(Color.BLACK); - mChartAcceleration.getDescription().setEnabled(false); - - LineData data = new LineData(); - data.setValueTextColor(Color.WHITE); - mChartAcceleration.setData(data); - - Legend l = mChartAcceleration.getLegend(); - l.setForm(Legend.LegendForm.LINE); - l.setTextColor(Color.WHITE); - - xAccelerometer.setTextColor(Color.WHITE); - xAccelerometer.setDrawGridLines(true); - xAccelerometer.setAvoidFirstLastClipping(true); - - yAccelerometer.setTextColor(Color.WHITE); - yAccelerometer.setAxisMaximum(25f); - yAccelerometer.setAxisMinimum(-25f); - yAccelerometer.setDrawGridLines(true); - yAccelerometer.setLabelCount(10); - - yAccelerometer2.setDrawGridLines(false); - - mChartGyroscope.setTouchEnabled(true); - mChartGyroscope.setHighlightPerDragEnabled(true); - mChartGyroscope.setDragEnabled(true); - mChartGyroscope.setScaleEnabled(true); - mChartGyroscope.setDrawGridBackground(false); - mChartGyroscope.setPinchZoom(true); - mChartGyroscope.setScaleYEnabled(false); - mChartGyroscope.setBackgroundColor(Color.BLACK); - mChartGyroscope.getDescription().setEnabled(false); - - LineData data2 = new LineData(); - data.setValueTextColor(Color.WHITE); - mChartGyroscope.setData(data2); - - Legend l2 = mChartGyroscope.getLegend(); - l2.setForm(Legend.LegendForm.LINE); - l2.setTextColor(Color.WHITE); - - xGyroscope.setTextColor(Color.WHITE); - xGyroscope.setDrawGridLines(true); - xGyroscope.setAvoidFirstLastClipping(true); - - yGyroscope.setTextColor(Color.WHITE); - yGyroscope.setAxisMaximum(200f); - yGyroscope.setAxisMinimum(-200f); - yGyroscope.setDrawGridLines(true); - yGyroscope.setLabelCount(10); - - yGyroscope2.setDrawGridLines(false); + initChart(mChartAcceleration); + initChart(mChartGyroscope); try { if (sensorMPU6050 != null && getScienceLab().isConnected()) { @@ -280,44 +210,24 @@ public void updateUi() { tvSensorMPU6050temp.setText(DataFormatter.formatDouble(dataMPU6050.get(3), DataFormatter.HIGH_PRECISION_FORMAT)); } - LineDataSet dataset1 = new LineDataSet(entriesAx, getString(R.string.ax)); - LineDataSet dataSet2 = new LineDataSet(entriesAy, getString(R.string.ay)); - LineDataSet dataSet3 = new LineDataSet(entriesAz, getString(R.string.az)); + LineDataSet dataSetAx = new LineDataSet(entriesAx, getString(R.string.ax)); + LineDataSet dataSetAy = new LineDataSet(entriesAy, getString(R.string.ay)); + LineDataSet dataSetAz = new LineDataSet(entriesAz, getString(R.string.az)); - LineDataSet dataset4 = new LineDataSet(entriesGx, getString(R.string.gx)); - LineDataSet dataSet5 = new LineDataSet(entriesGy, getString(R.string.gy)); - LineDataSet dataSet6 = new LineDataSet(entriesGz, getString(R.string.gz)); + LineDataSet dataSetGx = new LineDataSet(entriesGx, getString(R.string.gx)); + LineDataSet dataSetGy = new LineDataSet(entriesGy, getString(R.string.gy)); + LineDataSet dataSetGz = new LineDataSet(entriesGz, getString(R.string.gz)); + dataSetAx.setColor(Color.BLUE); + dataSetAy.setColor(Color.GREEN); + dataSetAz.setColor(Color.RED); - dataset1.setColor(Color.BLUE); - dataSet2.setColor(Color.GREEN); - dataSet3.setColor(Color.RED); + dataSetGx.setColor(Color.BLUE); + dataSetGy.setColor(Color.GREEN); + dataSetGz.setColor(Color.RED); - dataset4.setColor(Color.BLUE); - dataSet5.setColor(Color.GREEN); - dataSet6.setColor(Color.RED); - - List dataSets = new ArrayList<>(); - dataSets.add(dataset1); - dataSets.add(dataSet2); - dataSets.add(dataSet3); - - List dataSets2 = new ArrayList<>(); - dataSets2.add(dataset4); - dataSets2.add(dataSet5); - dataSets2.add(dataSet6); - - LineData data = new LineData(dataSets); - mChartAcceleration.setData(data); - mChartAcceleration.notifyDataSetChanged(); - mChartAcceleration.setVisibleXRangeMaximum(10); - mChartAcceleration.moveViewToX(timeElapsed); - - LineData data2 = new LineData(dataSets2); - mChartGyroscope.setData(data2); - mChartGyroscope.notifyDataSetChanged(); - mChartGyroscope.setVisibleXRangeMaximum(10); - mChartGyroscope.moveViewToX(timeElapsed); + updateChart(mChartAcceleration, timeElapsed, dataSetAx, dataSetAy, dataSetAz); + updateChart(mChartGyroscope, timeElapsed, dataSetGx, dataSetGy, dataSetGz); } } diff --git a/app/src/main/java/io/pslab/sensors/SensorMPU925X.java b/app/src/main/java/io/pslab/sensors/SensorMPU925X.java index 79a784728..ce3b0f003 100644 --- a/app/src/main/java/io/pslab/sensors/SensorMPU925X.java +++ b/app/src/main/java/io/pslab/sensors/SensorMPU925X.java @@ -10,17 +10,11 @@ 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 com.github.mikephil.charting.interfaces.datasets.ILineDataSet; import java.io.IOException; import java.util.ArrayList; -import java.util.List; import io.pslab.DataFormatter; import io.pslab.R; @@ -101,73 +95,8 @@ public void onCreate(@Nullable Bundle savedInstanceState) { mChartAcceleration = findViewById(R.id.chart_sensor_mpu925x_accelerometer); mChartGyroscope = findViewById(R.id.chart_sensor_mpu925x_gyroscope); - XAxis xAccelerometer = mChartAcceleration.getXAxis(); - YAxis yAccelerometer = mChartAcceleration.getAxisLeft(); - YAxis yAccelerometer2 = mChartAcceleration.getAxisRight(); - - XAxis xGyroscope = mChartGyroscope.getXAxis(); - YAxis yGyroscope = mChartGyroscope.getAxisLeft(); - YAxis yGyroscope2 = mChartGyroscope.getAxisRight(); - - mChartAcceleration.setTouchEnabled(true); - mChartAcceleration.setHighlightPerDragEnabled(true); - mChartAcceleration.setDragEnabled(true); - mChartAcceleration.setScaleEnabled(true); - mChartAcceleration.setDrawGridBackground(false); - mChartAcceleration.setPinchZoom(true); - mChartAcceleration.setScaleYEnabled(false); - mChartAcceleration.setBackgroundColor(Color.BLACK); - mChartAcceleration.getDescription().setEnabled(false); - - LineData data = new LineData(); - data.setValueTextColor(Color.WHITE); - mChartAcceleration.setData(data); - - Legend l = mChartAcceleration.getLegend(); - l.setForm(Legend.LegendForm.LINE); - l.setTextColor(Color.WHITE); - - xAccelerometer.setTextColor(Color.WHITE); - xAccelerometer.setDrawGridLines(true); - xAccelerometer.setAvoidFirstLastClipping(true); - - yAccelerometer.setTextColor(Color.WHITE); - yAccelerometer.setAxisMaximum(25f); - yAccelerometer.setAxisMinimum(-25f); - yAccelerometer.setDrawGridLines(true); - yAccelerometer.setLabelCount(10); - - yAccelerometer2.setDrawGridLines(false); - - mChartGyroscope.setTouchEnabled(true); - mChartGyroscope.setHighlightPerDragEnabled(true); - mChartGyroscope.setDragEnabled(true); - mChartGyroscope.setScaleEnabled(true); - mChartGyroscope.setDrawGridBackground(false); - mChartGyroscope.setPinchZoom(true); - mChartGyroscope.setScaleYEnabled(false); - mChartGyroscope.setBackgroundColor(Color.BLACK); - mChartGyroscope.getDescription().setEnabled(false); - - LineData data2 = new LineData(); - data.setValueTextColor(Color.WHITE); - mChartGyroscope.setData(data2); - - Legend l2 = mChartGyroscope.getLegend(); - l2.setForm(Legend.LegendForm.LINE); - l2.setTextColor(Color.WHITE); - - xGyroscope.setTextColor(Color.WHITE); - xGyroscope.setDrawGridLines(true); - xGyroscope.setAvoidFirstLastClipping(true); - - yGyroscope.setTextColor(Color.WHITE); - yGyroscope.setAxisMaximum(200f); - yGyroscope.setAxisMinimum(-200f); - yGyroscope.setDrawGridLines(true); - yGyroscope.setLabelCount(10); - - yGyroscope2.setDrawGridLines(false); + initChart(mChartAcceleration); + initChart(mChartGyroscope); try { if (sensorMPU925X != null) { @@ -283,44 +212,24 @@ public void updateUi() { tvSensorMPU925Xtemp.setText(DataFormatter.formatDouble(dataTemp, DataFormatter.HIGH_PRECISION_FORMAT)); } - LineDataSet dataSet1 = new LineDataSet(entriesAx, getString(R.string.ax)); - LineDataSet dataSet2 = new LineDataSet(entriesAy, getString(R.string.ay)); - LineDataSet dataSet3 = new LineDataSet(entriesAz, getString(R.string.az)); + LineDataSet dataSetAx = new LineDataSet(entriesAx, getString(R.string.ax)); + LineDataSet dataSetAy = new LineDataSet(entriesAy, getString(R.string.ay)); + LineDataSet dataSetAz = new LineDataSet(entriesAz, getString(R.string.az)); - LineDataSet dataSet4 = new LineDataSet(entriesGx, getString(R.string.gx)); - LineDataSet dataSet5 = new LineDataSet(entriesGy, getString(R.string.gy)); - LineDataSet dataSet6 = new LineDataSet(entriesGz, getString(R.string.gz)); + LineDataSet dataSetGx = new LineDataSet(entriesGx, getString(R.string.gx)); + LineDataSet dataSetGy = new LineDataSet(entriesGy, getString(R.string.gy)); + LineDataSet dataSetGz = new LineDataSet(entriesGz, getString(R.string.gz)); + dataSetAx.setColor(Color.BLUE); + dataSetAy.setColor(Color.GREEN); + dataSetAz.setColor(Color.RED); - dataSet1.setColor(Color.BLUE); - dataSet2.setColor(Color.GREEN); - dataSet3.setColor(Color.RED); + dataSetGx.setColor(Color.BLUE); + dataSetGy.setColor(Color.GREEN); + dataSetGz.setColor(Color.RED); - dataSet4.setColor(Color.BLUE); - dataSet5.setColor(Color.GREEN); - dataSet6.setColor(Color.RED); - - List dataSets = new ArrayList<>(); - dataSets.add(dataSet1); - dataSets.add(dataSet2); - dataSets.add(dataSet3); - - List dataSets2 = new ArrayList<>(); - dataSets2.add(dataSet4); - dataSets2.add(dataSet5); - dataSets2.add(dataSet6); - - LineData data = new LineData(dataSets); - mChartAcceleration.setData(data); - mChartAcceleration.notifyDataSetChanged(); - mChartAcceleration.setVisibleXRangeMaximum(10); - mChartAcceleration.moveViewToX(timeElapsed); - - LineData data2 = new LineData(dataSets2); - mChartGyroscope.setData(data2); - mChartGyroscope.notifyDataSetChanged(); - mChartGyroscope.setVisibleXRangeMaximum(10); - mChartGyroscope.moveViewToX(timeElapsed); + updateChart(mChartAcceleration, timeElapsed, dataSetAx, dataSetAy, dataSetAz); + updateChart(mChartGyroscope, timeElapsed, dataSetGx, dataSetGy, dataSetGz); } } diff --git a/app/src/main/java/io/pslab/sensors/SensorSHT21.java b/app/src/main/java/io/pslab/sensors/SensorSHT21.java index a5c4822b6..ee651763f 100644 --- a/app/src/main/java/io/pslab/sensors/SensorSHT21.java +++ b/app/src/main/java/io/pslab/sensors/SensorSHT21.java @@ -1,6 +1,5 @@ package io.pslab.sensors; -import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.widget.TextView; @@ -9,11 +8,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; @@ -62,73 +57,8 @@ public void onCreate(@Nullable Bundle savedInstanceState) { mChartTemperature = findViewById(R.id.chart_temperature_sht21); mChartHumidity = findViewById(R.id.chart_humidity_sht21); - XAxis xTemperature = mChartTemperature.getXAxis(); - YAxis yTemperature = mChartTemperature.getAxisLeft(); - YAxis yTemperature2 = mChartTemperature.getAxisRight(); - - XAxis xHumidity = mChartHumidity.getXAxis(); - YAxis yHumidity = mChartHumidity.getAxisLeft(); - YAxis yHumidity2 = mChartHumidity.getAxisRight(); - - mChartTemperature.setTouchEnabled(true); - mChartTemperature.setHighlightPerDragEnabled(true); - mChartTemperature.setDragEnabled(true); - mChartTemperature.setScaleEnabled(true); - mChartTemperature.setDrawGridBackground(false); - mChartTemperature.setPinchZoom(true); - mChartTemperature.setScaleYEnabled(false); - mChartTemperature.setBackgroundColor(Color.BLACK); - mChartTemperature.getDescription().setEnabled(false); - - LineData data = new LineData(); - data.setValueTextColor(Color.WHITE); - mChartTemperature.setData(data); - - Legend l = mChartTemperature.getLegend(); - l.setForm(Legend.LegendForm.LINE); - l.setTextColor(Color.WHITE); - - xTemperature.setTextColor(Color.WHITE); - xTemperature.setDrawGridLines(true); - xTemperature.setAvoidFirstLastClipping(true); - - yTemperature.setTextColor(Color.WHITE); - yTemperature.setAxisMaximum(125f); - yTemperature.setAxisMinimum(-40f); - yTemperature.setDrawGridLines(true); - yTemperature.setLabelCount(10); - - yTemperature2.setDrawGridLines(false); - - mChartHumidity.setTouchEnabled(true); - mChartHumidity.setHighlightPerDragEnabled(true); - mChartHumidity.setDragEnabled(true); - mChartHumidity.setScaleEnabled(true); - mChartHumidity.setDrawGridBackground(false); - mChartHumidity.setPinchZoom(true); - mChartHumidity.setScaleYEnabled(false); - mChartHumidity.setBackgroundColor(Color.BLACK); - mChartHumidity.getDescription().setEnabled(false); - - LineData data2 = new LineData(); - data.setValueTextColor(Color.WHITE); - mChartHumidity.setData(data2); - - Legend l2 = mChartHumidity.getLegend(); - l2.setForm(Legend.LegendForm.LINE); - l2.setTextColor(Color.WHITE); - - xHumidity.setTextColor(Color.WHITE); - xHumidity.setDrawGridLines(true); - xHumidity.setAvoidFirstLastClipping(true); - - yHumidity.setTextColor(Color.WHITE); - yHumidity.setAxisMaximum(100f); - yHumidity.setAxisMinimum(0f); - yHumidity.setDrawGridLines(true); - yHumidity.setLabelCount(10); - - yHumidity2.setDrawGridLines(false); + initChart(mChartTemperature); + initChart(mChartHumidity); if (savedInstanceState == null) { entriesTemperature = new ArrayList<>(); @@ -186,23 +116,11 @@ public void updateUi() { tvSensorSHT21Humidity.setText(DataFormatter.formatDouble(dataSHT21Humidity.get(0), DataFormatter.HIGH_PRECISION_FORMAT)); } - LineDataSet dataSet1 = new LineDataSet(entriesTemperature, getString(R.string.temperature)); - LineDataSet dataSet2 = new LineDataSet(entriesHumidity, getString(R.string.humidity)); + LineDataSet dataSetTemperature = new LineDataSet(entriesTemperature, getString(R.string.temperature)); + LineDataSet dataSetHumidity = new LineDataSet(entriesHumidity, getString(R.string.humidity)); - dataSet1.setDrawCircles(true); - dataSet2.setDrawCircles(true); - - LineData data = new LineData(dataSet1); - mChartTemperature.setData(data); - mChartTemperature.notifyDataSetChanged(); - mChartTemperature.setVisibleXRangeMaximum(10); - mChartTemperature.moveViewToX(timeElapsed); - - LineData data2 = new LineData(dataSet2); - mChartHumidity.setData(data2); - mChartHumidity.notifyDataSetChanged(); - mChartHumidity.setVisibleXRangeMaximum(10); - mChartHumidity.moveViewToX(timeElapsed); + updateChart(mChartTemperature, timeElapsed, dataSetTemperature); + updateChart(mChartHumidity, timeElapsed, dataSetHumidity); } } diff --git a/app/src/main/java/io/pslab/sensors/SensorTSL2561.java b/app/src/main/java/io/pslab/sensors/SensorTSL2561.java index 47918c6b2..64473cb16 100644 --- a/app/src/main/java/io/pslab/sensors/SensorTSL2561.java +++ b/app/src/main/java/io/pslab/sensors/SensorTSL2561.java @@ -11,17 +11,11 @@ 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 com.github.mikephil.charting.interfaces.datasets.ILineDataSet; import java.io.IOException; import java.util.ArrayList; -import java.util.List; import io.pslab.R; import io.pslab.communication.peripherals.I2C; @@ -73,6 +67,8 @@ public void onCreate(@Nullable Bundle savedInstanceState) { etSensorTSL2561Timing = findViewById(R.id.et_sensor_tsl2561_timing); mChart = findViewById(R.id.chart_tsl2561); + initChart(mChart); + try { if (sensorTSL2561 != null & getScienceLab().isConnected()) { sensorTSL2561.setGain(spinnerSensorTSL2561Gain.getSelectedItem().toString()); @@ -81,40 +77,6 @@ public void onCreate(@Nullable Bundle savedInstanceState) { Log.e(TAG, "Error setting gain.", e); } - 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(1700f); - y.setAxisMinimum(0f); - y.setDrawGridLines(true); - y.setLabelCount(10); - - y2.setDrawGridLines(false); - if (savedInstanceState == null) { entriesFull = new ArrayList<>(); entriesInfrared = new ArrayList<>(); @@ -182,28 +144,15 @@ public void updateUi() { tvSensorTSL2561Visible.setText(String.valueOf(dataTSL2561[2])); } - LineDataSet dataset1 = new LineDataSet(entriesFull, getString(R.string.full)); - LineDataSet dataSet2 = new LineDataSet(entriesInfrared, getString(R.string.infrared)); - LineDataSet dataSet3 = new LineDataSet(entriesVisible, getString(R.string.visible)); - - dataset1.setColor(Color.BLUE); - dataSet2.setColor(Color.GREEN); - dataSet3.setColor(Color.RED); - - dataset1.setDrawCircles(true); - dataSet2.setDrawCircles(true); - dataSet3.setDrawCircles(true); + LineDataSet datasetFull = new LineDataSet(entriesFull, getString(R.string.full)); + LineDataSet dataSetInfrared = new LineDataSet(entriesInfrared, getString(R.string.infrared)); + LineDataSet dataSetVisible = new LineDataSet(entriesVisible, getString(R.string.visible)); - List dataSets = new ArrayList<>(); - dataSets.add(dataset1); - dataSets.add(dataSet2); - dataSets.add(dataSet3); + datasetFull.setColor(Color.BLUE); + dataSetInfrared.setColor(Color.GREEN); + dataSetVisible.setColor(Color.RED); - LineData data = new LineData(dataSets); - mChart.setData(data); - mChart.notifyDataSetChanged(); - mChart.setVisibleXRangeMaximum(10); - mChart.moveViewToX(timeElapsed); + updateChart(mChart, timeElapsed, datasetFull, dataSetInfrared, dataSetVisible); } } diff --git a/app/src/main/java/io/pslab/sensors/SensorVL53L0X.java b/app/src/main/java/io/pslab/sensors/SensorVL53L0X.java index 55349c475..fa675acaa 100644 --- a/app/src/main/java/io/pslab/sensors/SensorVL53L0X.java +++ b/app/src/main/java/io/pslab/sensors/SensorVL53L0X.java @@ -1,6 +1,5 @@ package io.pslab.sensors; -import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.widget.TextView; @@ -9,11 +8,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; @@ -52,39 +47,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { tvSensorVL53L0X = findViewById(R.id.tv_sensor_vl53l0x); mChart = findViewById(R.id.chart_sensor_ads); - 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(10000f); - y.setAxisMinimum(0); - y.setDrawGridLines(true); - y.setLabelCount(10); - - y2.setDrawGridLines(false); + + initChart(mChart); if (savedInstanceState == null) { entries = new ArrayList<>(); @@ -133,12 +97,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); } }