如何在CorePlot上显示条形值-堆叠条形图 [英] How to show bar values on CorePlot -Stacked bar chart

查看:82
本文介绍了如何在CorePlot上显示条形值-堆叠条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 core-plot 在我的应用程序中显示堆积的条形图,我按照这个不错的教程来实现堆积的条形图,现在该图如下所示。



这里我遇到了几个问题



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


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

如果我单击蓝色条,则显示准确酒吧的价值。
显示值= 8



如果单击绿色条,我将获得蓝色条值和绿色条值。
显示值= 14



如果单击红色条,我将获得累积总值蓝色,绿色,红色的酒吧。
显示值= 21



如何在所选条形上显示准确的条形值和精确位置当用户触摸屏幕上的条形图时?

解决方案

问题中的代码显示所选条形的提示值。在这种情况下,这就是栏的顶部。如果要选择选定线段的长度,请减去该条的基本值。


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?

解决方案

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.

这篇关于如何在CorePlot上显示条形值-堆叠条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆