如何在CorePlot上显示条形值 – 已绑定的条形图

我已经使用core-plot在我的应用程序中显示堆叠的条形图,我遵循这个漂亮的教程来实现堆积的条形图,现在的graphics如下所示。 “ 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

如何在用户在屏幕上触摸横条的同时在精确位置上显示精确的横杠值?

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