Formatting Dates on x-axis in MPAndroidChart

2.7k Views Asked by At

I'm using MPAndroidChart and I know this topic is covered quite a bit in the docs, so apologies if I've missed something obvious, but I'm having a bit of trouble formatting timestamp values into dates on the x-axis of my Linechart.

It appears that getFormattedValue() isn't being invoked for my x-axis value for whatever reason. If i debug AxisBase, it only seems to looking to format the y-axis values, which i do not have a custom formatter set on.

Here is the code snippets, taken mostly from the Github docs:

          LineChart chart = (LineChart) findViewById(R.id.chart);
          XAxis xAxis = chart.getXAxis();

          // set min timestamp to Jul 2017 
          HourAxisValueFormatter(Long.parseLong("1499814559"));

          xAxis.setValueFormatter(new IAxisValueFormatter() {
             // THIS IS NEVER INVOKED!
            @Override
            public String getFormattedValue(float value, AxisBase axis) 
            {
                return new Date(Long.parseLong(""+value)).toString();
            }
        });

        // rating is a map of timestamp to numeric value, both stored 
        // as strings
        for (Map.Entry<String, String> rating : ratings.entrySet()) {
            String timestamp = rating.getKey();
            String ratingValue = rating.getValue();
            entries.add(new Entry(Float.parseFloat(timestamp), 
            Float.parseFloat(ratingValue)));
        }

            // add entries to dataset
            LineDataSet dataSet = new LineDataSet(entries, "Ratings");
            LineData lineData = new LineData(dataSet);
            chart.setData(lineData);
            chart.invalidate(); // refresh'

I'm using version 3.0.2 of the library:

compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'

Thanks in advance, Gary

EDIT per request for desired output/actual output: Ideally i would have a series of dates along the x-axis , e.g. 10/7, 11/8, 12/8, etc. constructed from the Epoch values I've provided. I know the code above wont format it in that format as it stands, but just want to get it formatting to any date value initially (so ignore that for now), the crux of the issue is that my formatter isn't getting invoked at all.

Below is a screenshot of the current output, it only prints a single value on the x=0 line (note even though there are multiple y values being provided, i wonder if they are all on the same x=0 line does it just draw the last point or something?):

enter image description here

Debugging the timestamp values above, Float.parseFloat(timestamp) is equal to 1.50516885e^12. The actual timestamp value for example is 1505168846751, but after parsing to a Float is rounded, and given an exponent value.

EDIT 2 :

Screen grab of the values supplied as 'entries' to the LineDataSet:

enter image description here

EDIT 3: The issue appears to be to do with the size/format of the timestamp values for the x-values I'm providing, changing these to small float values in the range 1.0 -> 20.0f, causes the formatter to be invoked as expected. I need to debug the code further to see why the exponential timestamp values are not being formatted.

1

There are 1 best solutions below

2
On BEST ANSWER

From looking into this i think this is a limitation at the moment, for this specific use case (timestamps are very close together) since Entry will only accept floating point values, my timestamps are all getting passed as 1.50516885e^12, even though the precise timestamp values are slightly different. When AxisRenderer works out the range for the x-axis then is sees it as 0, and sets the entries to 0 also, in this block here