0

How can I put a Date in the axis x in LineChart with MPAndroidChart? Now I'm convert the Date (in milliseconds) to string, but when i try to put is in the chart the x values don't display.

        LineDataSet dataSet = new LineDataSet(entries, getString(R.string.NDVI_trend));
    LineData lineData = new LineData(dataSet);

    chart.setData(lineData);
    Description description = new Description();
    description.setText("");
    chart.setDescription(description);
    chart.getAxisRight().setEnabled(false);
    chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
    chart.getXAxis().setDrawGridLines(false);
    chart.getLegend().setEnabled(false);

    String[] dateString = new String[entries.size() +1];
    for (int i = 0; i < entries.size(); i++){
        dateString[i] = getDate((long)entries.get(i).getX(),"dd MMM yyyy");
    }

    IAxisValueFormatter axisValueFormatter = new IndexAxisValueFormatter(dateString);
    chart.getXAxis().setValueFormatter(axisValueFormatter);
    chart.invalidate();

The getData method:

public static String getDate(long milliSeconds, String dateFormat)
{
    SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(milliSeconds);
    return formatter.format(calendar.getTime());
}
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
Davide
  • 127
  • 1
  • 2
  • 12

1 Answers1

1

If you know how to convert your float x-values into a String representation of a Date you can use a IAxisValueFormatter:

mChart.getXAxis().setValueFormatter(new IAxisValueFormatter() {
        private SimpleDateFormat mFormat = new SimpleDateFormat("dd MMM HH:mm");

        @Override
        public String getFormattedValue(float value, AxisBase axis) {

            long millis = (long) value;
            return mFormat.format(new Date(millis));
        }
    });
chart.getXAxis().setValueFormatter(axisValueFormatter);
chart.setGranularity(1.0f);
chart.setGranularityEnabled(true);
David Rawson
  • 20,912
  • 7
  • 88
  • 124
  • Related but out of date: http://stackoverflow.com/questions/32292466/mpandroidchart-linechart-using-dates-instead-of-strings-for-x-axis – David Rawson May 09 '17 at 06:55
  • i tried but the x values aren't display. the conversion is ok `String[] dateString = new String[entries.size()]; for (int i = 0; i < entries.size(); i++){ dateString[i] = getDate((long)entries.get(i).getX(),"dd MMM yyyy"); } IAxisValueFormatter axisValueFormatter = new IndexAxisValueFormatter(dateString); chart.getXAxis().setValueFormatter(axisValueFormatter); chart.invalidate();` – Davide May 09 '17 at 08:10
  • @Davide maybe try commenting out those lines with the granularity. Otherwise, you can edit your post to include a [mcve] and I'll have a look tomorrow – David Rawson May 09 '17 at 09:02
  • @Davide what values are you using? I mean what data? – David Rawson May 10 '17 at 08:25
  • The date value in milliseconds are like this: 1465084800000, 1465344000000, 1466208000000 – Davide May 10 '17 at 08:28
  • @Davide okay I'll check it out – David Rawson May 10 '17 at 08:29
  • @Davide what's in the getDate() method? – David Rawson May 10 '17 at 08:41
  • is the method that convert milliseconds in string, i added this method in my question – Davide May 10 '17 at 08:42
  • @Davide I updated my answer - turns out it was impossible to use strings like I suggested :/ you'll probably have to tweak the logic inside `getFormattedValue()` - I don't know enough about your dataset to configure it for you – David Rawson May 10 '17 at 09:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143852/discussion-between-davide-and-david-rawson). – Davide May 10 '17 at 09:08
  • @Davide did you click on chat? I can't see any replies there – David Rawson May 10 '17 at 09:18