玩转PNChart-Swift

PNChart-Swift是一款用Swift语言实现的iOS开源动画图标类库,可以生成简单、漂亮且有动画效果的柱状图、曲线图、折线图、饼图。

pod 'PNChartSwift',:git => 'https://github.com/kevinzhow/PNChart-Swift.git'

import UIKit
import PNChartSwift

class TranscriptDetailViewController: UIViewController {
    
    var name:String = ""
    var ChartLabel:UILabel = UILabel(frame: CGRectMake(0, 90, 320.0, 30))

    @IBOutlet weak var webView: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = name
        
        lineChart()
 
    }
    
    func barChart() {
        ChartLabel.textColor = PNGreenColor
        ChartLabel.font = UIFont(name: "Avenir-Medium", size:23.0)
        ChartLabel.textAlignment = NSTextAlignment.Center
        ChartLabel.text = "Bar Chart"
        
        let barChart = PNBarChart(frame: CGRectMake(0, 135.0, 320.0, 200.0))
        barChart.backgroundColor = UIColor.clearColor()
        barChart.animationType = .Waterfall
        barChart.labelMarginTop = 5.0
        
        barChart.xLabels = ["SEP 1","SEP 2","SEP 3","SEP 4","SEP 5","SEP 6","SEP 7"]
        barChart.yValues = [1,24,12,18,30,10,21]
        
        barChart.strokeChart()
        barChart.delegate = self
        
        self.view.addSubview(ChartLabel)
        self.view.addSubview(barChart)
        
        self.title = "Bar Chart"
    }
    
    func lineChart() {
        // Add LineChart
        ChartLabel.text = "Line Chart"
        
        let lineChart:PNLineChart = PNLineChart(frame: CGRectMake(0, 135.0, 320, 200.0))
        lineChart.yLabelFormat = "%1.1f"
        lineChart.showLabel = true
        lineChart.backgroundColor = UIColor.clearColor()
        lineChart.xLabels = ["SEP 1","SEP 2","SEP 3","SEP 4","SEP 5","SEP 6","SEP 7"]
        lineChart.showCoordinateAxis = true
        lineChart.delegate = self
        
        // Line Chart Nr.1
        var data01Array: [CGFloat] = [60.1, 160.1, 126.4, 262.2, 186.2, 127.2, 176.2]
        let data01:PNLineChartData = PNLineChartData()
        data01.color = PNGreenColor
        data01.itemCount = data01Array.count
        data01.inflexionPointStyle = PNLineChartData.PNLineChartPointStyle.PNLineChartPointStyleCycle
        data01.getData = ({(index: Int) -> PNLineChartDataItem in
            let yValue:CGFloat = data01Array[index]
            let item = PNLineChartDataItem(y: yValue)
            return item
        })
        
        lineChart.chartData = [data01]
        lineChart.strokeChart()
        
        self.view.addSubview(lineChart)
        self.view.addSubview(ChartLabel)
        self.title = "Line Chart"
    }

}

extension TranscriptDetailViewController : PNChartDelegate {
    
    func userClickedOnLineKeyPoint(point: CGPoint, lineIndex: Int, keyPointIndex: Int)
    {
        print("Click Key on line \(point.x), \(point.y) line index is \(lineIndex) and point index is \(keyPointIndex)")
    }
    
    func userClickedOnLinePoint(point: CGPoint, lineIndex: Int)
    {
        print("Click Key on line \(point.x), \(point.y) line index is \(lineIndex)")
    }
    
    func userClickedOnBarChartIndex(barIndex: Int)
    {
        print("Click  on bar \(barIndex)")
    }
    
}

你可能感兴趣的:(玩转PNChart-Swift)