【问题标题】:AndroidPlot: How i can change the domain data?AndroidPlot:如何更改域数据?
【发布时间】:2011-10-10 14:56:35
【问题描述】:

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

更具体地说,这一行:

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

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

SimpleDateFormat("dd-MM-yyyy")

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

有人知道我可以如何更改该日期吗?还是使用其他格式(如字符串)?

【问题讨论】:

    标签: android charts androidplot android-graphview


    【解决方案1】:

    迟到的答案,但可能对其他人有用。

    我也很难解决这个问题,尤其是因为我是 Java 初学者。 我实现了一个自定义格式化程序。似乎有点丑陋的解决方案,但它的工作原理。思路如下:

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

    这里有一些代码sn-ps。

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

    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]); 
            }
    

    这是我将它附加到情节的方式:

    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 值,您可能会得到奇怪的结果,我已经就这个问题寻求过帮助。

    【讨论】:

    • @Martin 关于水平线问题有什么说法吗?对于那些不知道的人,如果你绘制一条水平线(比如一个绘图的 y 值全为 0),该绘图将不会显示在图表上,但如果你有另一个不是水平线的绘图,则水平线会出现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多