[英]Chart formatter shows the wrong date (MPAndroidChart)
I want to get 7 days of coin exchange data using Coingecko api and show it on a chart.我想使用 Coingecko api 获取 7 天的硬币交换数据并将其显示在图表上。 I used a formatter for this, but it always shows the same date as seen in the picture I shared below.
我为此使用了格式化程序,但它总是显示与我在下面分享的图片中看到的相同的日期。 When I test the code inside the formatter separately, there is no problem and they show the date information correctly.
当我单独测试格式化程序中的代码时,没有问题,并且它们正确显示了日期信息。
Coingecko Example API GET Request Coingecko 示例 API GET 请求
My codes:我的代码:
ChartData.java: ChartData.java:
@Data
public class ChartData {
private long timeStamp;
private double cost;
}
Filling Chart Data:填充图表数据:
List < Entry > lineList = new ArrayList < > ();
for (int i = 0; i < chartDataList.size(); i) {
if (i % 10 == 0) {
ChartData chartData = chartDataList.get(i);
lineList.add(new Entry(chartData.getTimeStamp(), (float) chartData.getCost()));
}
}
drawLineChart(lineList);
Drawline Method:画线法:
private void drawLineChart(List<Entry> lineList) {
Description description = new Description();
description.setText(trying);
lineChart.setDescription(description);
LineDataSet lineDataSet = new LineDataSet(lineList, "Test");
lineDataSet.setValueFormatter(new LineChartXAxisFormatter());
lineDataSet.setFillAlpha(110);
lineDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
lineDataSet.setDrawFilled(true);
lineDataSet.setFillColor(Color.GREEN);
LineData lineData = new LineData(lineDataSet);
lineChart.setData(lineData);
lineData.setValueTextSize(20f);
lineData.setValueTextColor(Color.BLACK);
lineChart.invalidate();
}
My Formatter (Note: I tried multiple emissionsMilliSince1970Time with 1000 and it's still same):我的格式化程序(注意:我用 1000 尝试了多次排放MilliSince1970Time,但它仍然相同):
public class LineChartXAxisFormatter extends IndexAxisValueFormatter {
@Override
public String getFormattedValue(float value) {
long emissionsMilliSince1970Time = ((long) value);
Date timeMilliseconds = new Date(emissionsMilliSince1970Time);
DateFormat dateTimeFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
return dateTimeFormat.format(timeMilliseconds);
}
}
For my separately tests: (timestamp value is: 1658044843993
:对于我的单独测试:(时间戳值为:
1658044843993
:
long emissionsMilliSince1970Time = chartData.getTimeStamp();
Date timeMilliseconds = new Date(emissionsMilliSince1970Time);
DateFormat dateTimeFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
String formattedDate = dateTimeFormat.format(timeMilliseconds);
Returned value by;返回值;
formattedDate = Jul 17, 2022
In summary I want it to display the date correctly but I don't know how.总之,我希望它正确显示日期,但我不知道如何。 It's just showing
Jan 1, 1970
.它只是显示
Jan 1, 1970
。
Here is a example response from service, first one timestamp and second one is price:这是来自服务的示例响应,第一个是时间戳,第二个是价格:
[
1658008855204,
369452.2869662426
],
[
1658012443271,
369353.01214527374
],
[
1658016032181,
368759.6501746092
],
[
1658019690976,
370378.3317357749
],
[
1658023239165,
367820.598299985
]
I solved the problem as a result of my own experiments.我通过自己的实验解决了这个问题。 Sometimes Y value was sent to the formatter side as I did not understand the reason.
有时 Y 值被发送到格式化程序端,因为我不明白原因。 This is the reason for this error.
这就是此错误的原因。
I removed this section:我删除了这一部分:
lineDataSet.setValueFormatter(new LineChartXAxisFormatter());
And added this instead:并添加了这个:
lineChart.getXAxis().setValueFormatter(new LineChartXAxisFormatter());
It's solved my problem.它解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.