iphone - Core-Plot(饼图)中的图例问题

标签 iphone objective-c core-plot pie-chart

我在使用 Core-Plot 绘制饼图图例时遇到问题,因为图例中图表的每个元素的名称始终是 CPTPieChart 的标识符。有人能帮我吗?谢谢。

这是源代码:

-(void)constructPieChart {
    // Create pieChart from theme
    //[pieGraph applyTheme:theme];
    pieChartView.hostedGraph = pieGraph;
    pieGraph.plotAreaFrame.masksToBorder = YES;

    pieGraph.paddingLeft = 0;
    pieGraph.paddingTop = 20.0;
    pieGraph.paddingRight = 0;
    pieGraph.paddingBottom = 60.0;

    pieGraph.axisSet = nil;

    // Prepare a radial overlay gradient for shading/gloss
    CPTGradient *overlayGradient = [[[CPTGradient alloc] init] autorelease];
    overlayGradient.gradientType = CPTGradientTypeRadial;
    overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.0] atPosition:0.0];
    overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.3] atPosition:0.9];
    overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.7] atPosition:1.0];

    // Add pie chart
    piePlot = [[CPTPieChart alloc] init];
    piePlot.dataSource = self;
    piePlot.delegate = self;
    piePlot.pieRadius = 80.0;
    piePlot.identifier = @"Pie Chart 2";
    piePlot.startAngle = M_PI_2;
    piePlot.sliceDirection = CPTPieDirectionClockwise;
    piePlot.borderLineStyle = [CPTLineStyle lineStyle];
    //piePlot.sliceLabelOffset = 5.0;
    piePlot.overlayFill = [CPTFill fillWithGradient:overlayGradient];

    [pieGraph addPlot:piePlot];

    pieGraph.title=@"GRAFICA SECTORES";

    [piePlot release];

    // Add some initial data
    NSMutableArray *contentArray = [NSMutableArray arrayWithObjects:
                                    [NSNumber numberWithDouble:20.0], 
                                    [NSNumber numberWithDouble:40.0],
                                    [NSNumber numberWithDouble:30.0], 
                                    [NSNumber numberWithDouble:23], 
                                    [NSNumber numberWithDouble:60.0], 
                                    nil];
    self.dataForChart = contentArray;


    // Add legend
    CPTLegend *theLegend = [CPTLegend legendWithGraph:pieGraph];
    theLegend.numberOfColumns = 2;
    theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
    theLegend.borderLineStyle = [CPTLineStyle lineStyle];
    theLegend.cornerRadius = 5.0;

    pieGraph.legend = theLegend;

    pieGraph.legendAnchor = CPTRectAnchorBottom;
    pieGraph.legendDisplacement = CGPointMake(0.0, 30.0);
}

所以在传说中我总是“饼图2”。

PS:抱歉,因为我的英语不好。

最佳答案

您需要将此方法添加到您的数据源中:

-(NSString *)legendTitleForPieChart:(CPTPieChart *)pieChart
                        recordIndex:(NSUInteger)index;

将为每个索引(对应于每个饼图切片)调用它。只需为每个返回正确的标题字符串即可。

关于iphone - Core-Plot(饼图)中的图例问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7253533/

相关文章:

iphone - 更改 xib 时 iPhone 上的 SIGABRT

iphone - 在 uiwebview 中播放 youtube 视频。如何处理 "done"按钮?

iOS:文档目录为 'cleaned'

ios - 为什么隐藏状态栏会完全破坏我的简单动画?

ios - CPTBarPlot (Core-Plot) 上的定位标签

iphone - 核心情节: bars disappears when switching app to background and then to foreground while drawing a bar graph

swift - Core Plot 中的可点击图例

ios - 从使用 AVAssetImageGenerator 获得的视频或拇指获取方向

ios - 文本字段仅返回 1 个字母或 NULL 的奇怪问题

ios - 创建后 UITableViewCell 的顺序不正确