I need to update my graphview data continuously receiving through bluetooth, i am using com.jjoe64.graphview.GraphView ,I called Bluetooth connection, data communication logics in a Service class to maintain bluetooth connection throughout the Application, From Service class i need to update my Graphview whenever i receive new bluetooth data, Currently my graphview data overlaps with new data, I need to plot 3 graphs(Red,Yellow and blue phase) at a time with 2 series Voltage and Current. I need to reset the whole data each time, please suggest, I tried resetData method but no help, please suggest a solution
if(writeMessage.contains("$W11")){ //R Phase
String VRMagnitude_red = null,IRMagnitude_red = null, Theta_red = null;
int color=0;
int thickness=0;
list.remove("$W11");
if(writeMessage.contains("VR")){
list.remove("VR");
thickness=3;
VRMagnitude_red = (String) list.get(32);
System.out.println("--------------VR Magnitude[Red] "+VRMagnitude_red);
list.remove(VRMagnitude_red);
System.out.println("--------------list size "+list.size());
}
else if(writeMessage.contains("IR")) {
list.remove("IR");
thickness=0;
IRMagnitude_red = (String) list.get(32);
System.out.println("--------------IR Magnitude[Red] "+IRMagnitude_red+", "+list.size());
list.remove(IRMagnitude_red);
Theta_red = (String) list.get(32);
System.out.println("--------------Theta[Red] "+Theta_red);
list.remove(Theta_red);
System.out.println("--------------list size "+list.size());
}
color = Color.RED;
System.out.println("--------------ArrayList after removing spl chars-- "+list);
//---------------SINE WAVEFORM 1 starts--------------------
try {
dataPoints1 = new DataPoint[list.size()];
for (int i = 0; i < list.size(); i++) {
Double y = Double.parseDouble(String.valueOf(list.get(i)));
dataPoints1[i] = new DataPoint(i, y);
System.out.println("--------------ArrayList elements [Red]-- " + list.get(i));
}
//First scale of Red phase
series1 = new LineGraphSeries<>(dataPoints1);
//Second Scale of Red phase
series2 = new LineGraphSeries<>(dataPoints1);
series1.setThickness(thickness);
series1.setColor(color);
WaveformActivity.graphView1.getViewport().setXAxisBoundsManual(true);
WaveformActivity.graphView1.getViewport().setYAxisBoundsManual(true);
WaveformActivity.graphView1.getViewport().setMinX(0);
WaveformActivity.graphView1.getViewport().setMaxX(32);
WaveformActivity.graphView1.getViewport().setMinY(-1);
WaveformActivity.graphView1.getViewport().setMaxY(1);
// Set second scale
WaveformActivity.graphView1.getSecondScale().addSeries(series2);
// The y bounds are always manual for second scale
WaveformActivity.graphView1.getSecondScale().setMinY(-1);
WaveformActivity.graphView1.getSecondScale().setMaxY(1);
series2.setColor(color);
WaveformActivity.graphView1.getGridLabelRenderer().setVerticalLabelsSecondScaleColor(color);
WaveformActivity.graphView1.addSeries(series1);
newRSeries=false;
}catch (Exception e){
e.printStackTrace();
}
}else if(writeMessage.contains("$W12")){ //Y Phase
String VRMagnitude_yellow = null,IRMagnitude_yellow = null, Theta_yellow = null;
int color=0;
int thickness=0;
list.remove("$W12");
if(writeMessage.contains("VY")){
list.remove("VY");
thickness=3;
VRMagnitude_yellow = (String) list.get(32);
System.out.println("--------------VR Magnitude[Yellow] "+VRMagnitude_yellow);
list.remove(VRMagnitude_yellow);
System.out.println("--------------list size "+list.size());
}
else if(writeMessage.contains("IY")) {
list.remove("IY");
thickness=0;
IRMagnitude_yellow = (String) list.get(32);
System.out.println("--------------IR Magnitude[Yellow] "+IRMagnitude_yellow+", "+list.size());
list.remove(IRMagnitude_yellow);
Theta_yellow = (String) list.get(32);
System.out.println("--------------Theta[Yellow] "+Theta_yellow);
list.remove(Theta_yellow);
System.out.println("--------------list size "+list.size());
}
color = Color.parseColor("#feaa0c");
System.out.println("--------------ArrayList after removing spl chars-- "+list);
//---------------SINE WAVEFORM 2 starts--------------------
dataPoints2 = new DataPoint[list.size()];
try {
for (int i = 0; i < list.size(); i++) {
Double y = Double.parseDouble(String.valueOf(list.get(i)));
dataPoints2[i] = new DataPoint(i, y);
System.out.println("--------------ArrayList elements [YELLOW]-- " + list.get(i));
}
series3 = new LineGraphSeries<>(dataPoints2);
WaveformActivity.graphView2.addSeries(series3);
series3.setThickness(thickness);
series3.setColor(color);
WaveformActivity.graphView2.getViewport().setXAxisBoundsManual(true);
WaveformActivity.graphView2.getViewport().setYAxisBoundsManual(true);
WaveformActivity.graphView2.getViewport().setMinX(0);
WaveformActivity.graphView2.getViewport().setMaxX(32);
WaveformActivity.graphView2.getViewport().setMinY(-1);
WaveformActivity.graphView2.getViewport().setMaxY(1);
//Second Scale
series4 = new LineGraphSeries<>(dataPoints2);
// Set second scale
WaveformActivity.graphView2.getSecondScale().addSeries(series4);
// The y bounds are always manual for second scale
WaveformActivity.graphView2.getSecondScale().setMinY(-1);
WaveformActivity.graphView2.getSecondScale().setMaxY(1);
series4.setColor(color);
WaveformActivity.graphView2.getGridLabelRenderer().setVerticalLabelsSecondScaleColor(color);
newYSeries=false;
}catch (Exception e){
e.printStackTrace();
}
}else if(writeMessage.contains("$W13")){ //B Phase
String VRMagnitude_blue = null,IRMagnitude_blue = null, Theta_blue = null;
int color=0;
int thickness=0;
list.remove("$W13");
if(writeMessage.contains("VB")){
list.remove("VB");
thickness=3;
VRMagnitude_blue = (String) list.get(32);
System.out.println("--------------VR Magnitude[Blue] "+VRMagnitude_blue);
list.remove(VRMagnitude_blue);
System.out.println("--------------list size "+list.size());
}
else if(writeMessage.contains("IB")) {
list.remove("IB");
thickness=0;
IRMagnitude_blue = (String) list.get(32);
System.out.println("--------------IR Magnitude[Blue] "+IRMagnitude_blue+", "+list.size());
list.remove(IRMagnitude_blue);
Theta_blue = (String) list.get(32);
System.out.println("--------------Theta[Blue] "+Theta_blue);
list.remove(Theta_blue);
System.out.println("--------------list size "+list.size());
}
color = Color.BLUE;
System.out.println("--------------ArrayList after removing spl chars-- "+list);
//---------------SINE WAVEFORM 3 starts--------------------
dataPoints3 = new DataPoint[list.size()];
try {
for (int i = 0; i < list.size(); i++) {
Double y = Double.parseDouble(String.valueOf(list.get(i)));
dataPoints3[i] = new DataPoint(i, y);
System.out.println("--------------ArrayList elements [BLUE]-- " + list.get(i));
}
series5 = new LineGraphSeries<>(dataPoints3);
WaveformActivity.graphView3.addSeries(series5);
series5.setThickness(thickness);
series5.setColor(color);
WaveformActivity.graphView3.getViewport().setXAxisBoundsManual(true);
WaveformActivity.graphView3.getViewport().setYAxisBoundsManual(true);
WaveformActivity.graphView3.getViewport().setMinX(0);
WaveformActivity.graphView3.getViewport().setMaxX(32);
WaveformActivity.graphView3.getViewport().setMinY(-1);
WaveformActivity.graphView3.getViewport().setMaxY(1);
//Second Scale
series6 = new LineGraphSeries<>(dataPoints3);
// Set second scale
WaveformActivity.graphView3.getSecondScale().addSeries(series6);
// The y bounds are always manual for second scale
WaveformActivity.graphView3.getSecondScale().setMinY(-1);
WaveformActivity.graphView3.getSecondScale().setMaxY(1);
series6.setColor(color);
WaveformActivity.graphView3.getGridLabelRenderer().setVerticalLabelsSecondScaleColor(color);
newBSeries = false;
}catch (Exception e){
e.printStackTrace();
}
}
Also i try to clear old series data with new series data inside ConnectedThread class using below code,
public void run() {
Log.d("DEBUG BT", "IN CONNECTED THREAD RUN");
byte[] buffer = new byte[2048];
int begin = 0;
int bytes = 0;
while (true) {
try {
System.out.println("-------------New Series");
newRSeries = true;
newYSeries = true;
newBSeries = true;
//newWSeries = true;
al = new ArrayList<>();
bytes += mmInStream.read(buffer, bytes, buffer.length - bytes);
if(newRSeries && WaveformActivity.graphView1!=null && dataPoints1!=null){
WaveformActivity.graphView1.removeAllSeries();
WaveformActivity.graphView1.getSecondScale().removeAllSeries();
}
if(newYSeries && WaveformActivity.graphView2!=null && dataPoints2!=null){
WaveformActivity.graphView2.removeAllSeries();
WaveformActivity.graphView2.getSecondScale().removeAllSeries();
}
if(newBSeries && WaveformActivity.graphView3!=null && dataPoints3!=null){
WaveformActivity.graphView3.removeAllSeries();
WaveformActivity.graphView3.getSecondScale().removeAllSeries();
}
if(newRSeries && newYSeries && newBSeries &&
WaveformActivity.graphView1!=null && dataPoints1!=null &&
WaveformActivity.graphView2!=null && dataPoints2!=null &&
WaveformActivity.graphView3!=null && dataPoints3!=null){
WaveformActivity.graphView1.removeAllSeries();
WaveformActivity.graphView1.getSecondScale().removeAllSeries();
WaveformActivity.graphView2.removeAllSeries();
WaveformActivity.graphView2.getSecondScale().removeAllSeries();
WaveformActivity.graphView3.removeAllSeries();
WaveformActivity.graphView3.getSecondScale().removeAllSeries();
}
for(int i = begin; i < bytes; i++) {
if(buffer[i] == "#".getBytes()[0]) {
bluetoothIn.obtainMessage(1, begin, i, buffer).sendToTarget();
begin = i + 1;
if(i == bytes - 1) {
bytes = 0;
begin = 0;
}
}
}
} catch (IOException e) {
break;
}
}
}