Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 627
Appbrain Avatar asked Feb 15 '10 07:02

Appbrain


3 Answers

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)];
}
like image 127
Felix Khazin Avatar answered Oct 19 '22 06:10

Felix Khazin


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.

like image 7
DanSkeel Avatar answered Oct 19 '22 08:10

DanSkeel


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)];
}
like image 2
damien murphy. Avatar answered Oct 19 '22 08:10

damien murphy.