How to show bar values on CorePlot -Stacked bar chart

梦想与她 提交于 2019-12-11 10:22:17

问题


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.

  1. 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?


回答1:


The code in the question displays the "tip" value of the selected bar. In this case, that's the top of the bar. If you want the length of the selected segment, subtract the "base" value of the bar.



来源:https://stackoverflow.com/questions/33406743/how-to-show-bar-values-on-coreplot-stacked-bar-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!