Real-time graph with AChartEngine: Chart plotting last retrieved value from cloud database even when no value is sent to cloud

不羁的心 提交于 2019-12-11 16:03:30

问题


I am using AChartEngine to plot values obtained from Ubidots in real-time. The graph is retrieving values in real-time however when no value is being sent on Ubidots, the last retrieved value sent on Ubidots is being plotted again as shown in the screenshot.

Here are my codes:

LineGraph.java:

public class LineGraph {
 private GraphicalView view;

 private TimeSeries dataset = new TimeSeries("LDR Values");
 private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();

 private XYSeriesRenderer renderer = new XYSeriesRenderer(); // This will be used to customize line 1
 private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); // Holds a collection of XYSeriesRenderer and customizes the graph

public LineGraph()
{
    // Add single dataset to multiple dataset
    mDataset.addSeries(dataset);

    // Customization time for line 1!
    renderer.setColor(Color.BLUE);
    renderer.setPointStyle(PointStyle.SQUARE);
    renderer.setFillPoints(true);
    renderer.setDisplayChartValues(true);
    renderer.setChartValuesSpacing(10);


    // mRenderer: renderer that controls the full charts and add the single renderer for each series:
    // Enable Zoom
    //mRenderer.setZoomButtonsVisible(true);
    mRenderer.setMarginsColor(Color.argb(0x00, 0xff, 0x00, 0x00));
    mRenderer.setXTitle("Day #");
    mRenderer.setYTitle("LDR Values");
    mRenderer.setYAxisMax(30000);
    mRenderer.setYAxisMin(10000);
    mRenderer.setShowGrid(true);

    // Add single renderer to multiple renderer
    mRenderer.addSeriesRenderer(renderer);
}

public GraphicalView getView(Context context)
{
    view =  ChartFactory.getLineChartView(context, mDataset, mRenderer);
    return view;
}

public void addNewPoints(Point p)
{
    dataset.add(p.getX(), p.getY());
 }

}

Point.java:

public class Point {

private int x;
private int y;

public Point(int x, int y)
{
    this.x = x;
    this.y = y;

}

public int getX() {
    return x;
}

public int getY() {
    return y;
  }

}

MainActivity.java

public class MainActivity extends AppCompatActivity {

private TextView mTextMessage;
private static GraphicalView view;
private LineGraph line = new LineGraph();
private static Thread thread;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                mTextMessage.setText(R.string.title_home);
                return true;
            case R.id.navigation_dashboard:
                mTextMessage.setText(R.string.title_dashboard);
                return true;
            case R.id.navigation_notifications:
                mTextMessage.setText(R.string.title_notifications);
                return true;
        }
        return false;
    }
};

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

    mTextMessage = (TextView) findViewById(R.id.message);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    thread = new Thread() {
        public void run()
        {
            for (int i = 0; i <1000; i++)
            {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Point p = UbidotsData.getDataFromReceiver(i); // We got new data!

                line.addNewPoints(p); // Add it to our graph
                view.repaint();
            }
        }
    };
    thread.start();
}

@Override
protected void onStart() {
    super.onStart();
    view = line.getView(this);
    setContentView(view);
   }
}

UbidotsData.java

public class UbidotsData {

//private static Context context;


public static Point getDataFromReceiver(int x)
{
    ApiClient api = new ApiClient("XXXXXXXXXXXXXXXXXXX");
    Variable ldr = api.getVariable("XXXXXXXXXXXXXXXXXXX");
    Value[] values = ldr.getValues();

    int newValue = (int) values[0].getValue();

    if(newValue>22000){
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        MediaPlayer mp = MediaPlayer.create(Context.getContext(), notification);
        mp.start();
    }

    return new Point(x, newValue);
  }
}

Furthermore, when I close the app and open it again, it starts to plot the same data retrieved from Ubidots previously (when I ran the program while it was fetching data in real-time). How do I make the data permanent in the graph instead of having the graph plot it again when I open it? Should I be using AsyncTask instead of Thread in this case?

To make it clearer, it is this line which fetches the value:

int newValue = (int) values[0].getValue();

来源:https://stackoverflow.com/questions/48581812/real-time-graph-with-achartengine-chart-plotting-last-retrieved-value-from-clou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!