Core Plot Legent do not show

core-plot, objective-c

Solution

Add the plot to the graph before you set up the legend. You could also use the `-addPlot:` method on the legend directly, but it's easier to let the graph do it.

Problem

I am totally new to Core Plot and I am not able to add a Legend to my view. I have been trying all kinds of tutorial's and read a lot of information but am i do not understand how to display the legend. I would really appreciate some help as i have been spending quite some time to try to get this to work. Be aware that my aim at this point is to display a legend such as: Here is what i get: The only thing that show up is the while little thick line on the right. I can move that but not get the legend. The code: ``` // 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]; } ```

Original source