Iphone 核心图:ios在同一个CPTGraph中显示两个y轴、散射图和条形图
您好,请指导我写的方式,以显示两个Y轴左侧和右侧与一个x轴使用核心图,而且我必须绘制散射图和条形图都在单个CPTGraph。 CPTScatter绘图显示在右侧Y轴上,方向为-ve 我的代码片段Iphone 核心图:ios在同一个CPTGraph中显示两个y轴、散射图和条形图,iphone,ios,ios5,core-plot,Iphone,Ios,Ios5,Core Plot,您好,请指导我写的方式,以显示两个Y轴左侧和右侧与一个x轴使用核心图,而且我必须绘制散射图和条形图都在单个CPTGraph。 CPTScatter绘图显示在右侧Y轴上,方向为-ve 我的代码片段 -(void)configureAxes { // 1 - Configure styles CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle]; axisTitleStyle.color =
-(void)configureAxes
{
// 1 - Configure styles
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor whiteColor];
axisTitleStyle.fontName = @"Helvetica-Bold";
axisTitleStyle.fontSize = 8.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:1];
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor whiteColor];
axisTextStyle.fontName = @"Helvetica-Bold";
axisTextStyle.fontSize = 8.0f;
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor whiteColor];
tickLineStyle.lineWidth = 2.0f;
CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor blackColor];
tickLineStyle.lineWidth = 1.0f;
// 2 - Get the graph's axis set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
// 3 - Configure the x-axis
// axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
// axisSet.xAxis.title = @"";
// axisSet.xAxis.labelAlignment = CPTAlignmentRight;
// axisSet.xAxis.titleTextStyle = axisTitleStyle;
// axisSet.xAxis.titleOffset = 38.0f;
// axisSet.xAxis.axisLineStyle = axisLineStyle;
// axisSet.xAxis.labelTextStyle = axisTextStyle;
CGFloat dateCount = [self.arrayData count];
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount];
NSInteger i = 0;
for (ResolutionData *d in self.arrayData)
{
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[[d.appName componentsSeparatedByString:@"-"]objectAtIndex:0] textStyle:axisSet.xAxis.labelTextStyle];
// [label setRotation:45.0];
CGFloat location = i++;
label.tickLocation = CPTDecimalFromCGFloat(location);
label.offset = axisSet.xAxis.majorTickLength;
if (label) {
[xLabels addObject:label];
[xLocations addObject:[NSNumber numberWithFloat:location]];
}
}
axisSet.xAxis.axisLabels = xLabels;
axisSet.xAxis.majorTickLocations = xLocations;
// 4 - Configre the y-axis
CPTAxis *y = axisSet.yAxis;
y.title = @"count";
y.labelAlignment = CPTAlignmentCenter;
y.titleTextStyle = axisTitleStyle;
y.titleOffset = -40.0f;
y.axisLineStyle = axisLineStyle;
y.majorGridLineStyle = gridLineStyle;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelTextStyle = axisTextStyle;
y.labelOffset = 16.0f;
y.majorTickLineStyle = axisLineStyle;
// y.majorTickLength = 4.0f;
// y.minorTickLength = 2.0f;
y.tickDirection = CPTSignPositive;
NSInteger majorIncrement = 20;
NSInteger minorIncrement = 10;
CGFloat yMax = 100.0f; // should determine dynamically based on max price
NSMutableSet *yLabels = [NSMutableSet set];
NSMutableSet *yMajorLocations = [NSMutableSet set];
NSMutableSet *yMinorLocations = [NSMutableSet set];
int x =1;
for (NSInteger j = minorIncrement; j <= yMax; j += minorIncrement) {
NSUInteger mod = j % majorIncrement;
if (mod == 0) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", x] textStyle:y.labelTextStyle];
[label setAlignment:CPTAlignmentTop];
NSDecimal location = CPTDecimalFromInteger(j);x++;
label.tickLocation = location;
label.offset = -y.majorTickLength - y.labelOffset;
if (label) {
[yLabels addObject:label];
}
[yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
} else {
[yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
}
}
y.axisLabels = yLabels;
y.majorTickLocations = yMajorLocations;
y.minorTickLocations = yMinorLocations;
}
请帮助我。创建一个新的y轴对象,根据需要进行配置,然后将其添加到轴集
axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil];
如果希望右y轴具有不同的比例,则需要另一个打印空间。创建一个新的,对其进行配置,将xRange设置为与默认打印空间相同的xRange,然后将其添加到图形中。确保将新y轴的打印空间设置为新的打印空间对象
CPTXYPlotSpace *oldPlotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
CPTXYPlotSpace *newPlotSpace = [[[CPTXYPlotSpace alloc] init] autorelease];
newPlotSpace.xRange = oldPlotSpace.xRange;
newPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:/* location */
length:/* length */];
[graph addPlotSpace:linearPlotSpace];
有关示例代码,请参见Plot Gallery示例应用程序中的轴演示和打印空间演示。创建新的y轴对象,根据需要进行配置,然后将其添加到轴集
axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil];
如果希望右y轴具有不同的比例,则需要另一个打印空间。创建一个新的,对其进行配置,将xRange设置为与默认打印空间相同的xRange,然后将其添加到图形中。确保将新y轴的打印空间设置为新的打印空间对象
CPTXYPlotSpace *oldPlotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
CPTXYPlotSpace *newPlotSpace = [[[CPTXYPlotSpace alloc] init] autorelease];
newPlotSpace.xRange = oldPlotSpace.xRange;
newPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:/* location */
length:/* length */];
[graph addPlotSpace:linearPlotSpace];
有关示例代码,请参见Plot Gallery示例应用程序中的Axis演示和Plot Space演示。:谢谢,它对我有用。但有一个问题我需要根据右侧y轴绘制散点图…现在是从左侧y轴绘制,并取右侧y轴坐标,我怎么做?使用-addPlot:toPlotSpace:方法而不是-addPlot将该图添加到图形中。将绘图空间设置为您为第二个y轴创建的绘图空间。我按照您的建议执行了相同的操作,但它不起作用[self.graph addPlot:actualPlot toPlotSpace:plotSpace2];对于第二个Y轴,我编写以下代码开关字段enum{case cptspatterplotfieldx:if index