CorePlot 點線圖的時候,縮放不超過 一定範圍的功能實現

來源:互聯網
上載者:User

在一個具體的項目中,Y軸 範圍只能是 0-10 ,X軸範圍只能是49-288.

圖表需要支援縮放和移動。並且不能超過這個範圍。

實現思路:

1. 設定X和Y軸的範圍。

2.實現CPTPlotSpaceDelegate的委託中的 

shouldScaleBy 

shouldHandlePointingDeviceDraggedEvent

willChangePlotRangeTo

三個方法。

3.其中最主要的實現方法是 willChangePlotRangeTo 來,判斷將要縮放以後的 x,y軸的範圍。如果範圍在整個地區中間,則返回新的範圍。如果超過 整體範圍,則設定成最大範圍。

代碼主要有

1.實現代理

@interface Chart1ViewController : UIViewController <CPTPlotDataSource, CPTAxisDelegate,CPTPlotSpaceDelegate >

2. 設定x,y軸的最大範圍

 CPTPlotRange * xPlotRange; CPTPlotRange * yPlotRange;
    CPTXYPlotSpace *plotSpace =(CPTXYPlotSpace *) graph.defaultPlotSpace;plotSpace.allowsUserInteraction= YES;[plotSpace setDelegate:self];// 設定x,y座標範圍plotSpace.xRange = xPlotRange;plotSpace.yRange = yPlotRange;
//放大縮小的時候調用-(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate{//限制縮放和移動的時候。不超過原始範圍    if ( coordinate == CPTCoordinateX)    {        if ([ xPlotRange containsRange:newRange])        {            //如果縮放範圍在 原始範圍內。則返回縮放範圍            return newRange;                    }else if([newRange containsRange:xPlotRange])        {            //如果縮放範圍在原始範圍外,則返回原始範圍            return xPlotRange;        }        else{            //如果縮放和移動,導致新範圍和元素範圍向交叉。則要控制 左邊或者右邊超界的情況            NSDecimalNumber *myXPlotLocationNSDecimalNumber = [NSDecimalNumber decimalNumberWithDecimal:xPlotRange.location];            NSDecimalNumber *myXPlotLengthNSDecimalNumber = [NSDecimalNumber decimalNumberWithDecimal:xPlotRange.length];                        NSDecimalNumber *myNewRangeLocationNSDecimalNumber = [NSDecimalNumber decimalNumberWithDecimal:newRange.location];            NSDecimalNumber *myNewRangeLengthNSDecimalNumber = [NSDecimalNumber decimalNumberWithDecimal:newRange.length];            NSLog(@"willChangePlotRangeTo  newRange :%@\n xplotRange is %@",newRange,xPlotRange);            if ( myXPlotLocationNSDecimalNumber.doubleValue >= myNewRangeLocationNSDecimalNumber.doubleValue)            {                //限制左邊不超界                CPTPlotRange * returnPlot = [[CPTPlotRange alloc ] initWithLocation:xPlotRange.location length:newRange.length];                return returnPlot;            }            if ((myNewRangeLocationNSDecimalNumber.doubleValue + myNewRangeLengthNSDecimalNumber.doubleValue) > (myXPlotLengthNSDecimalNumber.doubleValue +myXPlotLocationNSDecimalNumber.doubleValue))            {                double offset = (myNewRangeLocationNSDecimalNumber.doubleValue + myNewRangeLengthNSDecimalNumber.doubleValue) -(myXPlotLengthNSDecimalNumber.doubleValue+myXPlotLocationNSDecimalNumber.doubleValue);                //限制右邊不超界                CPTPlotRange * returnPlot = [[CPTPlotRange alloc ] initWithLocation:[NSDecimalNumber numberWithDouble:(myNewRangeLocationNSDecimalNumber.doubleValue - offset)].decimalValue length:newRange.length];//                CPTPlotRange * returnPlot = [[CPTPlotRange alloc ] initWithLocation:newRange.location length:xPlotRange.length];                NSLog(@"右邊超界,超界 %f", offset);                NSLog(@"將要返回的 range 是:%@",returnPlot);                return returnPlot;            }        }        return newRange;    }else{        return yPlotRange;    }}
-(BOOL)plotSpace:(CPTPlotSpace *)space shouldScaleBy:(CGFloat)interactionScale aboutPoint:(CGPoint)interactionPoint{    return true;}-(BOOL) plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDraggedEvent:(UIEvent *)event atPoint:(CGPoint)point{      return YES;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.