Graphview不作图

时间:2018-11-05 19:58:02

标签: android android-graphview

我正在使用graphView库来实时绘制来自另一个活动的数据。尽管数据进入并且没有错误发生,但是由于某种原因,数据没有显示在图形上。我现在只想绘制x数据。有任何想法吗?我正在使用Galaxy S7进行测试。

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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我为子孙后代弄清楚了。我将addEntry函数的内部放入处理程序中,并放置了graph.addSeries(mSeries);。就在appendData函数之后。

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);

    }