【问题标题】:How do you enable touch selection of a section in a Core Plot pie chart?如何在 Core Plot 饼图中启用部分的触摸选择?
【发布时间】:2010-12-22 05:50:40
【问题描述】:

我正在使用 Core Plot 框架来绘制饼图,并且在绘制饼图本身时没有任何问题。

但是,我需要饼图本质上是交互式的,即,如果我点击饼图中的任何特定部分,它应该会触发导航到显示该特定部分详细信息的页面。

我尝试使用方法-(void)pieChart:sliceWasSelectedAtRecordIndex:,但从未调用过该委托方法。我需要什么才能启用这种触摸交互?

【问题讨论】:

    标签: iphone ios core-plot


    【解决方案1】:

    我已经在我的 iPad 应用中使用 CorePlot 0.2.2 实现了饼图选择。你的猜测使用 (void)pieChart:sliceWasSelectedAtRecordIndex: 是正确的,但也许你忘记了 声明以下两件事:

    • 您的控制器是否声明了 CPPieChartDelegate 协议?
    • 您是否告诉饼图您的控制器是它的代表

    我的视图控制器在标头声明中如下所示:

    @interface YourViewController : UIViewController < CPPieChartDataSource,
                                                       CPPieChartDelegate,
                                                       ... >
    {
       ...
       CPXYGraph*          pieGraph;
       CPGraphHostingView* pieView;
    }
    
    @property (nonatomic, retain) IBOutlet CPGraphHostingView* pieView;
    
    - (void)pieChart:(CPPieChart *)plot sliceWasSelectedAtRecordIndex:(NSUInteger)index;
    
    @end
    

    饼图的创建是在(void)viewDidLoad期间调用的,这里我设置了饼图的数据源和委托:

    -(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】:

      使用最后一个corePlot framework (1.4) 我找不到CPPieChart 但我用CPTPieChartDelegate 修复了:

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

      还有这个方法:

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

      CPPieChartDelegate 不再被识别为 Xcode 中的委托,使用 CorePlot 1.4

      希望对你有帮助。

      dom

      【讨论】:

      • Core Plot 类前缀从“CP”更改为“CPT”。
      • 谢谢:看起来如果你用谷歌搜索“corePlot 示例”,你总是会遇到旧示例或旧讨论线程。希望这篇笔记能帮助“iOS学习者”(像我一样;-))
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多