added legend and random colors for lines

This commit is contained in:
paul-loedige 2021-01-08 02:42:23 +01:00
parent 17f1278c20
commit b0596010cf

View File

@ -1,5 +1,7 @@
package com.example.aped.ui.plots;
import android.graphics.Color;
import android.graphics.ColorSpace;
import android.net.sip.SipSession;
import android.content.DialogInterface;
import android.os.Bundle;
@ -15,6 +17,7 @@ import com.example.aped.R;
import com.example.aped.communication.Communicator;
import com.example.aped.utils.IXML;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.LegendRenderer;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
@ -23,6 +26,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class PlotFragment extends Fragment {
@ -34,6 +38,8 @@ public class PlotFragment extends Fragment {
private GraphView graphView;
/** List of all the devices with buffers. **/
private List<String> bufferedDevicesSelected;
/** maximum value on X axis. **/
private int maxX = 0;
/**
* paul
@ -103,7 +109,14 @@ public class PlotFragment extends Fragment {
}
private void getBufferedValues(final Communicator communicator, final IXML xml) {
Random random = new Random();
for (String deviceName : this.bufferedDevices) {
int factor;
if(xml.getPort(deviceName).get("protocol").equals("PWM")) {
factor = 1;
} else {
factor = 100;
}
communicator.readBuffer(deviceName, response -> {
try {
JSONObject buffer = response.getJSONObject("buffer");
@ -111,7 +124,7 @@ public class PlotFragment extends Fragment {
String valueString = buffer.getString(pin);
valueString = valueString.substring(2, valueString.length() - 2);
String[] valueStringArray = valueString.split("', '");
int[] values = new int[valueString.length()];
int[] values = new int[valueStringArray.length];
for (int i = 0; i < valueStringArray.length; i++) {
values[i] = Integer.parseInt(valueStringArray[i]);
}
@ -119,15 +132,23 @@ public class PlotFragment extends Fragment {
//display data
DataPoint[] dataPoints = new DataPoint[values.length];
for (int i = 0; i < values.length; i++){
dataPoints[i] = new DataPoint(i, values[i]);
dataPoints[i] = new DataPoint(i, values[i] * factor);
}
LineGraphSeries<DataPoint> lineGraphSeries = new LineGraphSeries<>(dataPoints);
this.graphView.addSeries(lineGraphSeries);
this.graphView.getViewport().setMaxX(600);
this.graphView.getViewport().setMaxY(100);
this.graphView.getViewport().setXAxisBoundsManual(true);
this.graphView.getViewport().setYAxisBoundsManual(true);
lineGraphSeries.setTitle(deviceName +":"+ pin);
lineGraphSeries.setColor(Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
graphView.addSeries(lineGraphSeries);
if (maxX < values.length) {
maxX = values.length;
}
graphView.getViewport().setMaxY(100);
graphView.getViewport().setMaxX(maxX);
graphView.getViewport().setXAxisBoundsManual(true);
graphView.getViewport().setYAxisBoundsManual(true);
}
graphView.getLegendRenderer().setVisible(true);
graphView.getLegendRenderer().setBackgroundColor(Color.rgb(200,200,200));
graphView.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);
} catch (JSONException e) {
Log.e("PlotFragment", "error while parsing JSON on READ BUFFER: " + e.getMessage());
}