英文:
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("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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论