ios - View Controller 中的两个饼图

标签 ios core-plot

我可以在 View Controller 中使用两个 CPTPiechart 吗?
我在 dataSourcedelegate 上遇到问题。

这是我的代码://第一个饼图

-(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;

最佳答案

使用CPTPlot对象的identifier属性。

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

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

数据源方法中:

-(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
}
}

关于ios - View Controller 中的两个饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18227315/

相关文章:

ios - 使用addObserverForName时保留循环:object:queue:usingBlock:

ios - 沿路径的 SceneKit 动画节点

ios - Xcode 6 GM - 无法使用 Playground

ios - Core Plot - 绘制较少点时的 X 轴移动位置

iphone - 执行 CorePlot 代码时出现警告

ios - 在工作区中向现有项目添加核心图框架时出现问题

ios - 核心绘图轴标签在绘图区域外不可见

ios - protectedDataAvailable 始终为 true

ios - Core Plot 轴标签不使用我的数字格式化程序

ios - Xcode 7.0 测试版 :- ibtool failed with exit code 255