1

I'm using the graphView library to graph data coming in from another activity in real time. Although the data comes in and no errors occur, but for some reason the data doesn't show up on the graph. I am trying to just graph the x data for now. Any ideas? I am using the Galaxy S7 to test.

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;

import java.util.ArrayList;
import java.util.Random;

public class ShowData extends AppCompatActivity {

    private ArrayList<String> xData = new ArrayList<>();
    private ArrayList<String> yData = new ArrayList<>();
    private ArrayList<String> zData = new ArrayList<>();

    TextView Xval;
    TextView Yval;
    TextView Zval;

//    RealtimeUpdates update = new RealtimeUpdates();

    LineGraphSeries<DataPoint> mSeries = new LineGraphSeries<>();
    int lastX = 0;

    int whereX, whereY, whereZ;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_data);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

        LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);

        Xval = findViewById(R.id.valueX);
        Yval = findViewById(R.id.valueY);
        Zval = findViewById(R.id.valueZ);


        GraphView graph = (GraphView) findViewById(R.id.graph);
        LineGraphSeries<DataPoint> mSeries = new LineGraphSeries<>();
        mSeries.setDrawDataPoints(true);
        graph.addSeries(mSeries);


//        GraphView graph = (GraphView) findViewById(R.id.graph);
//        LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
//                new DataPoint(0, 1),
//                new DataPoint(1, 5),
//                new DataPoint(2, 3),
//                new DataPoint(3, 2),
//                new DataPoint(4, 6)
//        });
//        graph.addSeries(series);


        BroadcastReceiver xyzData = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //Do the things
                String Xdata = intent.getStringExtra("X data");
                Log.i("X intent data", "" + Xdata); // receives null, intent not sending correctly
                String Ydata = intent.getStringExtra("Y data");
                String Zdata = intent.getStringExtra("Z data");

                Xval.setText(Xdata);
                Yval.setText(Ydata);
                Zval.setText(Zdata);


                try {
                    if (Xdata.contains("X")) {
                        whereX = Xdata.indexOf("X");
                        try {
                            Xdata = Xdata.substring(whereX + 3, whereX + 8);
                            Log.d("Data mani. X", "DataVal: " + Xdata);
                        } catch (Exception e) {
                            Log.d("X at end", "" + whereX);
                        }

                    }

                    if (Ydata.contains("Y")) {
                        whereY = Ydata.indexOf("Y");
                        try {
                            Ydata = Ydata.substring(whereY, whereY + 8);
                            Log.d("Data mani. Y", "DataVal: " + Ydata);
                        } catch (Exception e) {
                            Log.d("Y at end", "" + whereY);
                        }

                    }

                    if (Zdata.contains("Z")) {
                        whereZ = Zdata.indexOf("Z");
                        try {
                            Zdata = Zdata.substring(whereZ, whereZ + 8);
                            Log.d("Data mani. Z", "DataVal: " + Zdata);
                        } catch (Exception e) {
                            Log.d("Z at end", "" + whereZ);
                        }

                    }

                } catch(Exception e){
                    e.printStackTrace();
                }


                xData.add(Xdata);
                yData.add(Ydata);
                zData.add(Zdata);


                addEntry(Xdata);
                lastX = lastX + 1;

            }
        };

        lbm.registerReceiver(xyzData, new IntentFilter("Data Reception"));

    }

    public void addEntry(String data){
        float dataVal = 0;

        try {
            dataVal = Float.parseFloat(data);
            mSeries.appendData(new DataPoint(lastX ,dataVal), true, 10); // Data point, scroll to end, max data points
            Log.d("lastX", "" + lastX);
            Log.d("dataVal", "" + dataVal);

        } catch (Exception e) {
            e.printStackTrace();
            Log.d("STR -> INT", "int dataVal contained string");
            Log.d("lastX", "" + lastX);
            Log.d("dataVal", "" + dataVal);
        }
    }
}
Catalina T.
  • 3,456
  • 19
  • 29

1 Answers1

0

I figured it out for future generations. I placed the inside of the addEntry function into a handler and placed the graph.addSeries(mSeries); right after the appendData function.

public void addEntry(final String data){
        graphHandle.postDelayed(new Runnable() {
            @Override
            public void run() {
                float dataVal = 0;
                try {

                    dataVal = Float.parseFloat(data);
                    mSeries.appendData(new DataPoint(lastX ,dataVal), true, 10); // Data point, scroll to end, max data points
                    lastX = lastX + 1;
                    graph.addSeries(mSeries);

                    //graphView.redrawAll();
                    //mSeries.resetData();
                    Log.d("lastX1", "" + lastX);
                    Log.d("dataVal1", "" + dataVal);

                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d("STR -> INT", "int dataVal contained string");
                    Log.d("lastX2", "" + lastX);
                    Log.d("dataVal2", "" + dataVal);
                }
            }
        }, 250);

    }