Search code examples
androidplot

AndroidPlot FixedSizeEditableXYSeries How to use


Im new to android and very new to android plot. Can you point me to an example that uses FixedSizeEditableXYSeries?

My goal is to create a streaming plot that shows the latest sensor readings in an android app.

Thanks

===================Update - following discussion with @Nick====================

public class MainActivity extends AppCompatActivity {


    // Create the redrawer so that the plot is updated
    private Redrawer redrawer;

    // create the message receiver - data is received via broadcasts
    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Get extra data included in the Intent
            String message = intent.getStringExtra("CurrentHR");

            Log.d("ReceivedHR ",message);

            // Now put the new data point at the end of the FixedSizeEditableXYSeries, move all data points by 1.
            for (int index=0;index<9;index++){

                if(index<9){
                    hrHistory.setY(hrHistory.getY(index+1),index);
                }else{
                    hrHistory.setY(Float.parseFloat(message),9);
                }
            }


        }
    };

    // create a few references
    private XYPlot xyPlot;
    private FixedSizeEditableXYSeries hrHistory;

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

        // Now find the plot views
        xyPlot = (XYPlot)findViewById(R.id.xyPlot);

        // Declare the local broadcast manager
        LocalBroadcastManager.getInstance(this).registerReceiver(
                mMessageReceiver, new IntentFilter("hrUpdate"));


        // now put in some data
        hrHistory = new FixedSizeEditableXYSeries("HR",10);

        xyPlot.addSeries(hrHistory, new LineAndPointFormatter(Color.GREEN,Color.RED,null,null));
        xyPlot.setRangeBoundaries(40, 120, BoundaryMode.FIXED);
        xyPlot.setDomainBoundaries(0, 20, BoundaryMode.FIXED);
    }

    @Override
    protected void onResume(){
        super.onResume();


        // set a redraw rate of 1hz and start immediately:
        redrawer = new Redrawer(xyPlot, 1, true);
    }
}

This gives me a nice graph but no line. It doesnt look like the plot is being updates as new data is filling the FixedSizeEditableXYSeries.


Solution

  • If you want scrolling behavior then FixedSizeEditableXYSeries would be the wrong choice; as your data scrolls you're essentially enqueueing the newest value and dequeuing the oldest value; a linked list type structure would be a better choice.

    You can either implement XYSeries and back it with any suitable data structure you prefer, or you can use SimpleXYSeries, which already supports queue operations a la removeFirst() and addLast(...). There's a great example of of a dynamic scrolling plot in the demo app: OrientationSensorExampleActivity. Lines 235-245 show the specific actions mentioned above.