如何制作实时多线图?

时间:2015-09-22 01:48:09

标签: android real-time mpandroidchart

我想使用MPAndroidChart制作实时多线图。

仅使用一个数据制作实时图表是没有问题的。 (以下代码)

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

并且,使用以下代码制作多个折线图没有问题。

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

但是,我不知道如何制作多个实时线图图。

如何制作多个实时折线图?

3 个答案:

答案 0 :(得分:4)

您确实在问题中发布了问题的答案。

这一行是关键:

data.addEntry(... , 0);

最后的0指定应插入条目的数据集索引。

所以你需要做的就是创建与你想要的行一样多的空DataSets,然后使用上面提到的Entries添加到你想要的任何DataSet具有正确索引的方法。

Here您可以找到动态和实时数据的官方维基条目。

答案 1 :(得分:2)

private void addEntry(int min, int max) {
    data = mChart.getData();
    if (data != null) {
        LineDataSet dataSetGraphA = data.getDataSetByIndex(0);
        LineDataSet dataSetGraphB = data.getDataSetByIndex(1);
        LineDataSet dataSetGraphC = data.getDataSetByIndex(2);
        LineDataSet dataSetGraphD = data.getDataSetByIndex(3);
        // LineDataSet set1= data.getDataSetByIndex(1);
        // set.addEntry(...); // can be called as well
        if (dataSetGraphA == null) {
            dataSetGraphA = setLineDataSet(0);
            data.addDataSet(dataSetGraphA);
        }
        if (dataSetGraphB == null) {
            dataSetGraphB = setLineDataSet(1);
            data.addDataSet(dataSetGraphB);
        }
        if (dataSetGraphC == null) {
            dataSetGraphC = setLineDataSet(2);
            data.addDataSet(dataSetGraphC);
        }
        if (dataSetGraphD == null) {
            dataSetGraphD = setLineDataSet(3);
            data.addDataSet(dataSetGraphD);
        }
        Calendar c = Calendar.getInstance();

        // yyyy-MM-dd.HH.mm.ss.SS
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.SS");
        String formattedDate = df.format(c.getTime());
        // Now formattedDate have current date/time
        System.out.println("Current time = " + formattedDate);
        Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
        // add a new x-value first
        data.addXValue(formattedDate);
        data.addEntry(
                new Entry(randomInRange(min, max), dataSetGraphA
                        .getEntryCount()), 0);
        data.addEntry(
                new Entry(randomInRange(min, max), dataSetGraphB
                        .getEntryCount()), 1);
        data.addEntry(
                new Entry(randomInRange(min, max), dataSetGraphC
                        .getEntryCount()), 2);
        data.addEntry(
                new Entry(randomInRange(min, max), dataSetGraphD
                        .getEntryCount()), 3);

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

        mChart.setVisibleXRangeMaximum(6);
        mChart.setVisibleYRangeMaximum(15, AxisDependency.LEFT);
        //
        // // this automatically refreshes the chart (calls invalidate())
        mChart.moveViewTo(data.getXValCount() - 7, 50f, AxisDependency.LEFT);
    }
}

答案 2 :(得分:0)

这是一个简单的例子:

def users_to_follow(request):
    all_users = User.objects.exclude(following__follower=request.user)
    return render(request,"auths/users_to_follow.html",locals())