当前位置: 首页 > 知识库问答 >
问题:

未正确解释时间序列数据

宇文金鑫
2023-03-14

我试图用时间序列创建一个简单的折线图。问题是androidplot没有正确显示数组中的时间值,即日期已关闭。日期应为:

2001年1月1日、2001年2月1日、2001年3月1日、2001年4月1日、2001年5月1日

但我得到了:

2001年1月1日、2001年1月30日、2001年3月1日、2001年3月31日、2001年5月1日

您可以从格式化的调试信息中看到androidplot解释了什么数据。我用JodaTime来表示时间。代码如下:

public class VisualizeFragment extends Fragment {

    public static final String TAG = Config.PRE + VisualizeFragment.class.getSimpleName();

    private XYPlot plot;

    public VisualizeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.simple_xy_plot_example, container, false);

        // initialize our XYPlot reference:
        plot = (XYPlot) v.findViewById(R.id.mySimpleXYPlot);

        // Create a couple arrays of y-values to plot:
        Number[] weights = {150, 180, 179, 170, 175};
        Number[] xyears = {
                978307200,  // 2001
                1009843200, // 2002
                1041379200, // 2003
                1072915200, // 2004
                1104537600  // 2005
        };
        Number[] years = {
                new DateTime(2001, 1, 1, 0, 0).getMillis() / 1000,
                new DateTime(2001, 2, 1, 0, 0).getMillis() / 1000,
                new DateTime(2001, 3, 1, 0, 0).getMillis() / 1000,
                new DateTime(2001, 4, 1, 0, 0).getMillis() / 1000,
                new DateTime(2001, 5, 1, 0, 0).getMillis() / 1000,
        };

        XYSeries series1 = new SimpleXYSeries(
                Arrays.asList(years),
                Arrays.asList(weights),
                "Weight");

        // Create a formatter to use for drawing a series using LineAndPointRenderer
        // and configure it from xml:
        LineAndPointFormatter series1Format = new LineAndPointFormatter();
        series1Format.setPointLabelFormatter(new PointLabelFormatter());
        series1Format.configure(getActivity().getApplicationContext(), R.xml.line_point_formatter_with_plf1);

        // add a new series' to the xyplot:
        plot.addSeries(series1, series1Format);

        // reduce the number of range labels
        //plot.setTicksPerRangeLabel(3);
        plot.getGraphWidget().setDomainLabelOrientation(-45);

        // draw a domain tick for each year:
        plot.setDomainStep(XYStepMode.SUBDIVIDE, years.length);


        plot.setDomainValueFormat(new Format() {

            // create a simple date format that draws on the year portion of our timestamp.
            // see http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
            // for a full description of SimpleDateFormat.
            private SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd");

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

                // because our timestamps are in seconds and SimpleDateFormat expects milliseconds
                // we multiply our timestamp by 1000:
                long timestamp = ((Number) obj).longValue() * 1000;
                Date date = new Date(timestamp);

                Log.d(TAG, "formatting date=" + new SimpleDateFormat("MMMM dd, yyyy").format(date));

                return dateFormat.format(date, toAppendTo, pos);
            }

            @Override
            public Object parseObject(String source, ParsePosition pos) {
                return null;

            }
        });


        return v;

    }


}

共有1个答案

邹锦
2023-03-14

我认为问题在于,您是均匀地细分您的域(XYStepMode. SUBDIVIDE),但因为您的xVals是划时代的值,它们不是均匀分布的;2001年1月1日至2001年2月1日之间的毫秒与2001年2月1日至2001年3月1日之间的毫秒不相同,这导致了差异。

我建议使用I作为域值,而不是历元值,然后使用I在域值格式代码中查找正确的历元值。这需要做两个改变。首先,使用i作为f(x):

// I've just removed the 'years' param here, which tells SimpleXYSeries
// to use an implicit value of 'i' for getY(i).
XYSeries series1 = new SimpleXYSeries(Arrays.asList(weights),"Weight");

然后将格式方法更改为:

@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
    int i = ((Number) obj).intValue();
    Date date = years[i];
    Log.d(TAG, "formatting date=" + new SimpleDateFormat("MMMM dd, yyyy").format(date));
    return dateFormat.format(date, toAppendTo, pos);
}
 类似资料:
  • 我正在尝试写入优先级队列。对于我的排队方法,我的逻辑是: 如果列表为空,则将事务添加到链接列表中的第一个节点 如果列表不为空,请将列表中事务对象的时间值与当前对象进行比较,如果对象的时间大于链表中的对象,则将对象插入当前索引。 否则,只需将它们添加到linkedlist的最后一个元素 我通过在方法中输入4个值来测试此方法,并相应地输入200020003000。 当我尝试从列表中除名时,它给我一个空

  • 我正在处理不同年表之间的动态日期格式转换,例如将(民国年表中的)110/02转换为(ISO年表中的)2021或相反。日期和模式都是在运行时给出的。 时间顺序转换要求时间为完整日期,否则抛出异常: DateTimeException:无法应用覆盖年表“Maguo”,因为正在格式化的时间对象包含日期字段但不代表整个日期 基于LocalDate(使用IsoChronology)的操作也失败: DateT

  • 我有一个自定义的POJO,我正在其上使用JOOQ.fetchInto(TestClassDto.class)映射数据库记录。我的POJO中的大多数字段与数据库表的列完全相似。然而,有一些是不同的,因此,我添加了java持久性,并使用在我的POJO上显式映射这些列,如这里所述。 不幸的是,如果我在几个特定字段上使用@Column,这是行不通的。只有用列注释的字段被映射,其余字段被忽略并设置为Null

  • 根据这里描述的内容,我应该使用JSR-310表示而不是数字表示来序列化对象。但是,我得到了数字表示。可能有什么问题? 我是这样配置我正在使用的映射器的: 这是我得到的反序列化示例 对于以下事例类:

  • 是寻求帮助的时候了。我已经学习D3.js好几个星期了,现在才开始觉得自己理解了它的10%(哈哈哈)。我正在尝试生成一个非常简单的线图。只要数据非常简单,我就能做到这一点,但是我的原始数据源有UTC时间戳,以及实数/十进制数字,这些数字总是使任何简单之外的东西崩溃。 原始数据源如下所示: 使用jQuery和javascript时间格式化函数,我可以组装以下简化的数据集: 下面是我的代码: 第一个错误