2010-02-15 9 views

Respuesta

9

Sí, La manera de hacerlo es mediante el ajuste de la PlotSpace al igual que (probablemente no es la mejor solución, pero lo mejor que pude llegar a):

-(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)]; 
} 
+2

donde u r llamar a estos métodos? – Pooja

+0

con este método veo que el zoom no está centrado, ¿hay alguna manera de acercar el centro de la gráfica? – kikko088

7

Es posible ajustar plotSpace delegado a algunos instancia de clase que se ajusta al protocolo CPTPlotSpaceDelegate (por ejemplo, self) [plotSpace setDelegate:self];.

configurar el plotSpace por lo que interactúa con el usuario [plotSpace setAllowsUserInteraction:YES];

Y entonces, en esa clase de implementar este método -(BOOL)plotSpace:(CPTPlotSpace *)space shouldScaleBy:(CGFloat)interactionScale aboutPoint:(CGPoint)interactionPoint. Regrese SÍ si desea que corePlot cambie el tamaño de su gráfico automáticamente. O NO y haz algo como en la respuesta anterior.

Nota: si quiere que su actual clase de controlador (uno mismo) sea un delegado, asegúrese de que su interfaz se vea como esta @interface CurrentController : UISomeController<...,CPTPlotSpaceDelegate>{}. Entonces ahora se ajusta al protocolo CPTPlotSpaceDelegate.

2

Uso de la interacción del usuario por defecto no fue suficiente para mí porque:

  1. Escala tanto X & eje Y al mismo tiempo y sólo quería escalar eje X
  2. Pan no permite que se pasa de cero y salta y básicamente no funciona bien ..

Mi solución fue hacerlo yo mismo

I añadieron 2 UIGestureRecognizer para 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]; 

entonces he implementado el código para hacer lo que necesitaba

Pan Gráfico

- (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)]; 
} 

carta de escala

- (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)]; 
} 
+0

faltan algunos puntos en el código, ¿puede proporcionar el código completo? –

1

Para escalar sólo en el eje x solo obligan a la nueva escala Y para estar a la antigua en el delegado:

-(BOOL)plotSpace:(CPTPlotSpace *)space shouldScaleBy:(CGFloat)interactionScale aboutPoint:(CGPoint)interactionPoint; 
Cuestiones relacionadas