2010-02-15 4 views

Répondre

9

Oui, comme je le fais est en ajustant la PlotSpace comme si (probablement pas la meilleure solution, mais le meilleur que je puisse trouver):

-(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

où u r appeler ces méthodes? – Pooja

+0

avec cette méthode je vois que le zoom n'est pas centré, il y a un moyen de zoomer le centre de l'intrigue? – kikko088

7

Vous pouvez définir délégué plotSpace à certains instance de classe conforme au protocole CPTPlotSpaceDelegate (par exemple self) [plotSpace setDelegate:self];.

configure le plotSpace il interagit avec l'utilisateur [plotSpace setAllowsUserInteraction:YES];

Et puis dans cette classe de mettre en œuvre cette méthode -(BOOL)plotSpace:(CPTPlotSpace *)space shouldScaleBy:(CGFloat)interactionScale aboutPoint:(CGPoint)interactionPoint. Renvoie YES si vous voulez que le corePlot redimensionne automatiquement votre graphique. Ou NON et faites quelque chose comme dans la réponse précédente.

Remarque: si vous voulez que votre classe de contrôleur actuelle (auto) soit un délégué, assurez-vous que votre interface ressemble à ceci: @interface CurrentController : UISomeController<...,CPTPlotSpaceDelegate>{}. Donc maintenant, il est conforme au protocole CPTPlotSpaceDelegate.

2

Utilisation de l'interaction de l'utilisateur par défaut ne suffisait pas pour moi parce que:

  1. Il adapte les deux X & axe Y en même temps et je voulais seulement à l'échelle axe X
  2. Pan ne vous permet pas d'aller passé zéro et il saute et ne fonctionne pas fondamentalement droit ..

Ma solution était de faire moi-même

I ajouté 2 UIGestureRecognizer 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]; 

Je puis mis en œuvre le code pour faire ce que je devais

Pan Graphique

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

échelle graphique

- (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

il manque quelques points dans le code, s'il vous plaît pouvez-vous fournir un code complet? –

1

à l'échelle que dans l'axe x juste force la nouvelle échelle y être l'ancien dans le délégué:

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