如何将DatePicker元素中选择的日期用作MPAndroidChart上的x轴数值?

huangapple go评论86阅读模式
英文:

How can I use dates from a DatePicker element to be the x-axis values on a MPAndroidChart?

问题

我正在尝试使用MPAndroidChart创建折线图,用户使用DatePicker输入日期(可以是今天或以前的某天),然后为该日期输入单独的值。我遇到的问题是如何将DatePicker的日期格式化,以便将其保存在SQLite数据库中,然后将其提取出来并用于我的图表的x轴。我已经尝试了所有我在这里和文档中看到的不同方法,但它们似乎更适用于实时数据,而不是使用以前日期的图表。

我对如何最好地处理这个问题感到非常困惑。任何帮助或建议都将不胜感激。

英文:

I am attempting to create a line graph using MPAndroidChart where the user enters a date (either today or a previous day) using a DatePicker and then enters a separate value for that date. The issue I am having is how to format that date from the DatePicker for saving it on a SQLite database, and then to take it out and use it for the x-axis of my graph. I have tried all of the different methods I have seen here and on the documentation, but they seem to be more for real-time data and not for graphs using previous days.

I am pretty much lost of the best way to go about this. Any help or advice would be apprecaited.

答案1

得分: 1

use custom valueformatter

class DateValueFormatter extends ValueFormatter {
    private SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    private List<Date> dateList;

    public DateValueFormatter(List<Date> dateList) {
        this.dateList = dateList;
    }

    @Override
    public String getAxisLabel(float value, AxisBase axis) {
        int axisValue = (int) value;
        if (axisValue >= 0 && axisValue < dateList.size()) {
            return dateFormat.format(dateList.get(axisValue));
        } else {
            return "";
        }
    }
}


and set xaxis value formatter

chart.getXAxis().setValueFormatter(new DateValueFormatter(datelist));
英文:

use custom valueformatter

class DateValueFormatter extends ValueFormatter {
    private SimpleDateFormat dateFormat = new SimpleDateFormat(&quot;dd-MM-yyyy&quot;);
    private List&lt;Date&gt; dateList;

    public DateValueFormatter(List&lt;Date&gt; dateList) {
        this.dateList = dateList;
    }

    @Override
    public String getAxisLabel(float value, AxisBase axis) {
        int axisValue = (int) value;
        if (axisValue &gt;= 0 &amp;&amp; axisValue &lt; dateList.size()) {
            return dateFormat.format(dateList.get(axisValue));
        } else {
            return &quot;&quot;;
        }
    }
}

and set xaxis value formatter

chart.getXAxis().setValueFormatter(new DateValueFormatter(datelist));

huangapple
  • 本文由 发表于 2020年8月21日 09:53:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63515418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定