MPAndroidChart x 日期值重复问题 [英] MPAndroidChart x date value duplication problem

查看:64
本文介绍了MPAndroidChart x 日期值重复问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现将锻炼时间按日期的变化显示为图表的功能.

I am implementing the function to display the change of exercise time by date as a graph.

但是出了点问题.

在说明问题之前,先简单说明一下我的代码.

Before I explain the problem, I will briefly explain my code.

当用户输入日期时,该日期存储在数据库中并按日期输出.

When a date is input from the user, the date is stored in the database and output by date.

例如,当用户输入2020/06/26时,在图表上显示为06/26.

For example, when the user enters 2020/06/26, it is displayed as 06/26 on the graph.

现在我来解释这个问题.

Now I will explain the problem.

图形的 x 值重叠.07/01 不会在 06/30 之后立即出现,而是很远.

The x value of the graph is overlapping. 07/01 does not appear immediately after 06/30, but very far.

我会附上我的代码和执行结果.在此处输入图片描述

I will attach my code and execution result. enter image description here

xAxis.setValueFormatter(new ValueFormatter() {

        @Override
        public String getFormattedValue(float value) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMdd");
            Date date = null;
            try {
                date = simpleDateFormat.parse(Float.toString(value));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            SimpleDateFormat newFormat = new SimpleDateFormat("MM/dd");
            String dateString = newFormat.format(date);
            return dateString;
        }

    });

推荐答案

从您提供的代码中很难判断.但最有可能的是,问题不在于 ValueFormatter,而在于您使用的实际 x 值.yyMMdd 格式的 x 值很可能不是您想要的,因为例如2020-04-01 和 2020-04-02 与 2020-03-31 和 2020-04-01 之间的事件不同,事件是否应该完全相同(1 天).您应该对 x 值使用另一种表示形式,例如自 1970 年以来的天数".

Hard to tell from the code you provide. But most probably, the problem does not lie in the ValueFormatter, but in the actual x values you use. Having x values in the format yyMMdd ist most likely not what you want, because the difference between e.g. 2020-04-01 and 2020-04-02 is not the same as between 2020-03-31 and 2020-04-01, event if it should be exactly the same (1 day). You should use another representation for the x values, e.g. "days since 1970".

然而,这仍然不能解释为什么在 07-01 之后甚至在 08-19 之后显示 06-30.我的猜测是您的条目列表没有按升序正确排序.

This still does not explain why 06-30 is displayed after 07-01 and even after 08-19, however. My guess is that your Entry list is not sorted properly in ascending order.

这篇关于MPAndroidChart x 日期值重复问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆