我正在编写一个应用程序,其中我有两个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 styles CPTMutableLinestyle *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]; // Axes CPTXYAxisSet *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 graph CPTScatterPlot *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 graph CPTScatterPlot *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 data NSMutableArray *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);