Reverse yAxis (DateTimeAxis)


I’m using a DateTimeAxis for my yAxis. Is there a way to reverse it? I’d like older entries at the top and newer entries at the bottom.

I found a forum post suggesting some trickery for a numeric yAxis, but that seems overly complicated - I could probably chart the negative milliseconds and use a custom label formatter to negate it again and display a proper date. 

Any simpler solution?

Android 1.5.0 Infinite Loop

Hi Radu,

In your case, to reverse the Y axis so that the older dates are at the top and the more recent dates at the bottom, you have to create and use a subclass of DateTimeAxis. It’s not particularly complicated though - you just have to override two protected methods: transformUserValueToChartValue and transformChartValueToUserValue.

In these methods you have to tell the axis how to map your user data into something the chart can use - basically, you have to swap round the dates!

To do this you will need to know the span, or range, of your data. One way of doing this, if you don’t know the span up front, is to get hold of the series on the axis and find the min and max Dates in the series’ data adapter - the Axis.getChart(), Chart.getSeriesForAxis(Axis) and Series.getDataAdapter() methods will be useful here.

Then it’s just a case of transforming your dates appropriately. If you think about it, the chartDate = span - userDate. And by the same logic, the userDate = span - chartDate. This will effectively swap the dates around - I found it helpful to first think about this in terms of a NumberAxis! So, with the min and max dates you have found, your _transformUserValueToChartValue _method will look something like:

Date min, max;
// … find min and max dates
long span = max.getTime() - min.getTime();
return new Date(span - userValue.getTime());

The transformChartValueToUserValue will look very similar but will subtract chartValue.getTime() instead of userValue.getTime().

Hope that helps!



Actually I found a simpler way:

using this to construct the data points:

new DataPoint<Double, Date>(value, new Date(-when.getTime()))

 (when is a Date)

and then using this for an axis:

DateTimeAxis yAxis = new DateTimeAxis()
            public String getFormattedString(Date value) {
                return super.getFormattedString(new Date(-value.getTime()));

The only gotcha was to set the color below the baseline as well


This way you don’t need to compute the min and the max.