核心图:ios在同一CPTGraph中显示两个y轴和ScaterPlot和Bar Plot

时间:2013-04-19 09:25:36

标签: iphone ios ios5 core-plot

嗨,请指导我使用核心图显示两个Y轴(左侧和右侧)和一个x轴,并且我必须在单个CPTGraph中绘制Scaterplot和barplot。 CPTScatter图显示在右侧Y轴上,带有-ve方向。

我的代码段

-(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;
}

请帮帮我。

1 个答案:

答案 0 :(得分:4)

创建一个新的y轴对象,根据需要对其进行配置,然后将其添加到轴集中。

axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil];

如果要为右侧y轴设置不同的比例,则需要另一个绘图空间。创建一个新的,配置它,将xRange设置为与默认绘图空间相同的xRange,并将其添加到图形中。确保新的y轴将plotSpace设置为新的绘图空间对象。

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 Demo”和“Plot Space Demo”以获取示例代码。

相关问题