I have used core-plot to display stacked bar chart in my application, I followed this nice tutorial to implement the stacked bar chart, now the graph looks like below.
"http://www.gilthonwe.com/2012/06/09/stacked-bar-chart-coreplot-ios/"
I used the below code to display the bar values while user touches them on the screen.
-(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];
}
See the figure for the response of the above code
Here I face couple of issues
1.The values are not displayed at the exact position above the selected bar.
- Consider Blue bar value=8, Green bar value=6, Red bar value=7
If I click the Blue bar displays the exact Value of the bar.
Display Value=8
If I click the Green bar I am getting the cumulative sum value of both Blue bar value and Green bar value.
Display Value=14
If I click the the red bar I am getting the cumulative sum value of Blue,Green,Red bars.
Display Value=21
How to display the exact bar value with exact position over the selected bar while user touches bars in the screen?
