如何使用 DatePicker 元素中的日期作为 MPAndroidChart 上的 x 轴值?
How can I use dates from a DatePicker element to be the x-axis values on a MPAndroidChart?
我正在尝试使用 MPAndroidChart 创建折线图,其中用户使用 DatePicker 输入日期(今天或前一天),然后为该日期输入单独的值。我遇到的问题是如何从 DatePicker 格式化该日期以将其保存在 SQLite 数据库中,然后将其取出并将其用于我的图形的 x 轴。我已经尝试了我在此处和文档中看到的所有不同方法,但它们似乎更多地用于实时数据,而不是用于使用前几天的图表。
我几乎找不到解决这个问题的最佳方法。如有任何帮助或建议,我们将不胜感激。
使用自定义值格式化程序
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 "";
}
}
}
并设置 xaxis 值格式化程序
chart.getXAxis().setValueFormatter(new DateValueFormatter(datelist));
我正在尝试使用 MPAndroidChart 创建折线图,其中用户使用 DatePicker 输入日期(今天或前一天),然后为该日期输入单独的值。我遇到的问题是如何从 DatePicker 格式化该日期以将其保存在 SQLite 数据库中,然后将其取出并将其用于我的图形的 x 轴。我已经尝试了我在此处和文档中看到的所有不同方法,但它们似乎更多地用于实时数据,而不是用于使用前几天的图表。
我几乎找不到解决这个问题的最佳方法。如有任何帮助或建议,我们将不胜感激。
使用自定义值格式化程序
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 "";
}
}
}
并设置 xaxis 值格式化程序
chart.getXAxis().setValueFormatter(new DateValueFormatter(datelist));