How do you enable touch selection of a section in a Core Plot pie chart?

一曲冷凌霜 提交于 2019-12-01 21:44:16

I have implemented pie piece selection in my iPad app with CorePlot 0.2.2. Your guess to use (void)pieChart:sliceWasSelectedAtRecordIndex: is correct, but maybe you have forgotten to declare the following two things:

  • Does your controller declares the CPPieChartDelegate protocol?
  • Did you tell the pie chart that your controller is its delegate?

My view controller looks like this in the header declaration:

@interface YourViewController : UIViewController < CPPieChartDataSource,
                                                   CPPieChartDelegate,
                                                   ... >
{
   ...
   CPXYGraph*          pieGraph;
   CPGraphHostingView* pieView;
}

@property (nonatomic, retain) IBOutlet CPGraphHostingView* pieView;

- (void)pieChart:(CPPieChart *)plot sliceWasSelectedAtRecordIndex:(NSUInteger)index;

@end

The creation of the pie chart is called during the (void)viewDidLoad, where I set the data source and the delegate of the pie chart:

-(void)viewDidLoad {
   [self createPie];
}

-(void)createPie {
   pieGraph = [[CPXYGraph alloc] initWithFrame:CGRectZero];
   pieGraph.axisSet = nil;
   self.pieView.hostedGraph = pieGraph;

   CPPieChart *pieChart = [[CPPieChart alloc] init];

   // This is important in order to have your slice selection handler called!
   pieChart.delegate = self;

   pieChart.dataSource = self;

   pieChart.pieRadius = 80.0;
   [pieGraph addPlot:pieChart];
   [pieChart release];
}

- (void)pieChart:(CPPieChart *)plot sliceWasSelectedAtRecordIndex:(NSUInteger)index {
   // Do whatever you need when the pie slice has been selected.
}
dev-dom

Using the last corePlot framework (1.4) i could not find CPPieChart but I fixed with CPTPieChartDelegate:

@interface CPDFirstViewController : UIViewController <CPTPlotDataSource, CPTPieChartDelegate, ...>

and this method:

-(void)pieChart:(CPTPieChart *)pieChart sliceWasSelectedAtRecordIndex:(NSUInteger)index
{
  // Put your action here
}

CPPieChartDelegate is not recognized any more as Delegate from Xcode, using CorePlot 1.4

Hope it helps.

dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!