我在Swift中使用CorePlot(https://github.com/core-plot/core-plot)。 爲了在iOS上製作餅圖,我通過將Obj-C代碼翻譯爲swift來編寫如下。 我能夠查看圖表標題,但無法繪製餅圖。Swift中的CorePlot無法調用「numberForPlot」
作爲查看NSlog的結果,我發現「numberForPlot」函數顯然沒有被調用。
我該如何重寫?
我的代碼如下所示: ViewController.swift
import UIKit
class ViewController: UIViewController, CPTPieChartDataSource, CPTPieChartDelegate{
@IBOutlet var graphView : CPTGraphHostingView
var dataForChart = NSNumber[]();
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var graph = CPTXYGraph(frame:self.graphView.bounds);
self.graphView.hostedGraph = graph;
graph.title = "Graph Title";
graph.axisSet = nil;
var pieChart = CPTPieChart();
pieChart.pieRadius = 80.0;
pieChart.dataSource = self;
pieChart.delegate = self;
graph.addPlot(pieChart);
self.dataForChart = [40, 30, 20, 10];
self.view.addSubview(self.graphView);
println("self.view.addSubview");
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfRecordsForPlot(plot:CPTPlot)-> Int {
println("numberOfRecordsForPlot");
return self.dataForChart.count;
}
func numberForPlot(plot:CPTPlot, fieldEnum:Int, index:Int) -> NSNumber {
println("numberForPlot");
return self.dataForChart[index] as NSNumber;
}
}
這裏的「所有輸出」。
self.view.addSubview
numberOfRecordsForPlot
numberOfRecordsForPlot
numberOfRecordsForPlot
numberOfRecordsForPlot
希望得到任何幫助,謝謝!
PS
以前的OBJ-C代碼
- (void)viewDidLoad
{
[super viewDidLoad];
CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc]
initWithFrame:CGRectMake(0, 0, 320, 320)];
CPTXYGraph *graph = [[CPTXYGraph alloc] initWithFrame:hostingView.bounds];
hostingView.hostedGraph = graph;
graph.title = @"Graph Title";
graph.axisSet = nil;
CPTPieChart *pieChart = [[CPTPieChart alloc] init];
pieChart.pieRadius = 80.0;
pieChart.dataSource = self;
pieChart.delegate = self;
[graph addPlot:pieChart];
self.pieChartData = [NSMutableArray arrayWithObjects:
[NSNumber numberWithDouble:40.0],
[NSNumber numberWithDouble:30.0],
[NSNumber numberWithDouble:20.0],
[NSNumber numberWithDouble:10.0],
nil];
[self.view addSubview:hostingView];
}
而且,這裏的 「numberOfRecordsForPlot」 返回。
func numberOfRecordsForPlot(plot:CPTPlot)-> Int {
println("self.dataForChart.count: \(self.dataForChart.count)");
return self.dataForChart.count;
}
輸出
self.view.addSubview
self.dataForChart.count: 4
self.dataForChart.count: 4
self.dataForChart.count: 4
self.dataForChart.count: 4
如果您以前的Obj-C代碼工作,您可以發佈'-viewDidLoad'方法嗎? – Undo
什麼是numberOfRecordsForPlot返回?如果它是0,那麼numberForPlot可能不是故意調用的。 –