How preserve zoom state?


#1

Hello guys,

Could you please advice me how can I preserve zoom state ?

Whenever I call redrawChart zooming is being lost, how can it be preserved (like reload chart data but zooming on the already zoomed point stay the same).

Thanks in advanc,cto_dev.


#2

And I am not able to find 

reloadDataAndPreserveAxisRanges

function, even after including SChart.h that function is not found.

It stated in SDK that it should exist in SChart.h, but I can’t find it, please help.


#3

It stated in SDK that it should exist in SChart.h, but I can’t find it, please help.

reloadDataAndPreserveAxisRanges: seems to have been added in 2.2.1; at least, there is no mention of it in the 2.2.0 docs. Are you sure you’re using the most recent version of the library?


#4

Dear Samuel,

thanks for reply, yes I am sure, just again downloaded recent version and can’t find it.


#5

Hi Guys,

The method reloadDataAndPreserveAxisRanges isn’t currently a method on our API. This method is currently under development as it is planned for a future release, and unfortunately it was included in our newest versions API documents by mistake. We are aware of this issue and we will be correcting our API documents shortly. I apologise for any inconvienience this has caused you.

Until this feature is released however, there is another way you can reload your chart with preserved ranges. Before you reload your chart, you need to save the charts current X and Y ranges in an ivar, set a flag indicating we want to reload preserving our axis ranges, and then reload our chart. We then pick up with flag in our sChartRenderStarted: method and set the X and Y ranges back to the ranges we stored previously. Then we trigger another chart redraw.

I implemented this method in my view controller but you could put it anywhere you want:

@implementation ViewController {
    SChartRange *_yRange;
    SChartRange *_xRange;
    BOOL _shouldPreserveRanges;
}

...

-(void)reloadAxisPreservingRanges
{
    // Save current ranges.
    _yRange = lineChart.yAxis.axisRange;
    _xRange = lineChart.xAxis.axisRange;
    
    // Set our flag.
    _shouldPreserveRanges = YES;

    // Reload & redraw our chart.
    [lineChart reloadData];
    [lineChart redrawChart];
}

Next, you need to implement something like this in your chart’s delegate. In my case, my view controller is my chart’s delegate:

-(void)sChartRenderStarted:(ShinobiChart *)chart withFullRedraw:(BOOL)fullRedraw
{
    // We only want to set the ranges and reload when our flag is YES
    if (_shouldPreserveRanges)
    {
        // Set the ranges of our chart's axes back to their old ranges.
        [lineChart.yAxis setRangeWithMinimum:_yRange.minimum andMaximum:_yRange.maximum];
        [lineChart.xAxis setRangeWithMinimum:_xRange.minimum andMaximum:_xRange.maximum];
        
        // Set our flag back to NO otherwise we will be caught in a redraw loop.
        _shouldPreserveRanges = NO;
        
        // Tell our chart to start its redraw again.
        [chart redrawChart];
    }
}

By implementing the above method I managed to get a seamless reload, keeping the chart in its current zoom/pan position :grin:.

Jan


#6

Hello,

This functionality has been implemented and can be found in the form of a property on the chart called “autoCalculateAxisRanges”.

If set to NO when you reload your data the current range on screen will stay displayed.

More information on this property can be found here:
https://www.shinobicontrols.com/docs/ShinobiControls/ShinobiCharts/2.8.0/Premium/Normal/html/Classes/ShinobiChart.html#//api/name/autoCalculateAxisRanges

You will need to initialise your axes with ranges if you don’t already do so to get them to display an initial range.

Let me know if you have any questions.

Kind regards,
Andrew Polkinghorn