开发者

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

I am using the Core Plot framework to draw a pie chart, and am havi开发者_运维问答ng 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?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜