先看代码,有标注,很详细,看看是如何设定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];
}
<pre name="code" class="objc">/** @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.
* @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];
}