我对 Core Plot 完全陌生,无法在视图中添加图例。我一直在尝试各种教程并阅读了很多信息,但我不明白如何显示图例。
我真的很感激一些帮助,因为我已经花了很多时间来尝试让它发挥作用。
请注意,我此时的目标是显示一个图例,例如:
这是我得到的:
唯一出现的是右边的小粗线。我可以移动它,但不能得到传说。
编码:
// Create pieChart from theme
pieChart = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[pieChart applyTheme:theme];
//Create host view
self.hostingView = [[CPTGraphHostingView alloc]
initWithFrame:[[UIScreen mainScreen]bounds]];
[self.view addSubview:self.hostingView];
hostingView_.hostedGraph = pieChart;
pieChart.axisSet = nil;
pieChart.plotAreaFrame.borderLineStyle = nil;
pieChart.paddingLeft = 10.0;
pieChart.paddingTop = 120.0;
pieChart.paddingRight = 10.0;
pieChart.paddingBottom = 80.0;
pieChart.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
pieChart.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
CPTMutableTextStyle *whiteText = [CPTMutableTextStyle textStyle];
whiteText.color = [CPTColor whiteColor];
whiteText.fontSize = 14;
whiteText.fontName = @"Helvetica";
pieChart.titleTextStyle = whiteText;
//==================================================//
// LEGENDS //
// 1 - Get graph instance
CPTGraph *graph = self.hostingView.hostedGraph;
// 2 - Create legend
CPTLegend *theLegend = [CPTLegend legendWithGraph:graph];
// 3 - Configure legen
theLegend.numberOfColumns = 1;
theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
theLegend.borderLineStyle = [CPTLineStyle lineStyle];
theLegend.cornerRadius = 5.0;
// 4 - Add legend to graph
graph.legend = theLegend;
graph.legendAnchor = CPTRectAnchorRight;
CGFloat legendPadding = -(self.view.bounds.size.width / 8);
graph.legendDisplacement = CGPointMake(legendPadding, 0.0);
//==================================================//
// Add pie chart
CPTPieChart *piePlot = [[CPTPieChart alloc] init];
piePlot.dataSource = self;
piePlot.pieRadius = 70.0; //140.0; //131.0;
piePlot.identifier = @"Pie Chart 1";
piePlot.startAngle = M_PI_4;
piePlot.sliceDirection = CPTPieDirectionCounterClockwise; //CPTPieDirectionCounterClockwise;
piePlot.centerAnchor = CGPointMake(0.5, 0.5); //(0.5, 0.38);
piePlot.borderLineStyle = [CPTLineStyle lineStyle];
piePlot.delegate = self;
[pieChart addPlot:piePlot];
xx
-(NSString *)legendTitleForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index {
return [NSString stringWithFormat:@"Pie Slice %lu", (unsigned long)index];
}