Core Plot:在饼图中添加一个图例,其中“legend TitleForPieChart”不起作用

时间:2012-03-05 14:19:05

标签: ios core-plot pie-chart

我是IOS开发的新手,我在绘制带有Core-Plot的pieChart的图例时遇到了问题。饼图很好地显示但它没有显示它的图例。我已经阅读了很多关于这个问题的内容(StackOverFlowResponseTopic),但我找不到为什么它不适用于我。

我创建了一个PieChartView,我在tabBar应用程序的第二个ViewController中初始化。

这是我的饼图视图的代码:

//
//  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

这是viewController的代码

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



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

非常感谢并为我糟糕的英语而烦恼。

2 个答案:

答案 0 :(得分:0)

如果您无法让CorePlot按照您的意愿行事,您可以考虑以下几种方法:

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

我个人推荐ShinobiCharts,但我承认我有偏见! 希望这会有所帮助:)

答案 1 :(得分:0)

尝试在viewController的viewWillAppear方法中重新加载托管视图的数据:

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

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

   [self.pieChart.hostingView reloadData];    
}