第三方库PNChart的使用

PNChart 是一个强大的带动画的图表库

 

要是用这个库可以使用pods,也可以直接将库导入项目中,必须引入"PNChart.h"头文件

 

下面我们来看一下代码!

 

/**
    折线图
    */
    func LineChart(){
        
        let lineChart = PNLineChart(frame: CGRectMake(0, 150, self.view.bounds.width, 300))
        
        lineChart.setXLabels(["魅族","华为","中兴","小米","苹果","一加","乐视 "], withWidth: 40)
       
        lineChart.delegate = self
        
        lineChart.showCoordinateAxis = true
        //设置数据1
        let dataArray1 = [5,3,1,2,7,20,13]
        let data1 = PNLineChartData()
        data1.color = UIColor.blueColor()
        data1.itemCount = (UInt)(dataArray1.count)
        data1.getData = ({(index:UInt)->PNLineChartDataItem in
            let y:CGFloat = (CGFloat)(dataArray1[(Int)(index)])
            return PNLineChartDataItem(y: y)
        })
        //设置数据2
        let dataArray2 = [1,4,5,8,2,16,12]
        let data2 = PNLineChartData()
        data2.color = UIColor.redColor()
        data2.itemCount = (UInt)(dataArray2.count)
        data2.getData = ({(index:UInt)->PNLineChartDataItem in
            let y:CGFloat = (CGFloat)(dataArray2[(Int)(index)])
            return PNLineChartDataItem(y: y)
        })
    
        //将数据添加到图中
        lineChart.chartData = [data1,data2]
        lineChart.strokeChart()
        self.view.addSubview(lineChart)
        
        data1.dataTitle = "数据一"
        data2.dataTitle = "数据二"
        lineChart.legendFont = UIFont.systemFontOfSize(13)
        lineChart.legendStyle = PNLegendItemStyle.Serial
        let legend = lineChart.getLegendWithMaxWidth(320)
        legend.frame = CGRectMake(150, 500, 320, 300)
        self.view.addSubview(legend)

        
    }
    /**
    柱状图
    */
    func BarChart() {
        let barLine = PNBarChart(frame: CGRectMake(0, 250, self.view.bounds.width, 300))
        barLine.xLabels = ["魅族","华为","中兴","小米","苹果","一加","乐视"]
        barLine.yValues = [27,76,15,45,66,35,10]
        barLine.strokeChart()
        self.view.addSubview(barLine)
 
        
    }
    /**
    圆形图
    */
    func CircleChart(){
        let CircleChart = PNCircleChart(frame:  CGRectMake(0, 150,self.view.bounds.width, 300), total: NSNumber(double: 100), current: NSNumber(double: 70), clockwise: true, shadow: true, shadowColor: UIColor.redColor())
        CircleChart.lineWidth = 40
        CircleChart.strokeColor = UIColor.blueColor()
        CircleChart.strokeChart()
        self.view.addSubview(CircleChart)
        
    }
    
    /**
    饼状图
    */
    func PieChart(){
        
        let items = [PNPieChartDataItem(value: 35, color: UIColor.blueColor(), description: "I'm Fay!!!"),PNPieChartDataItem(value: 40, color: UIColor.redColor(), description: "我是神仙!!!"),PNPieChartDataItem(value: 25, color: UIColor.orangeColor(), description: "这里是CCTV1")]
        
        let pieChart = PNPieChart(frame: CGRectMake(0, 150, self.view.bounds.width, 300), items: items)
        pieChart.descriptionTextFont = UIFont.boldSystemFontOfSize(13)
        pieChart.strokeChart()
        self.view.addSubview(pieChart)
        
        
        pieChart.legendStyle = PNLegendItemStyle.Stacked
        let legend = pieChart.getLegendWithMaxWidth(200)
        legend.frame = CGRectMake(170,550, legend.frame.size.width, legend.frame.size.height)
        self.view.addSubview(legend)
        
        
    }
    
    

    /**
    PNChartDelegate 带来方法
    
    - parameter point:      坐标
    - parameter lineIndex:  线
    - parameter pointIndex: 点
    */

    func userClickedOnLineKeyPoint(point: CGPoint, lineIndex: Int, pointIndex: Int) {
        
        print("第\(lineIndex)条线  " + "第\(pointIndex)个点  " +  "坐标为:\(point)" )
        
    }

  

转载于:https://www.cnblogs.com/aeronfay/articles/4929223.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值