MPAndroidChart LineChart 未正确绘制 [英] MPAndroidChart LineChart is not plotting correctly

查看:90
本文介绍了MPAndroidChart LineChart 未正确绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款记录心率并将该数据发送到我的 Android 应用的产品.该应用使用 MPAndroidChart 实时呈现这些数据.请注意,我使用的是最新版本的库.

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.

我在某些手机上遇到了一些问题.我已经在 Moto G2、Realme 1、OnePlus 5t、OnePlus 6、Lenovo K8 plus 上对其进行了测试.

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

OnePlus 6 手机上的图表(这是错误的):

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

Moto G2 手机上的图表(正确):

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

更新代码:

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

    }
}

这两种情况的代码、人员和设备都相同.我测试了很多次.我还检查了我发送到图表的数据.数据是正确的.图表只是没有正确绘制.如果您可能已经注意到,图表正在以模式绘制数据.它重复两点 3-5 次.我认为它只发生在好的或最新的手机上,比如 Realme、OnePlus.但我无法弄清楚为什么会发生这种情况.

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.

任何帮助将不胜感激.

推荐答案

我找到了解决方案.

我想明确说明这不是 MPAndroidChart 库中的错误.问题在于Android BLE.该应用程序以每秒 30 个的速率从设备读取文件(即图表的 125 个数据集).由于读取请求率很高,因此 BLE 之间跳过了很少的数据.

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.

这篇关于MPAndroidChart LineChart 未正确绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆