Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make real-time multiple line graph?

I want to make real-time multiple linechart using MPAndroidChart.

It is no problem to make real-time graph using only one data. (following code)

private void addEntry(int count) {
    LineData data = mChart.getData();

    if (data != null) {
        LineDataSet set = data.getDataSetByIndex(0);

        if (set == null) {
            set = createSet();
            data.addDataSet(set);
        }

        data.addXValue("");

        data.addEntry(new Entry(getPressure(), set.getEntryCount()), 0);

        data.setDrawValues(false);
        data.setHighlightEnabled(false);

        // let the chart know it's data has changed
        mChart.notifyDataSetChanged();

        // limit the number of visible entries
        mChart.setVisibleXRange(0, count);

        // move to the latest entry
        mChart.moveViewToX(data.getXValCount() - (count + 1));
    }
}

And, it is no problem to make multiple line chart using following code.

private void setData(int count, float range) {
    ArrayList<String> xValues = new ArrayList<String>();
    for (int i = 0 ; i < count ; i++) {
        xValues.add((1 + i) + "");
    }

    ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();

    for (int k = 0 ; k < 3 ; k++) {
        ArrayList<Entry> yValues = new ArrayList<Entry>();

        for (int i = 0 ; i < count ; i++) {
            if (k == 0) {
                yValues.add(new Entry(getSetPressure(), i));
            }
            else if (k == 1) {
                yValues.add(new Entry(getCurrentPressure(), i));
            }
            else {
                yValues.add(new Entry(getSuctionPressure(), i));
            }
        }

        String s;
        String c;
        if (k == 0) {
            s = "Set Pressure";
            c = "#ed1f24";
        }
        else if (k == 1) {
            s = "Current Pressure";
            c = "#004bf6";
        }
        else {
            s = "Suction Pressure";
            c = "#ffba00";
        }

        LineDataSet set = new LineDataSet(yValues, s);

        set.setAxisDependency(YAxis.AxisDependency.LEFT);
        set.setDrawCubic(false);
        set.setDrawCircles(false);
        set.setCircleColor(Color.parseColor(c));
        set.setCircleSize(8f);
        set.setCircleColorHole(Color.BLACK);
        set.setDrawCircleHole(false);
        set.setLineWidth(3f);
        set.setColor(Color.parseColor(c));
        set.setDrawHorizontalHighlightIndicator(false);
        set.setDrawVerticalHighlightIndicator(false);

        dataSets.add(set);
    }

    LineData data = new LineData(xValues, dataSets);

    data.setDrawValues(false);
    data.setHighlightEnabled(false);

    mChart.setData(data);
}

However, I do not know how to make multiple real-time linechart graph.

How can I make multiple real-time line chart?

like image 825
mooore Avatar asked Sep 22 '15 01:09

mooore


Video Answer


1 Answers

You actually posted the answer to your question in your question.

This line is the key:

data.addEntry(... , 0);

The 0 at the end specifies the dataset-index where the entry should be inserted.

So what you need to do is simply create as many empty DataSets as lines you want to have, and thenn add your Entries to whatever DataSet you want by using the above mentioned method with the correct index.

Here you can find the official wiki entry for dynamic- and realtime-data.

like image 129
Philipp Jahoda Avatar answered Sep 21 '22 08:09

Philipp Jahoda