【问题标题】:How to show bar values on CorePlot -Stacked bar chart如何在 CorePlot -Stacked 条形图上显示条形值
【发布时间】:2015-10-29 05:42:57
【问题描述】:

我使用core-plot 在我的应用程序中显示堆叠条形图,我按照这个不错的教程来实现堆叠条形图,现在图表如下所示。 "http://www.gilthonwe.com/2012/06/09/stacked-bar-chart-coreplot-ios/"

当用户在屏幕上触摸它们时,我使用下面的代码来显示条形值。

 -(void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)index
    {
        if (plot.isHidden == YES) {
            return;
        }
        static CPTMutableTextStyle *style = nil;
        if (!style) {
            style = [CPTMutableTextStyle textStyle];
            style.color= [CPTColor yellowColor];
            style.fontSize = 16.0f;
            style.fontName = @"Helvetica-Bold";
        }

        NSNumber *price = [NSNumber numberWithDouble:[self doubleForPlot:plot field:CPTBarPlotFieldBarTip recordIndex:index]];
        if (!self.priceAnnotation) {
            NSNumber *x = [NSNumber numberWithInt:0];
            NSNumber *y = [NSNumber numberWithInt:0];
            NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil];
            self.priceAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:plot.plotSpace anchorPlotPoint:anchorPoint];
        }
        static NSNumberFormatter *formatter = nil;
        if (!formatter) {
            formatter = [[NSNumberFormatter alloc] init];
            [formatter setMaximumFractionDigits:2];
        }
        // 5 - Create text layer for annotation
        NSString *priceValue = [formatter stringFromNumber:price];
        CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:priceValue style:style];
        self.priceAnnotation.contentLayer = textLayer;
        NSLog(@"barWasSelectedAtRecordIndex %lu", (unsigned long)index);
        NSInteger plotIndex = 0;
        if ([plot.identifier isEqual:[sets objectForKey:@"Due"]] == YES) {
            plotIndex = 0;
        } else if ([plot.identifier isEqual:[sets objectForKey:@"Overdue"]] == YES) {
            plotIndex = 1;
        } else if ([plot.identifier isEqual:[sets objectForKey:@"Paid"]] == YES) {
            plotIndex = 2;
        }

        CGFloat x =10.00;
        NSNumber *anchorX = [NSNumber numberWithFloat:x];
        CGFloat y = 10.00;
        NSNumber *anchorY = [NSNumber numberWithFloat:y];
        self.priceAnnotation.anchorPlotPoint = [NSArray arrayWithObjects:anchorX, anchorY, nil];
        // 8 - Add the annotation
        [plot.graph.plotAreaFrame.plotArea addAnnotation:self.priceAnnotation];
    }

上述代码的响应见图

这里我面临几个问题

1.值不会显示在所选栏上方的确切位置。

  1. 考虑蓝条值=8,绿条值=6,红条值=7

如果我单击蓝色条,则会显示该条的确切值。 Display Value=8

如果我单击绿条,我将获得蓝条值和绿条值的累积总和值。 Display Value=14

如果我单击红色条,我将获得蓝色、绿色、红色条的累积总和值。 Display Value=21

当用户触摸屏幕中的条时,如何在所选条上显示确切位置的确切条值?

【问题讨论】:

    标签: ios bar-chart xcode7 core-plot


    【解决方案1】:

    问题中的代码显示所选柱的“提示”值。在这种情况下,这是栏的顶部。如果您想要选定段的长度,请减去条的“基”值。

    【讨论】:

    • 伟大的 Eric,请提供任何代码示例。我得到了 y 位置值,请问如何得到 x 位置。
    • CPTBarPlotFieldBarBase字段中获取绘图数据并取差。
    • 我已经设法获得了 x, y 位置来显示值,如何从代码中获取 CPTBarPlotFieldBarBase ?
    • NSNumber *basePrice = @([self doubleForPlot:plot field:CPTBarPlotFieldBarBase recordIndex:index]);
    • 谢谢。埃里克它有效。 NSNumber *priceTip = [NSNumber numberWithDouble:[self doubleForPlot:plot field:CPTBarPlotFieldBarTip recordIndex:index]]; NSNumber *priceBase = [NSNumber numberWithDouble:[self doubleForPlot:plot field:CPTBarPlotFieldBarBase recordIndex:index]]; NSNumber *price=[NSNumber numberWithFloat:( [priceTip floatValue]-[priceBase floatValue])];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 2017-11-13
    相关资源
    最近更新 更多