Legends Pie Chart

358 views
Skip to first unread message

nitesh dulal

unread,
Aug 4, 2011, 5:42:43 AM8/4/11
to coreplot-discuss
HI
i am trying to show legend for my pie chart
i succesfully drew my pie chart using core plot
but when i add legends the pie chart just doesnt show up
the delegate method -(NSNumber *)numberForPlot:(CPTPlot *)plot field:
(NSUInteger)fieldEnum recordIndex:(NSUInteger)index doesnt get called
at all
when i remove the legends the pie diagram is shown
can please help me out or tell me what i might be missing
regards

Eric

unread,
Aug 4, 2011, 7:46:33 AM8/4/11
to coreplot-discuss
That shouldn't happen. Can you post the code you use to set up the
legend?

Eric

nitesh dulal

unread,
Aug 8, 2011, 12:39:42 AM8/8/11
to coreplot-discuss
hi this is the code
.h file
@interface PieChart : UIViewController
<CPTPieChartDataSource,CPTPlotSpaceDelegate,
CPTPlotDataSource>
{
IBOutlet CPTGraphHostingView *pieChartGraphView;
CPTXYGraph *pieChartGraph;
NSMutableArray *chartData;
NSMutableArray *description;
}

.m file

- (void)viewDidLoad
{
[super viewDidLoad];

chartData=[[NSMutableArray alloc]init];

pieChartGraph=[[CPTXYGraph alloc]initWithFrame:[pieChartGraphView
bounds]];
pieChartGraphView.hostedGraph=pieChartGraph;
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[pieChartGraph applyTheme:theme];

pieChartGraph.paddingLeft = 20.0;
pieChartGraph.paddingTop = 20.0;
pieChartGraph.paddingRight = 20.0;
pieChartGraph.paddingBottom = 20.0;
pieChartGraph.axisSet = nil;


CPTPieChart *piePlot = [[CPTPieChart alloc] init];
piePlot.dataSource = self;
piePlot.delegate=self;

piePlot.pieRadius = 100.0;
piePlot.identifier = @"Pie Chart 1";
piePlot.startAngle = M_PI_4;
piePlot.sliceDirection = CPTPieDirectionCounterClockwise;



[pieChartGraph addPlot:piePlot];
[piePlot release];

CPTLegend *theLegend = [CPTLegend legendWithGraph:pieChartGraph];
theLegend.numberOfColumns = 1;
theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
theLegend.borderLineStyle = [CPTLineStyle lineStyle];
theLegend.cornerRadius = 5.0;

pieChartGraph.legend = theLegend;

pieChartGraph.legendAnchor = CPTRectAnchorBottom;//
CPTRectAnchorRight;
pieChartGraph.legendDisplacement = CGPointMake(-16 -10, 0.0);

NSMutableArray *contentArray = [NSMutableArray arrayWithObjects:
[NSNumber numberWithDouble:20.0], [NSNumber numberWithDouble:30.0],
[NSNumber numberWithDouble:60.0], nil];
self.chartData = contentArray;


}

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
return [self.chartData count];
}


-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum
recordIndex:(NSUInteger)index
{
if ( index >= [self.chartData count] ) return nil;

if ( fieldEnum == CPTPieChartFieldSliceWidth ) {
return [self.chartData objectAtIndex:index];
}
else {
return [NSNumber numberWithInt:index];
}
}


-(NSString *)legendTitleForPieChart:(CPTPieChart *)pieChart
recordIndex:(NSUInteger)index
{
return [NSString stringWithFormat:@"Pie Slice %u", index];
}

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:
(NSUInteger)index
{
static CPTMutableTextStyle *whiteText = nil;

if ( !whiteText ) {
whiteText = [[CPTMutableTextStyle alloc] init];
whiteText.color = [CPTColor whiteColor];
}

CPTTextLayer *newLayer = [[[CPTTextLayer alloc] initWithText:
[NSString stringWithFormat:@"%3.0f", [[chartData objectAtIndex:index]
floatValue]] style:whiteText] autorelease];
return newLayer;
}

-(IBAction)dismiss{
[self.parentViewController
dismissModalViewControllerAnimated:YES];
}

Hopefully u can point out where m goin wrong
thnsk again :)

Eric

unread,
Aug 8, 2011, 7:41:13 PM8/8/11
to coreplot-discuss
I pasted your code into another project and except for the typo in
legendDisplacement, it worked fine for me.

Eric

pyanfield

unread,
Sep 20, 2011, 10:44:17 AM9/20/11
to coreplot-discuss
why I copied the code to my project.
There is nothing displayed.
> >     piePlot.identifier = @"PieChart 1";
> >         return [NSString stringWithFormat:@"PieSlice %u", index];
>
> > }
>
> > -(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:
> > (NSUInteger)index
> > {
> >     static CPTMutableTextStyle *whiteText = nil;
>
> >     if ( !whiteText ) {
> >         whiteText = [[CPTMutableTextStyle alloc] init];
> >         whiteText.color = [CPTColor whiteColor];
> >     }
>
> >     CPTTextLayer *newLayer = [[[CPTTextLayer alloc] initWithText:
> > [NSString stringWithFormat:@"%3.0f", [[chartData objectAtIndex:index]
> > floatValue]] style:whiteText] autorelease];
> >     return newLayer;
>
> > }
>
> > -(IBAction)dismiss{
> >     [self.parentViewController
> > dismissModalViewControllerAnimated:YES];
>
> > }
>
> > Hopefully u can point out where m goin wrong
> > thnsk again :)
>
> > On Aug 4, 4:46 pm, Eric <eskr...@mac.com> wrote:
>
> > > That shouldn't happen. Can you post the code you use to set up the
> > > legend?
>
> > > Eric
>
> > > On Aug 4, 5:42 am, nitesh dulal <nitesh.dul...@gmail.com> wrote:
>
> > > > HI
> > > > i am trying to show legend for mypiechart
> > > > i succesfully drew mypiechart using core plot
> > > > but when i add legends thepiechart just doesnt show up
> > > > the delegate method -(NSNumber *)numberForPlot:(CPTPlot *)plot field:
> > > > (NSUInteger)fieldEnum recordIndex:(NSUInteger)index  doesnt get called
> > > > at all
> > > > when i remove the legends thepiediagram is shown
Reply all
Reply to author
Forward
0 new messages