在一個具體的項目中,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;}