简体   繁体   English

MPAndroidChart LineChart无法正确绘制

[英]MPAndroidChart LineChart is not plotting correctly

I am working on a product which records heart rate and sends that data to my Android app. 我正在开发一种记录心率并将该数据发送到我的Android应用程序的产品。 The app presents this data using MPAndroidChart in real time . 该应用程序使用MPAndroidChart 实时显示此数据。 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. 我已经在Moto G2,Realme 1,OnePlus 5t,OnePlus 6,联想K8 plus上进行了测试。

The chart on OnePlus 6 phone (This is wrong): OnePlus 6手机上的图表(这是错误的): 在此处输入图片说明

The chart on Moto G2 phone (This is correct): Moto G2手机上的图表(正确): 在此处输入图片说明

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. 它重复3-5次两次。 I think it is only happening on good or the latest phones like Realme, OnePlus. 我认为这只会在优质或最新的手机(如Realme,OnePlus)上发生。 But I am not able to figure out why it is happening. 但是我不知道为什么会这样。

Any help would be appreciated. 任何帮助,将不胜感激。

I found the solution. 我找到了解决方案。

I would like to make it clear that it is not a bug in MPAndroidChart library. 我想说清楚,这不是MPAndroidChart库中的错误。 The problem lies in the Android BLE. 问题出在Android BLE。 The app is reading files from a device at a rate of 30 per seconds (ie 125 datasets for the graph). 该应用程序正在以每秒30的速度(即图表的125个数据集)从设备读取文件。 Since the read request rate is high, BLE was skipping few data in between. 由于读取请求率很高,因此BLE在这之间跳过了很少的数据。

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. 这就是为什么图形按问题所示进行绘制的原因。

After this line: 在此行之后:

data.notifyDataChanged();

Add following: 添加以下内容:

lineChartHeart.setData(data);

After this line: 在此行之后:

lineChartHeart.notifyDataSetChanged();

Add following line: 添加以下行:

lineChartHeart.invalidate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM