【问题标题】:Core plot : ios show two y axis and ScaterPlot and Bar Plot in Same CPTGraph核心图:ios 在同一 CPTGraph 中显示两个 y 轴和 ScaterPlot 和条形图
【发布时间】:2013-04-19 09:25:36
【问题描述】:

您好,请指导我编写方法来使用核心图显示两个 Y 轴(左侧和右侧)和一个 x 轴,而且我必须在单个 CPTGraph 中绘制散点图和条形图。 CPTScatter 图显示在右侧 Y 轴上,方向为 -ve。

我的代码 sn-p

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

请帮帮我。

【问题讨论】:

    标签: iphone ios ios5 core-plot


    【解决方案1】:

    创建一个新的 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”以获取示例代码。

    【讨论】:

    • :谢谢它对我有用。但是一个问题我需要根据右侧y轴绘制散点图......现在它是从左侧y轴绘制并获取右侧y轴坐标我该怎么做?
    • 使用-addPlot:toPlotSpace: 方法而不是-addPlot 将绘图添加到图形中。将绘图空间设置为您为第二个 y 轴创建的空间。
    • 我按照你的建议做了同样的事情,但它不起作用 [self.graph addPlot:actualPlot toPlotSpace:plotSpace2];
    • 对于第二个 Y 轴,我写了下面的代码开关 (fieldEnum) { case CPTScatterPlotFieldX: if (index
    • 如何分享屏幕截图..?
    猜你喜欢
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多