PNChart图表绘制库的使用

1,导入PNChart库并建立桥接文件

  

 

2,该第三方库用到了一个类,一并导入

  

3,可能会报错,做如下修改

  1⃣

  

  2⃣

  

4,用tableview创建一个简易的模版,用来显示对应的图表

  

5,折线图代码

  func LineChart(){
        print("LineChart")
        //声明一个折线图
        let LineChart = PNLineChart(frame: CGRectMake(-10,100,320,200))
        //设置X坐标,间隔
        LineChart.setXLabels(["1","2","3","4","5","6","7","8","9","10","11","12"], withWidth: 20)
        //设置Y坐标数据
        let dataArray = [5,1,2,3,3,4,8,7,6,4,3,2]
        //配置折线
        let LineData = PNLineChartData()
        //颜色
        LineData.color = UIColor.redColor()
        //折点数目
        LineData.itemCount = (UInt)(dataArray.count)
        //
        LineData.getData = ({(index:UInt)->PNLineChartDataItem in
            let y:CGFloat = (CGFloat)(dataArray[(Int)(index)])
            return PNLineChartDataItem(y:y)
        })
        
        /// 若有两组数据,只需添加Y数据和折线即可
        //设置Y坐标数据
        let dataArray1 = [8,7,6,5,5,4,3,1,2,4,5,6]
        //配置折线
        let LineData1 = PNLineChartData()
        //颜色
        LineData1.color = UIColor.blueColor()
        //折点数目
        LineData1.itemCount = (UInt)(dataArray1.count)
        //
        LineData1.getData = ({(index:UInt)->PNLineChartDataItem in
            let y:CGFloat = (CGFloat)(dataArray1[(Int)(index)])
            return PNLineChartDataItem(y:y)
        })
        
        
        //将数据添加到图表中,并进一步显示到view中
        LineChart.chartData = [LineData,LineData1]
        LineChart.strokeChart()
        self.view.addSubview(LineChart)
        
        //给图表添加legend
        LineData.dataTitle = "lorry"
        LineData1.dataTitle = "hehe"
        LineChart.legendStyle = PNLegendItemStyle.Serial
        
        let legend = LineChart.getLegendWithMaxWidth(320)
        legend.frame = CGRectMake(30,300,320,100)
        self.view.addSubview(legend)
        
        //更新数据
        legend.removeFromSuperview()
        
        let dataArray2 = [9,7,6,5,4,4,3,1,2,3,4,3]
        let LineData2 = PNLineChartData()
        LineData2.color = UIColor.yellowColor()
        LineData2.itemCount = (UInt)(dataArray2.count)
        LineData2.getData = ({(index:UInt)->PNLineChartDataItem in
            let y:CGFloat = (CGFloat)(dataArray2[(Int)(index)])
            return PNLineChartDataItem(y:y)
        })
        LineData2.dataTitle = "laji"
        
        LineChart.updateChartData([LineData2,LineData])
        LineChart.strokeChart()
        
        let legend2 = LineChart.getLegendWithMaxWidth(320)
        legend2.frame = CGRectMake(30,300,320,100)
        self.view.addSubview(legend2)
        
     }

6,柱状图代码

    func BarChart(){
        print("BarChart")
        let barChart = PNBarChart(frame: CGRectMake(5,100,320,200))
        barChart.xLabels = ["x1","x2","x3","x4","x5"]
        barChart.yValues = ["3","4","5","6","4"]
        barChart.strokeChart()
        self.view.addSubview(barChart)
    }

 7,环形图代码

    func CircleChart(){
        print("CircleChart")
        //let CircleChart = PNCircleChart(frame: CGRectMake(5,100,320,200),total:NSNumber(double: 100),current:NSNumber(double: 75),clockwise:false)
        let CircleChart = PNCircleChart(frame: CGRectMake(5,100,320,200), total: NSNumber(double: 100), current: NSNumber(double: 75), clockwise: false, shadow: false, shadowColor: UIColor.greenColor())
        CircleChart.strokeColor = UIColor.blueColor()
        CircleChart.strokeChart()
        self.view.addSubview(CircleChart)
    }

8,饼状图代码

    func PieChart(){
        print("PieChart")
        let items = [PNPieChartDataItem(value:10,color: UIColor.blueColor(),description: "lorry"),PNPieChartDataItem(value:30,color: UIColor.redColor(),description: "yangyang"),PNPieChartDataItem(value:20,color: UIColor.grayColor(),description: "chongchong")]
        let PieChart = PNPieChart(frame: CGRectMake(10,150,300,200), items: items)
        PieChart.descriptionTextColor = UIColor.greenColor()
        PieChart.descriptionTextFont = UIFont.systemFontOfSize(14)
        PieChart.strokeChart()
        self.view.addSubview(PieChart)

        //前面已经设置了title,直接添加legend即可
        let legend = PieChart.getLegendWithMaxWidth(320)
        legend.frame = CGRectMake(30,500,320,100)
        self.view.addSubview(legend)
    }

9,回调函数,返回点击处的数据

  继承协议

class ViewController: UIViewController,PNChartDelegate

  实现协议方法

    func userClickedOnLinePoint(point: CGPoint, lineIndex: Int) {
        print("\(lineIndex)"+":"+"\(point)")
        print("为嘛没反应")
    }
    func userClickedOnLineKeyPoint(point: CGPoint, lineIndex: Int, pointIndex: Int) {
        
    }  

posted on 2015-12-22 21:34  罗小夕  阅读(546)  评论(0编辑  收藏  举报

导航