How to implement Zoom-in and Zoom-Out with Core-Plot line chart on iPhone?
Asked Answered
B

4

12

Is it possible to implement Zoom-in and Zoom-Out with Core-Plot Chart component on iPhone? If so please suggest me how to do it?

Thanks in advance.

Blastula answered 15/2, 2010 at 7:43 Comment(0)
A
9

Yes, The way I do it is by adjusting the PlotSpace like so (probably not the best solution, but the best one i could come up with):

-(void)zoomOut 
{
    CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;

    graphScaleX+=5;
    graphScaleY+=5;

    plotSpace.xRange = [CPPlotRange plotRangeWithLocation:plotSpace.xRange.location length:CPDecimalFromFloat(graphScaleX)];
    plotSpace.yRange = [CPPlotRange plotRangeWithLocation:plotSpace.yRange.location length:CPDecimalFromFloat(graphScaleY)];
}

-(void)zoomIn
{
    CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;

    graphScaleX-=5;
    graphScaleY-=5;

    plotSpace.xRange = [CPPlotRange plotRangeWithLocation:plotSpace.xRange.location length:CPDecimalFromFloat(graphScaleX)];
    plotSpace.yRange = [CPPlotRange plotRangeWithLocation:plotSpace.yRange.location length:CPDecimalFromFloat(graphScaleY)];
}
Adviser answered 24/2, 2010 at 15:24 Comment(2)
where u r calling this methods?Dinesh
with this method I see that the zoom is not centered, there is a way for zooming the center of plot?Libya
O
7

You can set plotSpace delegate to some class instance that conforms to CPTPlotSpaceDelegate protocol(for example self)[plotSpace setDelegate:self];.

configure the plotSpace so it interacts with user [plotSpace setAllowsUserInteraction:YES];

And then in that class implement this method -(BOOL)plotSpace:(CPTPlotSpace *)space shouldScaleBy:(CGFloat)interactionScale aboutPoint:(CGPoint)interactionPoint. Return YES if you want the corePlot to resize your graph automatically. Or NO and do something like in previous answer.

Note: if you want your current controller class (self) to be a delegate, be sure that your interface looks like this @interface CurrentController : UISomeController<...,CPTPlotSpaceDelegate>{}. So now it conforms to CPTPlotSpaceDelegate protocol.

Overall answered 25/10, 2011 at 14:27 Comment(0)
B
2

Using the default user interaction was not enough for me because:

  1. It scales both X & Y axis simultaneously and I only wanted to scale X axis
  2. Pan does not allow you go past zero and it jumps and basically doesn't work right..

My solution was to do it myself

I added 2 UIGestureRecognizer for Pan, Zoom

    UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panChart:)];
    [pan setMinimumNumberOfTouches:1];
    [pan setMaximumNumberOfTouches:1];
    [self.view addGestureRecognizer:pan];

    UIPinchGestureRecognizer* scale = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleChart:)];
    [self.view addGestureRecognizer:scale];

I then implemented the code to do what I needed

Pan Chart

- (void)panChart:(UIPinchGestureRecognizer *)gestureRecognizer {
    CPTGraph *theGraph = [graphs objectAtIndex:0];
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)theGraph.defaultPlotSpace;

    CGPoint point = [gestureRecognizer  locationInView:self.view];

    if([(UIPanGestureRecognizer*)gestureRecognizer state] == UIGestureRecognizerStateBegan) {

        firstX = point.x;
        firstY = point.y;
        originX = chartBounds.origin.x;
        originY = chartBounds.origin.y;
        //NSLog(@"first (%f,%f)", firstX, firstY);
    }
    CGRect frame = theGraph.frame;

    float xMove =  ((firstX-point.x)/(frame.size.width/chartBounds.size.width));
    float yMove = ((firstY-point.y)/(frame.size.height/chartBounds.size.height));
    if(firstX-point.x == 0){
        xMove = 0;
    }
    if(firstY-point.y == 0){
        yMove = 0;
    }
    //NSLog(@"frame: (%f, %f)",frame.size.width, frame.size.height);
    //NSLog(@"touch (%f,%f)",firstX-point.x,firstY-point.y);
    //NSLog(@"move (%f,%f)",xMove,yMove);

    chartBounds.origin.x = originX+xMove;
    chartBounds.origin.y = originY-yMove;

    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(chartBounds.origin.x) length:CPTDecimalFromFloat(chartBounds.size.width)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(chartBounds.origin.y) length:CPTDecimalFromFloat(chartBounds.size.height)];
}

Scale Chart

- (void)scaleChart:(UIPinchGestureRecognizer *)gestureRecognizer {

if (kMaxDataPoints < kDataPointsHistory) {

    if ([gestureRecognizer scale] < 1) {
        if(kMaxDataPoints/10 < 1){
            kMaxDataPoints += 1;
        }else{
           kMaxDataPoints += kMaxDataPoints/10; 
        }
    }
}

if (kMaxDataPoints > 5) {
    if ([gestureRecognizer scale] > 1){
        if(kMaxDataPoints/10 < 1){
            kMaxDataPoints -= 1;
        }else{
            kMaxDataPoints -= kMaxDataPoints/10; 
        }
    }
}

if(kMaxDataPoints <= 5){
    kMaxDataPoints = 5;
}
if(kMaxDataPoints > kDataPointsHistory){
    kMaxDataPoints = kDataPointsHistory;
}

chartBounds.size.width = kMaxDataPoints;
CPTGraph *theGraph = [graphs objectAtIndex:0];
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)theGraph.defaultPlotSpace;

plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(chartBounds.origin.x) length:CPTDecimalFromFloat(chartBounds.size.width)];

//Need to calculate if its horizontal or vertical pinch gesture (not doing that yet :) )
//plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(chartBounds.origin.y) length:CPTDecimalFromFloat(chartBounds.size.height)];
}
Button answered 19/4, 2012 at 9:28 Comment(1)
there are some points missing in the code, please can you provide complete code?Mitochondrion
R
1

To scale only in x axis just force the new y-scale to be the old one in the delegate:

-(BOOL)plotSpace:(CPTPlotSpace *)space shouldScaleBy:(CGFloat)interactionScale aboutPoint:(CGPoint)interactionPoint;
Roxannroxanna answered 28/5, 2012 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.