AndroidPlot:我如何更改域数据?

标签 android charts androidplot android-graphview

我实际上使用 AndroidPlot 库在我的 android 项目中使用一个简单的图表,但我不知道如何更改域区域的值。

更具体地说,这一行:

mySimpleXYPlot.setDomainValueFormat(new DecimalFormat("#"));

在网站上说我可以使用其他格式,这是真的,但是如果我使用例如:

SimpleDateFormat("dd-MM-yyyy")

在图表中,所有域值中均出现 “31-12-1969”

有人知道我怎样才能更改那个日期吗?还是使用其他格式(如字符串)?

最佳答案

迟到的答案,但也许对其他人有用。

我也很难解决这个问题,特别是因为我是 Java 初学者。 我实现了一个自定义格式化程序。看起来有点难看的解决方案,但它确实有效。思路如下:

  • 仅采用 Y 轴作为值,并将 X 轴作为数组的索引(如 Androidplot 教程所建议的那样,使用 ArrayFormat.Y_VALS_ONLY,//Y_VALS_ONLY 表示使用元素索引作为 x 值)
  • 每次您的数据更改时,您都需要向 Plot 传递一个新的格式化程序,其中包含 X 轴的新数据

这里是一些代码 fragment 。

首先是将数组索引转换为自定义标签字符串的类:

public class MyIndexFormat extends Format {

    public String[] Labels = null;

        @Override
        public StringBuffer format(Object obj, 
                StringBuffer toAppendTo, 
                FieldPosition pos) {

            // try turning value to index because it comes from indexes
            // but if is too far from index, ignore it - it is a tick between indexes
            float fl = ((Number)obj).floatValue();
            int index = Math.round(fl);
            if(Labels == null || Labels.length <= index ||
                    Math.abs(fl - index) > 0.1)
                return new StringBuffer("");    

            return new StringBuffer(Labels[index]); 
        }

下面是我如何将它附加到 Plot 上:

MyIndexFormat mif = new MyIndexFormat ();
mif.Labels = // TODO: fill the array with your custom labels

// attach index->string formatter to the plot instance
pricesPlot.getGraphWidget().setDomainValueFormat(mif); 

这个技巧也适用于动态更新。

注意:Androidplot 在绘制水平线时似乎有问题,因此如果您的数据具有相同的 Y 值,您可能会得到奇怪的结果,我已经就此问题寻求帮助。

关于AndroidPlot:我如何更改域数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6783174/

相关文章:

android - 扫描 NFC 标签时是否可以启动应用程序?

ios - 导入 Swift 图表

java - 在 JFreeChart 中制作自定义环形图

android - ProGuard 破坏了我的 Androidplot 布局

java - android项目添加library后,xml标签还是 "unallowed in Android resource files"

android - Android 如何管理其资源?

android - 如何在 React Native 中删除 View

android - GCM 服务器以什么频率刷新注册 ID 以及如何在我的手机中获取 regId 更改事件?

javascript - 谷歌应用可视化可以吗?具有缩放选项的时间轴

android - 使用 AndroidPlot 在图形上自定义点