Core Plot ScatterPlot y轴未缩放以适合屏幕和绘图

时间:2012-10-28 15:02:32

标签: iphone ios core-plot

我一直在为我的应用程序制作一组图表,并且ScatterPlot存在一个非常具体的问题。我不能让图形的y轴或高度重新缩放以适应屏幕。我花了几个小时在网上搜索答案,而我最接近的是遵循raywenderlich.com教程。但我仍然无法让我的y轴压缩,以便图形适合屏幕。

理想情况下,我希望图表根据输入数据动态调整大小,因为绘制的数据差异可能从数十到数百不等。

以下是我用来设置图表的代码:

-(void)configureAxes{
     // 1 - Create styles
    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
    axisTitleStyle.color = [CPTColor whiteColor];
    axisTitleStyle.fontName = @"Helvetica-Bold";
    axisTitleStyle.fontSize = 12.0f;
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
    axisLineStyle.lineWidth = 2.0f;
    axisLineStyle.lineColor = [CPTColor whiteColor];
    CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
    axisTextStyle.color = [CPTColor whiteColor];
    axisTextStyle.fontName = @"Helvetica-Bold";
    axisTextStyle.fontSize = 11.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 axis set
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
    // 3 - Configure x-axis
    CPTAxis *x = axisSet.xAxis;
    x.title = @"Date";
    x.titleTextStyle = axisTitleStyle;
    x.titleOffset = 15.0f;
    x.axisLineStyle = axisLineStyle;
    x.labelingPolicy = CPTAxisLabelingPolicyNone;
    x.labelTextStyle = axisTextStyle;
    x.majorTickLineStyle = axisLineStyle;
    x.majorTickLength = 4.0f;
    x.tickDirection = CPTSignNegative;

    //This next line gets the number of records in the array for the date axes
    CGFloat dateCount = [timesTest count];
    NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount];
    NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount];

    //Now we build an array of dates to label the axes
    NSInteger i = 0;
    //for (NSString *date in [[CPDStockPriceStore sharedInstance] datesInMonth]) {
    for (NSString *date in datesTest) {
        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:date  textStyle:x.labelTextStyle];
        CGFloat location = i++;
        label.tickLocation = CPTDecimalFromCGFloat(location);
        label.offset = x.majorTickLength;
        if (label) {
            [xLabels addObject:label];
            [xLocations addObject:[NSNumber numberWithFloat:location]];
        }
    }
    x.axisLabels = xLabels;
    x.majorTickLocations = xLocations;

    // 4 - Configure y-axis
    CPTAxis *y = axisSet.yAxis;
    y.title = @"Time";
    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 = 100;
    NSInteger minorIncrement = 50;
    CGFloat yMax = 700.0f;  // should determine dynamically based on max time
    NSMutableSet *yLabels = [NSMutableSet set];
    NSMutableSet *yMajorLocations = [NSMutableSet set];
    NSMutableSet *yMinorLocations = [NSMutableSet set];
    for (NSInteger j = minorIncrement; j <= yMax; j += minorIncrement) {
        NSUInteger mod = j % majorIncrement;
        if (mod == 0) {
            CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle];
            NSDecimal location = CPTDecimalFromInteger(j);
            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;

}


#pragma mark - CPTPlotDataSource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
    return [timesTest count];
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
    //NSInteger valueCount = [[[CPDStockPriceStore sharedInstance] datesInMonth] count];
    NSInteger valueCount = [timesTest count];
    switch (fieldEnum) {
        case CPTScatterPlotFieldX:
            if (index < valueCount) {
                return [NSNumber numberWithUnsignedInteger:index];
            }
            break;

        case CPTScatterPlotFieldY:
            if ([plot.identifier isEqual:pbTest] == YES) {
                return [pbTest objectAtIndex:index];
            } else if ([plot.identifier isEqual:rollAvgTest] == YES) {
                return [rollAvgTest objectAtIndex:index];
            } else if ([plot.identifier isEqual:timesTest] == YES) {
                return [timesTest objectAtIndex:index];
            }
            break;
    }
    return [NSDecimalNumber zero];
}

这是我创建数组以测试图形的地方:

datesTest = [NSArray arrayWithObjects:@"4/27",
                                            @"4/28",
                                            @"4/29",
                                            nil];

        timesTest = [NSArray arrayWithObjects:
                     [NSDecimalNumber numberWithFloat:601.1],
                     [NSDecimalNumber numberWithFloat:614.4],
                     [NSDecimalNumber numberWithFloat:607.3],
                     nil];

        pbTest = [NSArray arrayWithObjects:
                     [NSDecimalNumber numberWithFloat:601.1],
                     [NSDecimalNumber numberWithFloat:601.1],
                     [NSDecimalNumber numberWithFloat:601.1],
                     nil];

        rollAvgTest = [NSArray arrayWithObjects:
                     [NSDecimalNumber numberWithFloat:602.1],
                     [NSDecimalNumber numberWithFloat:613.4],
                     [NSDecimalNumber numberWithFloat:605.3],
                     nil];

raywenderlich项目编译并完美地运行所需的y轴完美缩放以适应屏幕,但我的不是,但我无法弄清楚我错过了什么。任何有关我出错的建议都将受到大力赞赏。 编辑:添加了Plot Config以回应评论...

-(void)configurePlots{
    // 1 - Get graph and plot space
    CPTGraph *graph = self.hostView.hostedGraph;
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
    // 2 - Create the three plots
    // FIRST PLOT is Personal Best - pbPlot
    // SECOND PLOT is Rolling Average - rollAvgPlot
    // THIRD PLOT is Individual swims - swimssPlot

    //Plot 1 - Peronal Best
    CPTScatterPlot *pbPlot = [[CPTScatterPlot alloc] init];
    pbPlot.dataSource = self;
    NSString *pbID = [[NSString alloc] initWithFormat:@"pbID"];
    pbPlot.identifier = pbID;
    CPTColor *pbColor = [CPTColor redColor];
    [graph addPlot:pbPlot toPlotSpace:plotSpace];

    //Plot 2 - Rolling Average
    CPTScatterPlot *rollAvgPlot = [[CPTScatterPlot alloc] init];
    rollAvgPlot.dataSource = self;
    NSString *rollAvgID = [[NSString alloc] initWithFormat:@"rollAvgID"];
    rollAvgPlot.identifier = rollAvgID;
    CPTColor *rollAvgColor = [CPTColor greenColor];
    [graph addPlot:rollAvgPlot toPlotSpace:plotSpace];

    //Plot 3 - Actual Swim Times
    CPTScatterPlot *swimsPlot = [[CPTScatterPlot alloc] init];
    swimsPlot.dataSource = self;
    NSString *timesID = [[NSString alloc] initWithFormat:@"timesID"];
    swimsPlot.identifier = timesID;
    CPTColor *swimsColor = [CPTColor blueColor];
    [graph addPlot:swimsPlot toPlotSpace:plotSpace];

    // 3 - Set up plot space
    //[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:pbPlot, rollAvgPlot, swimsPlot, nil]];
    [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:swimsPlot, nil]];
    CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
    [xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
    plotSpace.xRange = xRange;
    CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
    [yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)];
    plotSpace.yRange = yRange;

    // 4 - Create styles and symbols
    CPTMutableLineStyle *pbLineStyle = [pbPlot.dataLineStyle mutableCopy];
    pbLineStyle.lineWidth = 2.5;
    pbLineStyle.lineColor = pbColor;
    pbPlot.dataLineStyle = pbLineStyle;
    CPTMutableLineStyle *pbSymbolLineStyle = [CPTMutableLineStyle lineStyle];
    pbSymbolLineStyle.lineColor = pbColor;
    CPTPlotSymbol *pbSymbol = [CPTPlotSymbol ellipsePlotSymbol];
    pbSymbol.fill = [CPTFill fillWithColor:pbColor];
    pbSymbol.lineStyle = pbSymbolLineStyle;
    pbSymbol.size = CGSizeMake(6.0f, 6.0f);
    pbPlot.plotSymbol = pbSymbol;

    CPTMutableLineStyle *rollAvgLineStyle = [rollAvgPlot.dataLineStyle mutableCopy];
    rollAvgLineStyle.lineWidth = 1.0;
    rollAvgLineStyle.lineColor = rollAvgColor;
    rollAvgPlot.dataLineStyle = rollAvgLineStyle;
    CPTMutableLineStyle *rollAvgSymbolLineStyle = [CPTMutableLineStyle lineStyle];
    rollAvgSymbolLineStyle.lineColor = rollAvgColor;
    CPTPlotSymbol *rollAvgSymbol = [CPTPlotSymbol starPlotSymbol];
    rollAvgSymbol.fill = [CPTFill fillWithColor:rollAvgColor];
    rollAvgSymbol.lineStyle = rollAvgSymbolLineStyle;
    rollAvgSymbol.size = CGSizeMake(6.0f, 6.0f);
    rollAvgPlot.plotSymbol = rollAvgSymbol;

    CPTMutableLineStyle *swimsLineStyle = [swimsPlot.dataLineStyle mutableCopy];
    swimsLineStyle.lineWidth = 2.0;
    swimsLineStyle.lineColor = swimsColor;
    swimsPlot.dataLineStyle = swimsLineStyle;
    CPTMutableLineStyle *swimsSymbolLineStyle = [CPTMutableLineStyle lineStyle];
    swimsSymbolLineStyle.lineColor = swimsColor;
    CPTPlotSymbol *swimsSymbol = [CPTPlotSymbol diamondPlotSymbol];
    swimsSymbol.fill = [CPTFill fillWithColor:swimsColor];
    swimsSymbol.lineStyle = swimsSymbolLineStyle;
    swimsSymbol.size = CGSizeMake(6.0f, 6.0f);
    swimsPlot.plotSymbol = swimsSymbol;
}

1 个答案:

答案 0 :(得分:1)

检查您的地图标识符。在设置绘图时将标识符设置为一个值,并将它们与数据源中的其他值进行比较。该测试可能失败,这意味着每个图的每个y值都为零(0)。