【问题标题】:Custom XAxis labels with MPAndroidChart使用 MPAndroidChart 自定义 XAxis 标签
【发布时间】:2018-01-09 14:37:32
【问题描述】:

我是 MPAndroidChart 的新手,我想在 LineChart 的 XAxis 上实时显示时间。我只想显示传入数据的最后 10 秒,如下图所示。我的采样频率为 25Hz,因此我需要显示 250 个值才能进行 10 秒的记录。

但是,我真的不知道该怎么做。我想我必须使用IAxisValueFormatter

目前,我的传入值是这样添加到数据集的:

addEntry(myDataSet, new Entry(myDataSet.getEntryCount(), myNewValue));

但也许我需要这样做:

/* add 40 ms on xAxis for each new value */
addEntry(myDataSet, new Entry(myLastTimeStamp + 40, myNewValue));

然后创建一个格式化程序,将 X 值转换为“xxx 秒”之类的字符串并仅显示“0s”、“5s”和“10s”。

我不知道它是否有效,但有更好的方法吗?

谢谢

【问题讨论】:

    标签: android real-time mpandroidchart linechart


    【解决方案1】:

    所以我在这里看到两个问题。
    1. 您需要在 10 秒内绘制 250 个值。
    2.正确格式化X轴。
    所以第一个问题的解决方案,你必须在 10 秒内显示 250 个值。因此,您的 x 轴实际上将有 250 个数据点,因为您正在这样做:

    addEntry(myDataSet, new Entry(myDataSet.getEntryCount(), myNewValue));

    所以在 1 秒内你将获得 25 分。

    现在有了所有这些数据,让我们在 XAxis 上工作。

    xAxis = chart.getXAxis();
    xAxis.setAxisMinimum(0);
    xAxis.setAxisMaximum(250); // because there are 250 data points
    xAxis.setLabelCount(3); // if you want to display 0, 5 and 10s which are 3 values then put 3 else whatever of your choice.
    xAxis.setValueFormatter(new MyFormatter());
    

    你的格式化程序必须看起来像这样:

    public class MyFormatter implements IAxisValueFormatter {
    @Override
    public String getFormattedValue(float value, AxisBase axis) {
    //Modify this as per your needs. If you need 3 values like 0s, 5s and 10s then do this.
    //0 mod 125 = 0 Corresponds to 0th second
    //125 mod 125 = 0 Corresponds to 5th second
    //250 mod 125 = 0 Corresponds to 10th second
        if(value % 125 == 0){
           int second = (int) value / 25; // get second from value
           return second + "s" //make it a string and return
        }else{
           return ""; // return empty for other values where you don't want to print anything on the X Axis
        }
    }
    

    希望这能清除一切。干杯!

    【讨论】:

    • 我试过了,但它不适用于实时图表...没有显示任何内容,所有值都为 0。
    • 您的 getEntryCount() 方法是否每次都返回正确的值而不是 0 ?这是我看到一切都在 0 的唯一原因。
    • 嗨@Pavan,嗨,我已经发布了关于 XAxis 标签问题的类似 Bounty 问题,请您尝试回答stackoverflow.com/questions/55325511/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多