MPAndroidChart LineChart is not plotting correctly

偶尔善良 提交于 2021-02-08 08:00:25

问题


I am working on a product which records heart rate and sends that data to my Android app. The app presents this data using MPAndroidChart in real time. Note that, I am using the latest version of the library.

I am facing some issue in some phones. I have tested it on Moto G2, Realme 1, OnePlus 5t, OnePlus 6, Lenovo K8 plus.

The chart on OnePlus 6 phone (This is wrong):

The chart on Moto G2 phone (This is correct):

Update Code:

 private void initHeartLineChart(){

    lineChartHeart.getDescription().setEnabled(false);
    lineChartHeart.getAxisRight().setEnabled(false);
    lineChartHeart.getLegend().setEnabled(false);
    lineChartHeart.setDrawGridBackground(false);
    lineChartHeart.setPinchZoom(false);
    lineChartHeart.setScaleEnabled(false);
    lineChartHeart.setDoubleTapToZoomEnabled(false);
    lineChartHeart.setScaleYEnabled(false);
    lineChartHeart.setDragXEnabled(false);
    lineChartHeart.setDragYEnabled(false);

    XAxis xAxis = lineChartHeart.getXAxis();
    xAxis.setEnabled(false);
    xAxis.setDrawGridLines(false);
    xAxis.setGranularity(1f);

    YAxis yAxisHeart = lineChartHeart.getAxisLeft();
    yAxisHeart.setEnabled(false);
    yAxisHeart.setAxisMaximum(600f);
    yAxisHeart.setAxisMinimum(-600f);
    yAxisHeart.setDrawAxisLine(false);
    yAxisHeart.setDrawZeroLine(false);

    //add empty data
    lineChartHeart.setData(new LineData());
    lineChartHeart.setViewPortOffsets(0,0,0,0);
}

private LineDataSet createHeartDataSet() {

    LineDataSet set = new LineDataSet(null, "Live Heart");
    set.setAxisDependency(YAxis.AxisDependency.LEFT);
    set.setColor(getResources().getColor(R.color.heart_color));
    set.setLineWidth(1f);
    set.setDrawCircles(false);
    set.setHighlightEnabled(false);
    set.setDrawValues(false);
    set.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
    set.setCubicIntensity(0.2f);
    return set;
}


private void addNewHeartEntry(double heartRate) {

    LineData data = lineChartHeart.getData();

    if (data != null) {

        ILineDataSet set = data.getDataSetByIndex(0);

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

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

        data.notifyDataChanged();

        lineChartHeart.notifyDataSetChanged();
        lineChartHeart.setVisibleXRangeMaximum(625);

        // move to the latest entry
        lineChartHeart.moveViewToX(set.getEntryCount());

    }
}

Code, person, and device are the same for both the cases. I tested it many times. I also checked the data which I was sending to the chart. The data was correct. The chart is just not plotting it right. If you may have noticed, the chart is plotting data in a pattern. It is repeating two points 3-5 times. I think it is only happening on good or the latest phones like Realme, OnePlus. But I am not able to figure out why it is happening.

Any help would be appreciated.


回答1:


I found the solution.

I would like to make it clear that it is not a bug in MPAndroidChart library. The problem lies in the Android BLE. The app is reading files from a device at a rate of 30 per seconds (i.e. 125 datasets for the graph). Since the read request rate is high, BLE was skipping few data in between.

Since premium phones have the faster processing power, previous data was getting added in the list. That's why the graph was plotting as shown in the question.




回答2:


After this line:

data.notifyDataChanged();

Add following:

lineChartHeart.setData(data);

After this line:

lineChartHeart.notifyDataSetChanged();

Add following line:

lineChartHeart.invalidate();


来源:https://stackoverflow.com/questions/52033392/mpandroidchart-linechart-is-not-plotting-correctly

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