Swift中的CorePlot无法调用“ numberForPlot” [英] CorePlot in Swift can not call “numberForPlot”

查看:56
本文介绍了Swift中的CorePlot无法调用“ numberForPlot”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CorePlot( https://github.com/core-plot/core-plot )。
为了在iOS上制作饼图,我通过将Obj-C代码转换为swift如下编写。
我可以查看图形标题,但无法绘制饼图。

I am using CorePlot(https://github.com/core-plot/core-plot) in Swift. In order to make a pie chart on iOS, I wrote as below by translating Obj-C code to swift. I was able to view the graph title, but I could not plot a pie chart.

由于查看了NSlog,我发现 numberForPlot

As a result of looking at NSlog, I found that "numberForPlot" function was apparently not called.

我应该如何重写它?

我的代码如下:
ViewController.swift

My code looks like this: 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;
    }

}

此处为所有输出。 / p>

Here's "All Output".

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的返回。

And, Here's "numberOfRecordsForPlot" returning.

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


推荐答案

尝试声明数据源方法如下:

Try declaring the datasource methods like this:

func numberOfRecordsForPlot(plot: CPTPlot!) -> UInt {}
func numberForPlot(plot: CPTPlot!, field: UInt, recordIndex: UInt ) -> AnyObject! {}

签名必须与Objective-C声明完全匹配,否则将不会被调用。

The signatures have to match the Objective-C declarations exactly or they won't be called.

这篇关于Swift中的CorePlot无法调用“ numberForPlot”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆