0

我可以在核心图的帮助下绘制堆积条形图。我的问题是我无法将标签放在堆叠条形图的顶部。为了创建酒吧,我做了以下事情

在此处输入图像描述

for (NSString *set in [[barChartidentifiers allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]) {
    CPTBarPlot *plot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
    plot.lineStyle = barLineStyle;
    CGColorRef color = ((UIColor *)[barChartidentifiers objectForKey:set]).CGColor;
    plot.fill  = [CPTFill fillWithColor:[CPTColor colorWithCGColor:color]];
    if (firstPlot) {
        plot.barBasesVary = NO;
        firstPlot  = NO;
    } else {
        plot.barBasesVary = YES;
    }
    plot.barWidth = CPTDecimalFromFloat(0.5);
    plot.barsAreHorizontal  = NO;
    plot.labelTextStyle = [CPTTextStyle textStyle];
    plot.dataSource = self;
    plot.identifier = set;
    [graph addPlot:plot toPlotSpace:graph.defaultPlotSpace];
}

下面是我正在编写的代码

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
    NSNumber *num;
    if (fieldEnum == 0) {
        num = [NSNumber numberWithInt:index];
    }

    else {
        double offset = 0;
        if (((CPTBarPlot *)plot).barBasesVary) {
            for (NSString *set in [[barChartidentifiers allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]) {
                if ([plot.identifier isEqual:set]) {
                    break;
                }
                offset += [[[data objectForKey:[dates objectAtIndex:index]] objectForKey:set] doubleValue];


            }
        }

        //Y Value
        if (fieldEnum == 1) {
            num =[NSNumber numberWithDouble:[[[data objectForKey:[dates objectAtIndex:index]] objectForKey:plot.identifier] doubleValue] + offset];
        }

        //Offset for stacked bar
        else {
            num =[NSNumber numberWithDouble:offset];
        }
    }

    //NSLog(@"%@ - %d - %d - %f", plot.identifier, index, fieldEnum, num);

    return num;
}
4

1 回答 1

1

您也许可以调整labelOffset每个情节的 。尝试使用负值将标签放在其绘图上,而不是卡在另一个绘图下。

另一件要尝试的事情是以相反的顺序添加地块,这样较低的地块及其标签就在上面的地块之上。在您的示例中,黄色情节将是第一个,绿色第二个,等等。

如果一切都失败了,请使用绘图空间注释制作自己的标签。这也将在定位标签方面提供更大的灵活性。例如,您可以将标签放在条形旁边而不是顶部。

于 2013-09-18T01:14:48.370 回答