Problem with axis setRangeWithMinimum:andMaximum: when double tapping to zoom


#1

I’ve got a line chart that plots values over time, time being the x axis and value being the x axis.

I’m trying to restirct the zoom from zooming in further than we have data for. For example the data for the chart is daily data. By default the user could zoom in beoynd what we have data for and would start to see ranges in hours etc, so the graph lines would become streight. We don’t want this as it’s not accuatre and confusing as there is no data for this level of zoom precision.

I found some code from somewhere else on this forum where it was suggested that you should in sChartIsZooming:withChartMovementInformation: detect the span and compare it to a required minimum span and it smaller reset the range. self.miniumSpan in my case is 3600 * 24 * 7 (7 days).

if ([chart.xAxis.axisRange.span floatValue] < self.minimumSpan) {
                
        // re-zoom it
        CGFloat midValue = [chart.xAxis.axisRange.span floatValue] / 2 + [chart.xAxis.axisRange.minimum floatValue];
        CGFloat newMinValue = midValue - self.minimumSpan / 2;
        CGFloat newMaxValue = midValue + self.minimumSpan / 2;
        
        [chart.xAxis setRangeWithMinimum:@(newMinValue) andMaximum:@(newMaxValue)];
    }

While this works well when pinching this fails to work when double tapping to zoom. Initally it works but if you double tap a few times it jumps in and zooms further and futher and the logic above seems to have no effect.

This only seems to be a problem when double tapping, any ideas?


#2

Is the delegate method in question definitely called when double tapping to zoom? If so have you debugged it to find out why the if statement is failing? - this might give us an idea as to what’s going wrong.

An alterative to using the delegate might be to subclass your x axis and override the method setRangeWithMinimum:andMaximum:withAnimation:usingBounceLimits:. But the fact that you say your solution initially works and then stops working after a couple of double of taps makes me think something is going wrong with the if statement rather than the delegate not being called.


#3

Yeah the delegate method gets called. It appears to be an issue because the zooming when pinching is linear but when double tapping it jumps to specfic zoom levels causing the logic above to calculate odd values.

The code is always called even when double tapping just that the values in axisRange.span become unreliable.