如何在iPhone上使用Core-Plot折线图实现放大和缩小?

时间:2010-02-15 07:43:11

标签: iphone charts core-plot

是否可以在iPhone上使用Core-Plot Chart组件实现放大和缩小?如果是这样,请建议我怎么做?

提前致谢。

4 个答案:

答案 0 :(得分:9)

是的,我这样做是通过像这样调整PlotSpace(可能不是最好的解决方案,但是我能想到的最好的解决方案):

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

答案 1 :(得分:7)

您可以将plotSpace委托设置为符合CPTPlotSpaceDelegate协议的某个类实例(例如self)[plotSpace setDelegate:self];

配置plotSpace,使其与用户[plotSpace setAllowsUserInteraction:YES];

进行交互

然后在该类中实现此方法-(BOOL)plotSpace:(CPTPlotSpace *)space shouldScaleBy:(CGFloat)interactionScale aboutPoint:(CGPoint)interactionPoint。如果您希望corePlot自动调整图表大小,请返回YES。或者不做,并在之前的回答中做一些事情。

注意:如果您希望当前的控制器类(self)成为委托,请确保您的界面看起来像@interface CurrentController : UISomeController<...,CPTPlotSpaceDelegate>{}。所以现在它符合CPTPlotSpaceDelegate协议。

答案 2 :(得分:2)

使用默认用户交互对我来说还不够,因为:

  1. 它可以扩展X&amp; Y轴同时,我只想缩放 X轴
  2. 潘不允许你经过零而且它会跳跃 基本上不能正常工作..
  3. 我的解决方案是自己动手

    我为Pan,Zoom添加了2个UIGestureRecognizer

        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];
    

    然后我实现了代码来完成我需要的工作

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

    比例表

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

答案 3 :(得分:1)

要仅在x轴上缩放,只需强制新y尺度为委托中的旧y尺度:

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