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

半世苍凉 提交于 2019-12-20 02:33:24

问题


I am using the Core Plot framework to draw a pie chart, and am having no issues in drawing the pie chart itself.

However, I need the pie chart to be interactive in nature, i.e., if I tap on any particular section in the pie chart, it should trigger the navigation to a page showing details of that particular section.

I tried using the method -(void)pieChart:sliceWasSelectedAtRecordIndex:, but that delegate method was never called. What do I need to enable this touch interaction?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/4506544/how-do-you-enable-touch-selection-of-a-section-in-a-core-plot-pie-chart

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