第三方庫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)" )
        
    }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM