如何构造具有可变段计数的核心图多栏

时间:2016-08-03 22:10:21

标签: bar-chart core-plot

[简明]来源于InfoBarView.m

我想看到什么 enter image description here

我得到了什么 enter image description here

我的plotData是一个项目数组:

- (void)generateData
{
    InfoBarView * barView = (id)[[self defaultLayerHostingView] superview];
    InfoController * controller = barView.controller;
    Group * group = controller.group;

    self.plotData = group.items;
}

这对我来说似乎很好,而且这些图表呈现为:

//  Create bar plot(s)
for (Item * item in group.items)
{
    static int offsets[] = { -2, -1, 0, 1, 2 };
    int segsCount=0;

    for (NSString * getter in @[ @"fldA", @"fldB",  @"fldC", @"fldD" ])
    {
        barPlot = [[[CPTBarPlot alloc] init] autorelease];

        NSLog(@"render:%lu '%@' field:%d %@", plotCount++, item.name, segsCount, getter);
        barPlot.lineStyle           = barLineStyle;
        barPlot.barCornerRadius     = 4.0;
        barPlot.barsAreHorizontal   = NO;
        barPlot.dataSource          = self;
        barPlot.delegate            = self;
        barPlot.identifier          = @{ kTarget : item,
                                         kGetter : getter },
        barPlot.barOffset           = @(offsets[segsCount]);
        barPlot.barBasesVary        = (segsCount++ > 0);
        barPlot.barWidth            = @(segsCount * .25f);

        [graph addPlot:barPlot toPlotSpace:barPlotSpace];
    }
}

但是这里我感到很困惑;我正试图获得'n'条,每条都有4个段。我试图了解我的数据源应该响应什么;目前:

- (nullable id)numberForPlot:(nonnull CPTPlot *)plot 
                       field:(NSUInteger)fieldEnum
                 recordIndex:(NSUInteger)index
{
    InfoBarView * barView = (id)[[self defaultLayerHostingView] superview];
    InfoController * controller = barView.controller;
    Group * group = controller.group;

    NSDictionary * data = (NSDictionary *)plot.identifier;
    SEL dataItem = NSSelectorFromString(kTargets[controller.barDATAIndex]);
    SEL getter = NSSelectorFromString(data[kGetter]);
    SEL starts = @selector(fldD);
    Item * item = data[kTarget];
    CGFloat runs, value=0;
    id num=nil, data;

    //  First get which otpj block we're after
    data = [item performSelector:dataItem];
    runs = [[data performSelector:starts] floatValue];

    //  Now fetch its getter
    switch ( fieldEnum )
    {
        case CPTBarPlotFieldBarLocation:
            num = @(index % group.itemCount);
            break;

        case CPTBarPlotFieldBarTip:
            value = [[data performSelector:getter] floatValue];
            num = @(value / runs);
            break;

        case CPTBarPlotFieldBarBase:
            //  Calc base by sum prior fields
            for (fpos_type_t fpos=Win; fpos < (index % 4); fpos++)
            {
                value += ([[data performSelector:NSSelectorFromString(kGetters[fpos])] floatValue] / runs);
            }
            num = @(value);
    }

    NSLog(@"numberForPlot:'%@' record:%lu field:%lu %@ = %@", item.name, index, fieldEnum, data[kGetter], num);

    return num;
}

我认为recordIndex应为0..group.items.count;这些条中的每一条都有4个段0..4。因为我的plot.identifier是项目对象,我可以从那里获取我需要的东西;首先是子记录数据对象。然后是该数据记录中的一个字段。

我认为我的数据源应该返回 CPTBarPlotFieldBarLocation:0..group.items.count CPTBarPlotFieldBarTip:seg的值 CPTBarPlotFieldBarBase:seg

的起始垂直偏移量

最后,关于条形填充,我想采用项目颜色来获得一系列不断增加的黑暗颜色,以便组内的颜色具有相同的色调。

所以你可以看到我得到的只是一个blob;在我的第一张照片中应该有每个有希望分组的单独的酒吧项目。对于初学者来说,我只是试图让seg超过另一个相同的宽度,但最终确实希望seg的宽度不同以暴露更多。

0 个答案:

没有答案