IOSデベロップメントで新しく、core-Plotを持つpieChartの伝説を描くのに問題があります。円グラフはうまく表示されますが、その凡例は表示されません。私はすでにこの問題(StackOverFlowResponseTopic)について多くのことを読んだことがありますが、なぜそれが私とうまくいかないのか分かりません。Core Plot:legendTitleForPieChartで凡例を追加しても動作しない
私は、TabBarアプリケーションの2番目のViewControllerで初期化するPieChartViewを作成しました。
この
は私の円グラフビューのコードです://
// 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;
[email protected]"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];
}
おかげで私の悪い英語のためにたくさんおよびSRYのコードです。
感謝を – Alexandre