2013-08-14 2 views
0

Могу ли я использовать два контроллера CPTPiechart?
У меня проблема с dataSource и delegate.Два контроллера Piechart in view

Вот мой код: // первый PieChart

-(IBAction)TG:(id)sender{ 


pieanother *InterPie = [[pieanother alloc]init]; 

pieChart = [[CPTPieChart alloc] init]; 
self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.view.bounds]; 
self.hostView.allowPinchScaling = NO; 
self.hostView.frame = CGRectMake(0, 220,320, 240); 


CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds]; 
self.hostView.hostedGraph = graph; 
graph.paddingLeft = 0.0f; 
graph.paddingTop = 0.0f; 
graph.paddingRight = 0.0f; 
graph.paddingBottom = 0.0f; 
graph.axisSet = nil; 
// 2 - Set up text style 
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle]; 
textStyle.color = [CPTColor grayColor]; 
textStyle.fontName = @"Helvetica-Bold"; 
textStyle.fontSize = 16.0f; 
// 3 - Configure title 
NSString *title = @"Portfolio Prices: May 1, 2012"; 
graph.title = title; 
graph.titleTextStyle = textStyle; 
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop; 
graph.titleDisplacement = CGPointMake(0.0f, -12.0f); 
// 4 - Set theme 
self.selectedTheme = [CPTTheme themeNamed:kCPTPlainWhiteTheme]; 
[graph applyTheme:self.selectedTheme]; 


pieChart.dataSource = InterPie;// 
pieChart.delegate = InterPie; 
pieChart.pieRadius = (self.hostView1.bounds.size.height * 0.7)/2; 
pieChart.identifier = graph.title; 
pieChart.startAngle = M_PI_4; 
pieChart.sliceDirection = CPTPieDirectionClockwise; 
// 3 - Create gradient 
CPTGradient *overlayGradient = [[CPTGradient alloc] init]; 
overlayGradient.gradientType = CPTGradientTypeRadial; 
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.0] atPosition:0.9]; 
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.4] atPosition:1.0]; 
pieChart.overlayFill = [CPTFill fillWithGradient:overlayGradient]; 
// 4 - Add chart to graph 
[graph addPlot:pieChart]; 


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


[self.view addSubview: self.hostView]; 

} 

// Второй PieChart

- (IBAction)OA:(id)sender { 
self.anotherView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.view.bounds]; 
self.anotherView.allowPinchScaling = NO; 
self.anotherView.frame = CGRectMake(0, 220,320, 240); 


CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.anotherView.bounds]; 
self.anotherView.hostedGraph = graph; 
graph.paddingLeft = 0.0f; 
graph.paddingTop = 0.0f; 
graph.paddingRight = 0.0f; 
graph.paddingBottom = 0.0f; 
graph.axisSet = nil; 
// 2 - Set up text style 
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle]; 
textStyle.color = [CPTColor grayColor]; 
textStyle.fontName = @"Helvetica-Bold"; 
textStyle.fontSize = 16.0f; 
// 3 - Configure title 
NSString *title = @"Portfolio Prices: May 1, 2012"; 
graph.title = title; 
graph.titleTextStyle = textStyle; 
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop; 
graph.titleDisplacement = CGPointMake(0.0f, -12.0f); 
// 4 - Set theme 
self.selectedTheme = [CPTTheme themeNamed:kCPTPlainWhiteTheme]; 
[graph applyTheme:self.selectedTheme]; 

pieanother *DomPie = [[pieanother alloc]init]; 

pieChart = [[CPTPieChart alloc] init]; 
pieChart.dataSource = DomPie;// 
pieChart.delegate = DomPie; 
pieChart.pieRadius = (self.hostView1.bounds.size.height * 0.7)/2; 
pieChart.identifier = graph.title; 
pieChart.startAngle = M_PI_4; 
pieChart.sliceDirection = CPTPieDirectionClockwise; 
// 3 - Create gradient 
CPTGradient *overlayGradient = [[CPTGradient alloc] init]; 
overlayGradient.gradientType = CPTGradientTypeRadial; 
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.0] atPosition:0.9]; 
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.4] atPosition:1.0]; 
pieChart.overlayFill = [CPTFill fillWithGradient:overlayGradient]; 
// 4 - Add chart to graph 
[graph addPlot:pieChart]; 


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


[self.view addSubview: self.anotherView]; 


} 

Когда-нибудь я могу запустить приложение без ошибок, но другие времена приложение просто врезаться. Пожалуйста посоветуй.

pieChart.dataSource = DomPie; //if i comment pieChart.dataSource I alway run App without error but no data 
pieChart.delegate = DomPie; 

ответ

1

Используйте identifier свойство CPTPlot объекта.

CPTPieChart piePlot1 = [[CPTPieChart alloc] init]; 
piePlot1.identifier  = @"Pie Chart 1"; 

CPTPieChart piePlot2 = [[CPTPieChart alloc] init]; 
piePlot2.identifier  = @"Pie Chart 2"; 

В datasource методы:

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot{ 
if ([plot.identifier isEqual:@"Pie Chart 1"]) { 
// return for 1st Pie Chart 
} 
else if ([plot.identifier isEqual:@"Pie Chart 2"]) { 
// return for 2nd Pie Chart 
} 
} 
+0

, спасибо, но это не работает, я могу запустить мое приложение, но имеет ту же проблему. –

+0

Ой, жаль, мое плохое, его работа. Спасибо, Пунеет. –

+0

Рад помочь !!! –

Смежные вопросы