1

I'm new in IOS developpement and I have a problem drawing the legend of a pieChart with Core-Plot.The pie chart is well displayed but it doesn't display its legend. I've already read a lot about this issue (StackOverFlowResponseTopic) but I can't find why it doesn't work with me.

I've created a PieChartView that I initialse in my second ViewController of a tabBar application.

This is the code of my pie chart view:

//
//  PieChartView.m
//  ExerciceRecap
//
//  Created by Alex on 02/03/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PieChartView.h"
#import "ModeleDeDonnes.h"
#import "CorePlot-CocoaTouch.h"    

@implementation PieChartView

@synthesize graph=_graph;
@synthesize hostingView = _hostingView;
@synthesize graphData = _graphData;
@synthesize myLabels = _myLabels;    

- (id) initWithHostingView:(CPTGraphHostingView *)hostingView andData:(NSMutableArray *)data{
    self = [super init];

    if (self != nil){
        self.hostingView = hostingView;
        self.graph = nil;

        // Manage data from dataController
        _myLabels = [[[NSMutableArray alloc]init]autorelease];
        NSMutableArray *myValues  = [[[NSMutableArray alloc]init]autorelease];

        for (NSUInteger i = 0; i < [data count]; i++) { 
            ModeleDeDonnes *theObject = [data objectAtIndex:i]; 
            [_myLabels addObject:theObject.nom];
            [myValues addObject:theObject.montant];
        }
        self.graphData = myValues;
    }
    return self;
}

-(void)initialisePieChart{

    if ([_graphData count] == 0 ){
        NSLog(@"No data");
        return;
    }

    if((self.hostingView == nil) || (self.graphData == nil)){
        NSLog(@" Cannot initialse hostingView");
        return;
    }

    if (self.graph != nil){
        NSLog(@" graph already exists");
        return;
    }

    CGRect frame = [self.hostingView bounds];
    self.graph = [[[CPTXYGraph alloc] initWithFrame:frame] autorelease];

    //Tie the graph we have created with the hosting view
    self.hostingView.hostedGraph = self.graph;

    CPTPieChart * pieChart = [[[CPTPieChart alloc]init]autorelease];
    pieChart.dataSource =  self;
    pieChart.pieRadius = 100.0;
    pieChart.identifier = @"pieChart1";
    pieChart.startAngle = M_PI_2;
    pieChart.sliceDirection = CPTPieDirectionCounterClockwise;
    _graph.title=@"My PieChart";    

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

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

    [self.graph addPlot:pieChart];

}

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

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
    return [self.graphData objectAtIndex:index];
}

-(NSString *)legendTitleForPieChart:(CPTPieChart *)pieChart
                        recordIndex:(NSUInteger)index{
    NSLog(@"LegendTitleForPieChart");
   return [NSString stringWithFormat:@"Ma légende", index];        
}

@end

And this is the code of the viewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];



   self.pieChart =[[PieChartView alloc] initWithHostingView:_myGraphView     andData:_dataController.masterDataList];
   [self.pieChart initialisePieChart];        
}

Thanks a lot and sry for my bad english.

Community
  • 1
  • 1
Alexandre
  • 410
  • 5
  • 21

2 Answers2

0

Try to reload data of the hosting view in the viewWillAppear method of your viewController:

- (void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];

   self.pieChart =[[PieChartView alloc] initWithHostingView:_myGraphView     andData:_dataController.masterDataList];
   [self.pieChart initialisePieChart];    

   [self.pieChart.hostingView reloadData];    
}
Werewolf
  • 76
  • 10
0

if you can't get CorePlot to do what you want it to, there are several alternatives you might consider:

What are the alternatives to Core-Plot for drawing graphs in iPhone SDK

I'd recommend ShinobiCharts personally, but admittedly I am biased! Hope this helps :)

Community
  • 1
  • 1
Simon Withington
  • 1,485
  • 2
  • 11
  • 17