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)" )
}