Skip to content

Realtime chart

Jonas Gehring edited this page Apr 12, 2019 · 2 revisions

Realtime updates / Live chart

It is possible to alter the data of the graph at runtime, so you can plot realtime data. To do so you have two important method in the Series subclasses:

  • resetData

    This method resets the whole data, so the current data will be replaced with the new.

  • appendData

    This methods adds a single data set to the current data. There's also a flag "scrollToEnd", that will scroll the GraphView automatically to the last X value.

See a full example:

public class RealtimeUpdates extends Fragment {
    private final Handler mHandler = new Handler();
    private Runnable mTimer1;
    private Runnable mTimer2;
    private LineGraphSeries<DataPoint> mSeries1;
    private LineGraphSeries<> mSeries2;
    private double graph2LastXValue = 5d;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main2, container, false);

        GraphView graph = (GraphView) rootView.findViewById(;
        mSeries1 = new LineGraphSeries<>(generateData());

        GraphView graph2 = (GraphView) rootView.findViewById(;
        mSeries2 = new LineGraphSeries<>();

        return rootView;

    public void onAttach(Activity activity) {
        ((MainActivity) activity).onSectionAttached(

    public void onResume() {
        mTimer1 = new Runnable() {
            public void run() {
                mHandler.postDelayed(this, 300);
        mHandler.postDelayed(mTimer1, 300);

        mTimer2 = new Runnable() {
            public void run() {
                graph2LastXValue += 1d;
                mSeries2.appendData(new DataPoint(graph2LastXValue, getRandom()), true, 40);
                mHandler.postDelayed(this, 200);
        mHandler.postDelayed(mTimer2, 1000);

    public void onPause() {

    private DataPoint[] generateData() {
        int count = 30;
        DataPoint[] values = new DataPoint[count];
        for (int i=0; i<count; i++) {
            double x = i;
            double f = mRand.nextDouble()*0.15+0.3;
            double y = Math.sin(i*f+2) + mRand.nextDouble()*0.3;
            DataPoint v = new DataPoint(x, y);
            values[i] = v;
        return values;

    double mLastRandom = 2;
    Random mRand = new Random();
    private double getRandom() {
        return mLastRandom += mRand.nextDouble()*0.5 - 0.25;
You can’t perform that action at this time.