首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CorePlot中的多个(分组)条

CorePlot中的多个(分组)条
EN

Stack Overflow用户
提问于 2016-04-01 16:17:43
回答 1查看 89关注 0票数 0

我需要创建一个图表,将每个x轴分成3条,类似于这个图像:

我正在创建的图表如下所示:

我的代码:

constructEVDChart

代码语言:javascript
复制
- (void)constructEVDChart
{
    CPTXYGraph *newGraph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];

    CPTTheme *theme = [CPTTheme themeNamed:@"Sales Farma"];
    [newGraph applyTheme:theme];

    self.hostingView.hostedGraph = newGraph;
    self.barChart = newGraph;

    newGraph.plotAreaFrame.masksToBorder = NO;
    newGraph.paddingLeft   = 60.0;
    newGraph.paddingTop    = 40.0;
    newGraph.paddingRight  = 20.0;
    newGraph.paddingBottom = 50.0;

    // Create grid line styles
    CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
    majorGridLineStyle.lineWidth = 1.0;
    majorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.15];

    // Add plot space for horizontal bar charts
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)newGraph.defaultPlotSpace;
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:@0.0 length:@30.0];
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:@0.0 length:@15.0];

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)newGraph.axisSet;
    CPTXYAxis *x          = axisSet.xAxis;
    {
        x.axisLineStyle       = nil;
        x.majorTickLineStyle  = nil;
        x.minorTickLineStyle  = nil;
        x.majorIntervalLength = @2.5;
        x.orthogonalPosition  = @0.0;
        x.majorGridLineStyle = majorGridLineStyle;

        // Define some custom labels for the data elements
        //x.labelRotation  = CPTFloat(M_PI_4);
        x.labelingPolicy = CPTAxisLabelingPolicyNone;
    }

    CPTNumberArray customTickLocations  = @[@1, @5, @10, @15];
    CPTStringArray xAxisLabels          = @[@"Label A", @"Label B", @"Label C", @"Label D"];
    NSUInteger labelLocation            = 0;

    NSNumber * vTickLocation = @1.5;

    CPTMutableAxisLabelSet customLabels = [NSMutableSet setWithCapacity:xAxisLabels.count];
    for ( NSNumber *tickLocation in customTickLocations ) {
        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:xAxisLabels[labelLocation++] textStyle:x.labelTextStyle];
        newLabel.tickLocation = vTickLocation;
        newLabel.offset       = x.labelOffset + x.majorTickLength;
        [customLabels addObject:newLabel];

        vTickLocation = [NSNumber numberWithFloat:([vTickLocation floatValue] + 2.5)];
    }

    x.axisLabels = customLabels;

    CPTXYAxis *y = axisSet.yAxis;
    {
        y.axisLineStyle       = nil;
        y.majorTickLineStyle  = nil;
        y.minorTickLineStyle  = nil;
        y.majorIntervalLength = @5.0;
        y.orthogonalPosition  = @0.0;
        y.majorGridLineStyle = majorGridLineStyle;
    }

    // Profissional
    CPTBarPlot *profBar = [[CPTBarPlot alloc] init];
    profBar.dataSource = self;
    profBar.barOffset  = @0.50;
    profBar.barWidth   = @0.50;
    profBar.identifier = @"Bar Plot 1";
    profBar.delegate        = self;
    profBar.fill = [CPTFill fillWithColor:[CPTColor getCustomSFAColor:[UIColor sfaGraphOrangeColor]]];
    profBar.lineStyle = nil;
    [newGraph addPlot:profBar toPlotSpace:plotSpace];
    [profBar release];

    // PDV
    CPTBarPlot *pdvBar = [[CPTBarPlot alloc] init];
    pdvBar.dataSource      = self;
    pdvBar.barOffset       = @1.25;
    pdvBar.barWidth        = @0.50;
    pdvBar.identifier      = @"Bar Plot 2";
    pdvBar.delegate        = self;
    pdvBar.fill = [CPTFill fillWithColor:[CPTColor getCustomSFAColor:[UIColor sfaGraphBlueColor]]];
    pdvBar.lineStyle = nil;
    [newGraph addPlot:pdvBar toPlotSpace:plotSpace];
    [pdvBar release];

    // Estab
    CPTBarPlot *estabBar = [[CPTBarPlot alloc] init];
    estabBar.dataSource      = self;
    estabBar.barOffset       = @2.00;
    estabBar.barWidth        = @0.50;
    estabBar.identifier      = @"Bar Plot 3";
    estabBar.delegate        = self;
    estabBar.fill = [CPTFill fillWithColor:[CPTColor getCustomSFAColor:[UIColor sfaGraphGreenColor]]];
    estabBar.lineStyle = nil;
    [newGraph addPlot:estabBar toPlotSpace:plotSpace];
    [estabBar release];
}

numberOfRecordsForPlot

代码语言:javascript
复制
- (NSUInteger)numberOfRecordsForPlot:(nonnull CPTPlot *)plot
{
    return 4;
}

numberForPlot

代码语言:javascript
复制
- (nullable id)numberForPlot:(nonnull CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    NSNumber *num = nil;

    switch ( fieldEnum ) {
        case CPTBarPlotFieldBarLocation:
        {
            num = @(index);
            break;
        }
        case CPTBarPlotFieldBarTip:
            num = @( (index + 1) * 2 );
            break;
    }

    return num;
}

dataLabelForPlot

代码语言:javascript
复制
- (nullable CPTLayer *)dataLabelForPlot:(nonnull CPTPlot *)plot recordIndex:(NSUInteger)index
{
    static CPTMutableTextStyle *whiteText = nil;
    static dispatch_once_t onceToken      = 0;

    dispatch_once(&onceToken, ^{
        whiteText = [[CPTMutableTextStyle alloc] init];
        whiteText.color = [CPTColor whiteColor];
    });

    CPTTextLayer *newLayer = nil;

    if ( [plot isKindOfClass:[CPTPieChart class]] ) {
        switch ( index ) {
            case 0:
                newLayer = (id)[NSNull null];
                break;

            default:
                newLayer = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%lu", (unsigned long)index] style:whiteText];
                break;
        }
    }
    else if ( [plot isKindOfClass:[CPTScatterPlot class]] ) {
        newLayer = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%lu", (unsigned long)index] style:whiteText];
    }

    return newLayer;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-02 15:37:33

这些条子相隔一个单位,但由于它们的宽度为0.5,不管你做什么,它们都会重叠。若要匹配示例图,请将每个条形图的宽度设为0.25。这使得所有三个地块在每组之间的差距为0.25。使棒材宽度稍微小一点(例如0.2),在相邻的酒吧之间留下一个很小的间隙。我会使用- 0.25,0.0和0.25的条形偏移。这将使分组集中在条形图的位置上,并在组之间留下一些空间。

数据标签没有显示,因为-dataLabelForPlot:recordIndex:总是返回nil。图是一个CPTBarPlot,而数据标签方法只为饼图和散点图创建标签。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36361117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档