我可以CPTPiechart
在视图控制器中使用两个吗?
我在dataSource
和上遇到问题delegate
。
这是我的代码://第一个饼图
-(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];
}
// 第二个饼图
- (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;