先看代碼,有標註,很詳細,看看是如何設定x、y軸的可視範圍、移動範圍、已經如何確定原點的位置的、還有就是如何固定座標軸!!!
//座標軸的初始化
-(void)axesInit
{
// Setup plot space: 設置一屏內可顯示的x,y量度範圍
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)[xyGraph defaultPlotSpace];
plotSpace.delegate = self;
plotSpace.allowsUserInteraction = YES;//允許拖動
//設置移動時的停止動畫 這些參數保持默認即可 變化不大
plotSpace.momentumAnimationCurve = CPTAnimationCurveCubicIn;
plotSpace.bounceAnimationCurve = CPTAnimationCurveBackIn;
plotSpace.momentumAcceleration = 20000.0;
//設置x,y在視圖顯示中大小,也就是點的個數,通過這樣設置可以達到放大縮小的效果,來達到我們想要的合理視圖顯示
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(18.0) length:CPTDecimalFromFloat(22.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(32.0) length:CPTDecimalFromFloat(10.5)];
//設置x、y軸的滾動範圍,如果不設置,默認是無線長的
plotSpace.globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-1.0) length:CPTDecimalFromFloat(25.0)];
plotSpace.globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(32.0) length:CPTDecimalFromFloat(10.5)];
// Axes: 設置x,y軸屬性,如原點,量度間隔,標籤,刻度,顏色等
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)xyGraph.axisSet;
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.miterLimit = 1.0f;
lineStyle.lineWidth = 1.0f;
lineStyle.lineColor = [CPTColor whiteColor];
[plotSpace setAllowsMomentumX:YES];
CPTXYAxis *x = axisSet.xAxis;
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"33");// x軸的原點位置,其實這裏是y座標的值,也就是說x軸的原點在y軸的1位置
x.majorIntervalLength = CPTDecimalFromString(@"1.0"); // x軸主刻度:顯示數字標籤的量度間隔
x.minorTicksPerInterval = 4; // x軸細分刻度:每一個主刻度範圍內顯示細分刻度的個數
x.minorTickLineStyle = lineStyle;
lineStyle.lineColor = [CPTColor lightGrayColor];
x.majorGridLineStyle = lineStyle;//這裏設置x軸中主刻度的柵格,平行於y軸
// 需要排除的不顯示數字的主刻度
NSArray *exclusionRanges = [NSArray arrayWithObjects:
[self CPTPlotRangeFromFloat:0.99 length:0.02],
[self CPTPlotRangeFromFloat:2.99 length:0.02],
nil];
x.labelExclusionRanges = exclusionRanges;
CPTXYAxis *y = axisSet.yAxis;
y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"18");//y軸的原點位置,其實這裏是x座標的值,也就是說y軸的原點在x軸的0位置
//固定y軸,也就是在你水平移動時,y軸是固定在左/右邊不動的,以此類推x軸
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:20];//這裏是固定y座標軸在最右邊(距離可視右邊界有20個像素距離,一遍顯示標籤)
y.majorIntervalLength = CPTDecimalFromString(@"0.5");
y.minorTicksPerInterval = 4;
y.minorTickLineStyle = lineStyle;
y.tickDirection = CPTSignNegative;//標籤的方向,對於y軸來說:CPTSignPositive標籤在y軸的右邊,CPTSignNegative:在y軸的左側
lineStyle.lineColor = [CPTColor lightGrayColor];
y.majorGridLineStyle = lineStyle;//設置柵格線,平行於x軸 如果 labelingPolicy 設置爲 CPTAxisLabelingPolicyNone , majorGridLineStyle 將不起作用
// y.labelingPolicy = CPTAxisLabelingPolicyNone;
NSArray *exclusionRangesY = [NSArray arrayWithObjects:
[self CPTPlotRangeFromFloat:1.99 length:0.2],
[self CPTPlotRangeFromFloat:2.99 length:0.2], nil];
y.labelExclusionRanges = exclusionRangesY;
y.delegate = self;
}
下面我想說說如何固定一個座標軸,在移動時,該軸始終是不會動的!上面代碼裏有:
//固定y軸,也就是在你水平移動時,y軸是固定在左/右邊不動的,以此類推x軸
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:20];//這裏是固定y座標軸在最右邊(距離可視右邊界有20個像素距離,一遍顯示標籤)
下面分析一下相應的方法有哪些:主要在CPTConstraints.h和CPTConstraints.m中實現的
/** @brief Creates and returns a new CPTConstraints instance initialized with a fixed offset from the lower bound.
* @param newOffset The offset.
* @return A new CPTConstraints instance initialized with the given offset.
* @sfx :該函數主要是實現固定座標軸在距離最小端newoffset的地方,我個人對大端小端的理解:對y軸來說,最大端(或者說最高處)也就是我們座標系bounce的右邊界
* 最小端(或者說最低處)就是座標系視圖boundce的左邊界,這樣x軸也就同樣可以理解了,相應的就是上、下邊界
**/
+(CPTConstraints *)constraintWithLowerOffset:(CGFloat)newOffset
{
return [[(_CPTConstraintsFixed *)[_CPTConstraintsFixed alloc] initWithLowerOffset : newOffset] autorelease];
}
/** @brief Creates and returns a new CPTConstraints instance initialized with a fixed offset from the upper bound.
* @param newOffset The offset.
* @return A new CPTConstraints instance initialized with the given offset.
<pre name="code" class="objc"> * @sfx :該函數主要是實現固定座標軸在距離最高端newoffset的地方,我個人對大端小端的理解:對y軸來說,最大端(或者說最高處)也就是我們座標系bounce的右邊界
* 最小端(或者說最低處)就是座標系視圖boundce的左邊界,這樣x軸也就同樣可以理解了,相應的就是上、下邊界
**/+(CPTConstraints *)constraintWithUpperOffset:(CGFloat)newOffset{ return [[(_CPTConstraintsFixed *)[_CPTConstraintsFixed alloc] initWithUpperOffset : newOffset] autorelease];}/** @brief Creates and returns a new CPTConstraints instance initialized with a proportional offset relative to the bounds. * * For example, an offset of @num{0.0} will return a position equal to the lower bound, @num{1.0} will return the upper bound, * and @num{0.5} will return a point midway between the two bounds. * * @param newOffset The offset.
* @sfx : 這個類方法實現的是按一定比例值來固定座標軸,比如我們固定y軸,如果newoffset = 1.0,就相當於把y軸固定在右邊界,如果newoffset = 0.0 就是左邊界,如果等於0.5就是中間
newoffset取值範圍:0 --- 1.0
* @return A new CPTConstraints instance initialized with the given offset. **/+(CPTConstraints *)constraintWithRelativeOffset:(CGFloat)newOffset{ return [[(_CPTConstraintsRelative *)[_CPTConstraintsRelative alloc] initWithRelativeOffset : newOffset] autorelease];}