Chart formatter shows the wrong date (MPAndroidChart)

Viewed 43

I want to get 7 days of coin exchange data using Coingecko api and show it on a chart. 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

My Error: Just showing Jan 1, 1970

My codes:

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):

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:

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.

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
]
1 Answers

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. 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.

Related