AndroidPlot:我如何更改域数据?

发布于 2024-11-25 16:14:17 字数 358 浏览 3 评论 0原文

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

更具体地说,这一行:

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

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

SimpleDateFormat("dd-MM-yyyy")

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

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

I use actually the AndroidPlot library to use a simple chart in my android project but I don't know how i can change the values of domain zone.

More specific, this line:

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

In the web site said that i can use another formats and its true, but if I use for example:

SimpleDateFormat("dd-MM-yyyy")

In the chart appears "31-12-1969" in all domain values.

Somebody know how I can change that date? or use another format (like String)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水波映月 2024-12-02 16:14:17

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

我在这个问题上也遇到了困难,尤其是因为我是 Java 初学者。
我实现了一个自定义格式化程序。看起来有点丑陋的解决方案,但它有效。想法如下:

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

以下是一些代码片段。

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

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

Late answer, but maybe will be useful to other people.

I also had a hard time with this issue, especially since I am Java beginner.
I implemented a custom formatter. Seems a bit ugly solution but it works. Idea is the following:

  • take only Y axis for values, and leave X axis as indexes into an array (as Androidplot tutorial suggests, use using ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value)
  • each time your data changes, you need to pass to the Plot a new formatter with your new data for X axis

Here are some code snippets.

First is the class which transforms array index to a custom label String:

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

And here is how I attach it to the 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); 

This trick works also for dynamic updates.

NOTICE: Androidplot seems to have problems with drawing horizontal lines, so if your data has the same Y values, you might get strange results, I already asked for help on this issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文