CORE PLOT在Vertical BarChart中再添加一个Graph

时间:2011-04-14 13:17:07

标签: iphone objective-c cocoa-touch core-plot

您好我想制作如此处所示的条形图

enter image description here

我想使用coreplot示例绘制图形:( coreplot gallery- Vertical Bar Chart)

目前默认代码绘制图形如下所示

enter image description here

我怎样才能做到这一点?

我尝试添加另一张图表但效果不佳。

请帮助和建议

由于

2 个答案:

答案 0 :(得分:1)

这是我的解决方案: enter image description here

在您的代码中,您需要添加以下内容:

-(NSNumber *)numberForPlot:(CPPlot *)plot
                 field:(NSUInteger)fieldEnum
           recordIndex:(NSUInteger)index
{
    if (plot.identifier==@"locked") {
        switch ( fieldEnum ) {
            case CPBarPlotFieldBarLocation:
                return (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
            case CPBarPlotFieldBarLength:
                return [self.lockedPercentage objectAtIndex:index];
            }
    }else if (plot.identifier==@"occupied") {
        switch ( fieldEnum ) {
            case CPBarPlotFieldBarLocation:
                return (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
            case CPBarPlotFieldBarLength:
                return [self.occupiedPercentage objectAtIndex:index];
        }
    }else if (plot.identifier==@"empty") {
        switch ( fieldEnum ) {
            case CPBarPlotFieldBarLocation:
                return (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
            case CPBarPlotFieldBarLength:
                return [self.emptyPercentage objectAtIndex:index];
        }
    }
    return nil;
}

线plot.identifier在这里很重要,他们检查要采取的数据,这里占用空或锁定。

当然你需要设置这些标识符,我在 - (void)loadgraph:

中这样做
//  Bar plot Locked
CPBarPlot *barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor blueColor] horizontalBars:NO];
barPlot.dataSource = self;
barPlot.baseValue = CPDecimalFromString(@"0");
barPlot.barOffset = 0.0f+viewOffset;
barPlot.barWidth = 15.0f;
barPlot.identifier = @"occupied";

[graph addPlot:barPlot toPlotSpace:plotSpace];

这里设置了我的一个标识符。要显示x-ax下方的值,您需要更改范围:

    plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(100.0f)];

希望这有帮助。

答案 1 :(得分:1)

我就是这样做的:

    // Define some custom labels for the data elements

customTickLocations = [NSArray arrayWithObjects: 
                           [NSDecimalNumber numberWithInt:10], 
                           [NSDecimalNumber numberWithInt:20],
                           [NSDecimalNumber numberWithInt:30],
                           [NSDecimalNumber numberWithInt:40],
                           [NSDecimalNumber numberWithInt:50],
                           [NSDecimalNumber numberWithInt:60],
                           [NSDecimalNumber numberWithInt:70],
                           [NSDecimalNumber numberWithInt:80],
                           [NSDecimalNumber numberWithInt:90],
                           [NSDecimalNumber numberWithInt:100],
                           nil];

NSArray *xAxisLabels = [NSArray arrayWithObjects:@"10%", @"20%", @"30%",
                            @"40%", @"50%", @"60%",@"70%",@"80%",@"90%",@"100%", nil];

labelLocation = 0;
customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];

for (NSNumber *tickLocation in customTickLocations) {
    CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] 
                                                    textStyle:y.labelTextStyle];
    newLabel.tickLocation = [tickLocation decimalValue];
    newLabel.offset = y.labelOffset;
    [customLabels addObject:newLabel];
    //[newLabel release];
}

y.axisLabels =  [NSSet setWithArray:customLabels];
y.majorTickLocations =  [NSSet setWithArray:customTickLocations];

您定义了2个数组,一个带有标签,另一个带有放置它们的值,定义“CPXAxisLabel”并将它们放在另一个数组中,这个数组可以放在图表上。 希望这会有所帮助。