2010-02-15 10 views

risposta

9

Sì, Il modo in cui lo faccio è regolando il PlotSpace in questo modo (probabilmente non la soluzione migliore, ma il migliore che ho potuto venire con):

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

dove u r chiamare questo metodo? – Pooja

+0

con questo metodo vedo che lo zoom non è centrato, c'è un modo per ingrandire il centro della trama? – kikko088

7

È possibile impostare plotSpace delegato ad alcuni istanza di classe conforme al protocollo CPTPlotSpaceDelegate (ad esempio self) [plotSpace setDelegate:self];.

Configurare il plotSpace modo in cui interagisce con l'utente [plotSpace setAllowsUserInteraction:YES];

E poi in quella classe implementare questo metodo -(BOOL)plotSpace:(CPTPlotSpace *)space shouldScaleBy:(CGFloat)interactionScale aboutPoint:(CGPoint)interactionPoint. Ritorna SÌ se vuoi che il corePlot ridimensioni automaticamente il tuo grafico. O NO e fare qualcosa come nella risposta precedente.

Nota: se si desidera che la classe del controller corrente (self) sia delegata, assicurarsi che l'interfaccia sia simile a questa @interface CurrentController : UISomeController<...,CPTPlotSpaceDelegate>{}. Quindi ora è conforme al protocollo CPTPlotSpaceDelegate.

2

Utilizzando l'interazione con l'utente di default non è stato sufficiente per me, perché:

  1. È scalabile sia X & Y simultaneamente e volevo solo per scalare X asse
  2. Pan non consente di andare oltre lo zero e salta e fondamentalmente non funziona giusto ..

La mia soluzione era quella di farlo io stesso

ho aggiunto 2 UIGestureRecognizer per 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]; 

Ho poi implementato il codice per fare quello che mi serviva

Pan Grafico

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

Grafico Scala

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

ci sono alcuni punti mancanti nel codice, per favore puoi fornire il codice completo? –

1

Per scalare solo in asse x appena costringono il nuovo y scala di essere il vecchio nel delegato:

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