CORE PLOT 在垂直条形图中再添加一张图表

发布于 2024-11-01 03:13:54 字数 310 浏览 2 评论 0原文

您好,我想制作条形图,如下所示

在此处输入图像描述

我想使用 coreplot 示例绘制图表:(coreplot gallery - 垂直条形图)

当前默认代码绘制的图表如下所示

在此处输入图像描述

我怎样才能实现这一点?

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

请帮助和建议

谢谢

Hi I want to make bar chart as shown here

enter image description here

I want to plot graph using coreplot example : (coreplot gallery- Vertical Bar Chart)

Currently default code plots the graph as shown below

enter image description here

How can I achieve this?

I tried adding another graph but didn't work out well.

Please Help and Suggest

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不会写诗 2024-11-08 03:13:54

这是我的解决方案:
在此处输入图像描述

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

-(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) 负载图中执行此操作:

//  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 轴以下的值,您需要更改范围:

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

希望这会有所帮助。

Here is my solution:
enter image description here

In your code you need to add the following:

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

The lines plot.identifier are here important, they check which data to take, here occupied empty or locked.

Of course you need to set these identifiers, I do this in a -(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];

Here one of my identifier is set. To display values below the x-axe you need to change your range:

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

Hope this helps.

单调的奢华 2024-11-08 03:13:54

我是这样做的:

    // 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”并将它们放入另一个数组中,然后可以将该数组放在图形上。
希望这有帮助。

This is how I do this:

    // 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];

You define 2 array, one with the labels and one with the values where to put them, define "CPXAxisLabel"s and put them in another array, and this array you can put on your graph.
Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文