xcode – 核心图中的两个y轴,具有不同的轴刻度

xcode – 核心图中的两个y轴,具有不同的轴刻度,第1张

概述我正在编写一个应用程序,其中我有两个y轴和一个x轴的图形.左侧y轴的范围从0到20所以有20个majorTick轴.右y轴的范围从0到10,所以我想要左y轴标记为每个替代majorTickAxis. 这是我的代码片段 //code CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;float 我正在编写一个应用程序,其中我有两个y轴和一个x轴的图形.左侧y轴的范围从0到20所以有20个majorTick轴.右y轴的范围从0到10,所以我想要左y轴标记为每个替代majorTickAxis.

这是我的代码片段

//code   CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;float xmax=10.0;float xmin=0.0;plotSpace.xrange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromfloat(xmin)      length:CPTDecimalFromfloat(xmax-xmin)];float ymax=20.0;float ymin=0.0;float ymax2=10.0;plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromfloat(ymin) length:CPTDecimalFromfloat(ymax-ymin)];// GrID line stylesCPTMutablelinestyle *majorGrIDlinestyle = [CPTMutablelinestyle linestyle];majorGrIDlinestyle.linewidth = 0.75;majorGrIDlinestyle.linecolor = [[CPTcolor whitecolor] colorWithAlphaComponent:0.75];CPTMutablelinestyle *minorGrIDlinestyle = [CPTMutablelinestyle linestyle];minorGrIDlinestyle.linewidth = 0.25;minorGrIDlinestyle.linecolor = [[CPTcolor whitecolor] colorWithAlphaComponent:0.1];CPTMutablelinestyle *redlinestyle = [CPTMutablelinestyle linestyle];redlinestyle.linewidth = 2.0;redlinestyle.linecolor = [[CPTcolor redcolor] colorWithAlphaComponent:0.5];                         CPTMutablelinestyle *greenlinestyle = [CPTMutablelinestyle linestyle];greenlinestyle.linewidth = 2.0;greenlinestyle.linecolor = [[CPTcolor greencolor] colorWithAlphaComponent:0.5];     // AxesCPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;CPTXYAxis *x          = axisSet.xAxis;x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");x.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"1"] decimalValue];x.minorTicksPerInterval       = 4;x.labelOffset = 3.0f;x.Title         = @"Time";x.TitleOffset   = 20.0;x.TitleLocation = CPTDecimalFromfloat((xmax+xmin)/2);x.majorGrIDlinestyle= majorGrIDlinestyle;x.minorGrIDlinestyle=minorGrIDlinestyle;CPTXYAxis *y = axisSet.yAxis;y.majorIntervalLength         = CPTDecimalFromString(@"1.0");y.majorTickLength=2.0f;y.minorTicksPerInterval       = 4;y.orthogonalCoordinateDecimal = CPTDecimalFromfloat(0.0);y.labelExclusionRanges = [NSArray arrayWithObjects:                          [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0.0)                                                        length:CPTDecimalFromInteger(0.0)],Nil];y.majorGrIDlinestyle= majorGrIDlinestyle;y.minorGrIDlinestyle=minorGrIDlinestyle;CPTXYAxis *y2 = [[(CPTXYAxis *)[CPTXYAxis alloc] initWithFrame:CGRectZero] autorelease];y2.plotSpace    =plotSpace;     y2.orthogonalCoordinateDecimal = CPTDecimalFromfloat(xmax);y2.majorGrIDlinestyle=majorGrIDlinestyle;y2.minorGrIDlinestyle=minorGrIDlinestyle;y2.minorTicksPerInterval       = 4;y2.majorIntervalLength        = CPTDecimalFromString(@"1.0");y2.labelOffset        = 10.0;y2.coordinate          =CPTCoordinateY;y2.axislinestyle       = x.axislinestyle;y2.labelTextStyle              = x.labelTextStyle;y2.labelOffset                 = -30.0f;y2.visibleRange                = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(ymax2)];y2.Title                       = @"Temperature";y2.TitleLocation               = CPTDecimalFromInteger(5.0);y2.TitleTextStyle              =x.TitleTextStyle;y2.TitleOffset                 =-45.0f;y2.labelExclusionRanges = [NSArray arrayWithObjects:                          [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0.0)                                                        length:CPTDecimalFromInteger(0.0)],Nil];self.graph.axisSet.axes = [NSArray arrayWithObjects:x,y,y2,nil];// Create a plot that uses the data source method for red graphCPTScatterPlot *redplot = [[[CPTScatterPlot alloc] init] autorelease];redplot.IDentifIEr = @"red Plot";;CPTMutablelinestyle *linestyle = [[redplot.datalinestyle mutablecopy] autorelease];linestyle.miterlimit        = 1.0f;redplot.datalinestyle = redlinestyle;redplot.dataSource = self;redplot.interpolation = CPTScatterPlotInterpolationStepped;[self.graph addplot:redplot];// Create a plot that uses the data source method for green graphCPTScatterPlot *greenPlot = [[[CPTScatterPlot alloc] init] autorelease];greenPlot.IDentifIEr = @"green Plot";;CPTMutablelinestyle *greenlinestyle = [[greenPlot.datalinestyle mutablecopy] autorelease];greenlinestyle.miterlimit       = 1.0f;greenPlot.datalinestyle = greenlinestyle;greenPlot.dataSource = self;[self.graph addplot:greenPlot]; // Add some dataNSMutableArray *newData = [NSMutableArray arrayWithCapacity:100];NSUInteger i;for ( i = 0; i < 45; i++ ) {    ID x = [NSNumber numberWithDouble:i * 0.2];    ID y = [NSNumber numberWithDouble:i * rand() / ((double)RAND_MAX*5.0) ];            [newData addobject:[NSDictionary dictionaryWithObjectsAndKeys:      x,@"x",@"y",nil]];}NSMutableArray *newData1 = [NSMutableArray arrayWithCapacity:100];for ( i = 0; i < 45; i++ ) {    ID x =[NSNumber numberWithDouble:i * rand() / ((double)RAND_MAX*5.0) ];     ID y2 = [NSNumber numberWithDouble:i * 0.2];    [newData1 addobject:[NSDictionary dictionaryWithObjectsAndKeys:                        x,@"y2",nil]];}self.plotData = newData;self.plotData2=newData1;
解决方法 如果希望两个y轴具有不同的比例,则需要添加另一个绘图空间.对第二个绘图空间使用相同的xrange,但使用不同的yRange,例如,
CPTXYPlotSpace *plotSpace2 = [[[CPTXYPlotSpace alloc] init] autorelease];plotSpace2.xrange = plotSpace.xrange;plotSpace2.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromfloat(ymin)                                                 length:CPTDecimalFromfloat(ymax2 - ymin)];[graph addplotSpace:plotSpace2];y2.plotSpace = plotSpace2;

使用majorIntervalLength来控制刻度线和网格线的位置:

y.majorIntervalLength = CPTDecimalFromfloat((ymax - ymin) / 10.0f);y2.majorIntervalLength = CPTDecimalFromfloat((ymax2 - ymin) / 10.0f);
总结

以上是内存溢出为你收集整理的xcode – 核心图中的两个y轴,具有不同的轴刻度全部内容,希望文章能够帮你解决xcode – 核心图中的两个y轴,具有不同的轴刻度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1105265.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存