如何在Swift中使用ObjC PNChart库使用getData()绘制LineChart? [英] How do I use getData() to plot a LineChart with ObjC PNChart library in Swift?

查看:82
本文介绍了如何在Swift中使用ObjC PNChart库使用getData()绘制LineChart?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新手程序员,最近开始学习Swift.

I am a newbie programmer and recently started learning Swift.

我正在Swift中制作一个项目,我的意图是在引入参数的同一视图中绘制用户引入的信号函数(使用由PNLineChart类控制的子视图).

I am making a project in Swift and my intention is to plot a user-introduced signal function in the same View where the parameters are introduced (using a subView controlled by the class PNLineChart).

事实是,我使用CocoaPods安装了PNChart库,并使用了Objective C标头的桥接标头.关键是,在ObjC中使用分类和方法的方法是将它们翻译"为Swift语法.我发现PNChart具有作者编写的Swift实现,但Pods无法使用它(如果有人知道如何将它们放入我的项目中,这也会有所帮助).

The fact is that I installed PNChart library using CocoaPods, using a bridging header for the Objective C headers. The thing is that the way to use the clases and methods in ObjC is "translating" them to Swift syntax. I found that PNChart has a Swift implementation made by the author, but is not available by Pods (if someone knows how to put them into my project it will be helpful too).

好吧,用于绘制作者在其Wiki中给出的数组图的ObjC代码如下:

Well, the ObjC code to make the plot of an array given by the author in its wiki is the following:

#import "PNChart.h"

//For Line Chart
PNLineChart * lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];
[lineChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5"]];

// Line Chart No.1
NSArray * data01Array = @[@60.1, @160.1, @126.4, @262.2, @186.2];
PNLineChartData *data01 = [PNLineChartData new];
data01.color = PNFreshGreen;
data01.itemCount = lineChart.xLabels.count;
data01.getData = ^(NSUInteger index) {
    CGFloat yValue = [data01Array[index] floatValue];
    return [PNLineChartDataItem dataItemWithY:yValue];
};
// Line Chart No.2
NSArray * data02Array = @[@20.1, @180.1, @26.4, @202.2, @126.2];
PNLineChartData *data02 = [PNLineChartData new];
data02.color = PNTwitterColor;
data02.itemCount = lineChart.xLabels.count;
data02.getData = ^(NSUInteger index) {
    CGFloat yValue = [data02Array[index] floatValue];
    return [PNLineChartDataItem dataItemWithY:yValue];
};

lineChart.chartData = @[data01, data02];
[lineChart strokeChart];

所以我试图将其转换为Swift,其中:

So I tried to convert it to Swift, where:

  • inputSignal:UITextField! ->将是用户引入数学函数的textField(仍在开发中)
  • 其他IBOutlet->用于设置功能限制和全局图表限制的参数
  • inputSignalLineChart:PNLineChart! ->将subView连接到ViewController
  • initSignalExample->用于测试绘图的初始数组
  • signalObtainedValues-> PNLineChartData类型的声明

在viewDidLoad()函数内部,我正在初始化一个初始图表以测试它是否可以在View中工作.当我尝试使用PNLineChartData类中定义的getData()方法时,问题就来了,而我实际上无法将其转换为正确的Swift代码.

Inside the viewDidLoad() function I am initiating an initial chart to test if it works in the View. The problem comes when I try to use the method getData() that is defined in the PNLineChartData class and I am not really able to translate it to a correct Swift code.

我的理解是,getData作为输入的UInteger称为索引(我认为它将选择单个数据,但我看不到它在任何地方声明:-/),并在输出时返回PNLineChartDataItem().正如我在ObjC代码中看到的那样,执行此操作的方式有点像PNLineChartDataItem(y:CGFloat),因此我想我可以按照下面的代码进行操作:

My understanding is that getData has as input an UInteger called index (I think it will select the individual data, but I do not see it declared anywhere :-/) and as output it returns a PNLineChartDataItem(). As I see in the ObjC code, the way to do this is somewhat like PNLineChartDataItem(y: CGFloat) so I suppose that I can do the code as I show you below:

import UIKit


class ViewController: UIViewController, PNChartDelegate{


    @IBOutlet weak var inputSignal: UITextField!

    @IBOutlet weak var initialTimeInputSignal: UITextField!

    @IBOutlet weak var finalTimeInputSignal: UITextField!

    @IBOutlet weak var initialGlobalTime: UITextField!
    @IBOutlet weak var finalGlobalTime: UITextField!

    @IBOutlet weak var inputSignalLineChart: PNLineChart!

    let screenWidth = UIScreen.mainScreen().bounds.size.width
    let screenHeight = UIScreen.mainScreen().bounds.size.height
    let initSignalExample = [0.0, 1.0, 1.0, 1.0, 0.0]
    let signalObtainedValues: PNLineChartData!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        view.backgroundColor = UIColor.cyanColor()

        inputSignalLineChart.delegate = self
        inputSignalLineChart.frame = CGRectMake(0, 135.0, screenWidth, screenHeight)
        inputSignalLineChart.backgroundColor = UIColor.whiteColor()
        inputSignalLineChart.setXLabels(["0","1","2","3","4"], withWidth: 1)
        //signalObtainedValues.color = UIColor.greenColor()
        //signalObtainedValues.itemCount = UInt(inputSignalLineChart.xLabels.count)

        signalObtainedValues.getData(UInt(index)) = { PNLineChartDataItem(y: CGFloat(initSignalExample[index]))

        }

        //inputSignalLineChart.chartData = [signalObtainedValues]
        inputSignalLineChart.strokeChart()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

尝试并尝试仅收到错误:无法在getData()行中将其分配给该表达式的结果.所以我认为这是我在Swift中编写的代码的语法.有谁知道如何执行此操作并正确在子视图中绘制图表?任何答案或有用的提示,将不胜感激.谢谢!

Trying and trying I only get the error: Cannot assign to the result of this expression, in the getData() line. So I think this is for the syntax of the code I have made in Swift. Has anyone any idea of how to do this and correctly draw the chart in the subview? Any answer or helpful tip will be appreciated. Thank you!

推荐答案

我终于找到了解决该问题的方法.适用于我的代码如下:

I finally got a solution for the problem. The code that works for me is the following:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    view.backgroundColor = UIColor.cyanColor()

    playButton.layer.backgroundColor = UIColor.whiteColor().CGColor
    playButton.layer.cornerRadius = 5

    let initSignalExample = [0.0, 1.0, 2.0, 1.0, 0.0]


    lineChartSubView.frame = CGRectMake(0, 0, screenWidth, screenHeight)
    lineChartSubView.backgroundColor = UIColor.whiteColor()
    lineChartSubView.setXLabels(["0","1","2","3","4"], withWidth: 1)
    lineChartSubView.showCoordinateAxis = true
    lineChartSubView.showLabel = true
    lineChartSubView.delegate = self

    var signalObtainedValues = PNLineChartData()

    signalObtainedValues.color = UIColor.greenColor()
    signalObtainedValues.itemCount = UInt(initSignalExample.count)
    signalObtainedValues.getData = ({(index: UInt) -> PNLineChartDataItem in
        var yValue:CGFloat = CGFloat(initSignalExample[Int(index)])
        var item = PNLineChartDataItem(y: yValue)
        return item
    })

    lineChartSubView.chartData = [signalObtainedValues]
    lineChartSubView.strokeChart()

}

现在的问题是该图不适合子视图,但是由于原始帖子已解决,因此我可能会打开另一个问题. 非常感谢你!

Now the problem is that the graph does not fit in the subview, but maybe I will open another question since the original post is solved. Thank you very much!

这篇关于如何在Swift中使用ObjC PNChart库使用getData()绘制LineChart?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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