Kashyap
Kashyap

Reputation: 43

Zooming core plot along Y-axis and X-axis Independently

I am Using the below code to scroll my core plot along X-axis Y-axis and zoom. Its working fine. But I when i zoom my core plot, it zooms in both the directions. I want the plot to zoom along X if I pinch along x-direction and zoom Y if pinch along Y-direction. Can someone help me with this please.

-(CGPoint)plotSpace:(CPTPlotSpace *)space willDisplaceBy:(CGPoint)displacement
{
    return CGPointMake(displacement.x, displacement.y);
}

-(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate
{                                                                                                                                                                                                                                           
    // Adjust axis to keep them in view at the left and bottom;
    // adjust scale-labels to match the scroll.

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.hostView.hostedGraph.axisSet;
    if (coordinate == CPTCoordinateX) {
        axisSet.yAxis.orthogonalCoordinateDecimal = newRange.location;
    }

    else {
        axisSet.yAxis.titleLocation = CPTDecimalFromFloat(newRange.locationDouble +                                                  (newRange.lengthDouble / 2.0F));
    }

    return newRange;
}

Upvotes: 2

Views: 2541

Answers (2)

Kashyap
Kashyap

Reputation: 43

I am calculating the change in X and Y coordinates using UIPinchGestureRecognizer and then determining which direction the plot range should change(either X or Y). Its working fine but It is not as smooth as regular zoom, and it responds late. Can someone suggest me a better way to do this

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer{
       if ([gestureRecognizer state] == UIGestureRecognizerStateBegan){

        CGPoint translation = [gestureRecognizer locationInView:hostView];
       NSLog(@"Sender value %f %f", translation.x,translation.y );
        initialX = translation.x;
        initialY = translation.y;        
        return;   
} 
else if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged){
                NSLog(@"inside else");
        CGPoint currentTouchLocation = [gestureRecognizer locationInView:hostView];
        NSLog(@"currentTouchLocation = %f and  %f and ",currentTouchLocation.x, currentTouchLocation.y);
            finalX = currentTouchLocation.x;
            finalY = currentTouchLocation.y;
                  }
}
-(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate
{
    float x = fabsf(finalX - initialX) ;
    float y = fabsf(finalY - initialY);
    NSLog(@"pinch x = %f  pinch y = %f", x, y);
      CPTPlotRange *updatedRange = nil;

    if (x > y) {
        switch ( coordinate ) {
            case CPTCoordinateX:
                NSLog(@"x is greater than y change x-range");

                if (newRange.locationDouble < 0.0F ) {
                    CPTMutablePlotRange *mutableRange = [[newRange mutableCopy] autorelease];
                  mutableRange.location = CPTDecimalFromFloat(0.0);
                    updatedRange = mutableRange;
                }
                else {
                    updatedRange = newRange;
               }
                break;
            case CPTCoordinateY:
                NSLog(@"x is greater than y keep y range constant");

                updatedRange = ((CPTXYPlotSpace *)space).yRange;
                break;

        }

            }

    if (x < y) {
        switch ( coordinate ) {
            case CPTCoordinateX:
                NSLog(@"y is greater than x keep x-range constant");

                updatedRange = ((CPTXYPlotSpace *)space).xRange;
                                break;
            case CPTCoordinateY:
                if (newRange.locationDouble < 0.0F) {
                    NSLog(@"y is greater than x increase y range");
                    CPTMutablePlotRange *mutableRange = [[newRange mutableCopy] autorelease];
                  //  mutableRange.location = CPTDecimalFromFloat(0.0);
                    updatedRange = mutableRange;
                }
                else {
                   updatedRange = newRange;
                }

                break;

        }

    }
    if (x == y) {
        switch ( coordinate ) {
            case CPTCoordinateX:
                NSLog(@"y is equal to  x keep x-range constant");
                updatedRange = ((CPTXYPlotSpace *)space).xRange;
                break;
            case CPTCoordinateY:
                NSLog(@"y is equal to x keep y-range constant");
                //NSLog(@"%d", CPTCoordinateY);
                updatedRange = ((CPTXYPlotSpace *)space).yRange;
                break;

        }

    }



      return updatedRange;



}

Upvotes: 0

Eric Skroch
Eric Skroch

Reputation: 27381

The easiest way to keep the axes and title in the correct position is to use the axisConstraints for the axes and leave the titleLocation at it's default of NAN. This will relieve your delegate of responsibility for updating those items and you can focus on the zooming.

Of those two delegate methods, you only need -plotSpace:willChangePlotRangeTo:forCoordinate:. The other one is only called when scrolling.

Decide whether you want to allow the zoom to happen in x or y (see the links in the comments on the original question). Check the coordinate parameter in the delegate method; return the newRange to allow the zoom to happen or [space plotRangeForCoordinate:coordinate] to restore the original range and prevent zooming.

If you need to use your own gesture recognizer to detect the pinch angle, set allowPinchScaling to NO on the hosting view to disable the built-in recognizer. Add your own recognizer to the hosting view. In the handler method, decide which axis to scale (if any) and adjust the appropriate plot range accordingly. If you do it this way, you don't need a plot space delegate at all.

Upvotes: 1

Related Questions