我是前言: 大约几个月前我在某平台写了一篇文章, 文中简单地介绍了Charts两种图表的样式的使用, 不过有种意犹未尽的感觉, 利用周末的空闲时间再次看了看, 有了新的收获, 今天发出来,分享给大家, 来稍加详细的再次走进Charts的世界. 完全纯代码.
经过测试:
1.1使用cocoa pods集成(只能在9.3之后运行), 有知道原因的请在下方评论
1.2 这篇文章的例子我是使用手动加入的framework进行测试的. 如果你不清楚如何加入三方框架请点击看看这篇文章http://www.jianshu.com/p/fd91c10c9f55 如果模拟器能运行, 真机不能运行, 一定看这篇
这篇你可以看到下面这些
- LineChartView —– 折线
- BarChartView —— 柱状
- CandleStickChartView—–K线
- PieChartView —–饼状
- RadarChartView —– 雷达
还有这些你看不到0.-, 用法大同小异吧…


折线图 – LineChartView
1 2 3 4 5 6 7 8 9 |
// 创建- func createLineChartView() { chartView = LineChartView.init(frame: CGRectMake(0, 64, s_w, s_h - 64)) // 签协议 chartView.delegate = self chartView.backgroundColor = UIColor.whiteColor() self.view.addSubview(chartView) } |
折线
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// 加上模拟数据 func setChart(dataPoints: [String], values: [Double]) { var dataEntries: [ChartDataEntry] = [] for i in 0..<dataPoints.count { let dataEntry = ChartDataEntry(value: values[i], xIndex: i) dataEntries.append(dataEntry) } let chartDataSet = LineChartDataSet(yVals: dataEntries, label: "Units Sold") let chartData = LineChartData(xVals: months, dataSet: chartDataSet) // 加上一个界限, 演示图中红色的线 let jx = ChartLimitLine(limit: 12.0, label: "I am LimitLine") chartView.rightAxis.addLimitLine(jx) chartView.data = chartData // 自定义颜色 // colors 是一个数组, 可以给相应的颜色 chartDataSet.colors = [UIColor.redColor()] chartDataSet.colors = ChartColorTemplates.colorful() chartView.animate(yAxisDuration: 1.0, easingOption: .EaseInBounce) } |
柱状图- BarChartView
1 2 3 4 5 6 7 8 |
func createLineChartView() { chartView = BarChartView.init(frame: CGRectMake(0, 64, s_w, s_h - 64)) // 签协议 chartView.delegate = self chartView.backgroundColor = UIColor.whiteColor() self.view.addSubview(chartView) } |
柱状
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
func setChart(dataPoints: [String], values: [Double]) { >, values: [Double]) { ̡看了看, 有了新的收获, 今天发出来,分享给大家, 来稍加详细的再次走进Charts的世界. 完全纯代码.
经过测试: 这篇你可以看到下面这些
还有这些你看不到0.-, 用法大同小异吧… ![]() ![]() 折线图 –
|
1 2 3 4 5 6 7 8 9 |
// 创建- func createLineChartView() { chartView = LineChartView.init(frame: CGRectMake(0, 64, s_w, s_h - 64)) // 签协议 chartView.delegate = self chartView.backgroundColor = UIColor.whiteColor() self.view.addSubview(chartView) } |
折线
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// 加上模拟数据 func setChart(dataPoints: [String], values: [Double]) { var dataEntries: [ChartDataEntry] = [] for i in 0..<dataPoints.count { let dataEntry = ChartDataEntry(value: values[i], xIndex: i) dataEntries.append(dataEntry) } let chartDataSet = LineChartDataSet(yVals: dataEntries, label: "Units Sold") let chartData = LineChartData(xVals: months, dataSet: chartDataSet) // 加上一个界限, 演示图中红色的线 let jx = ChartLimitLine(limit: 12.0, label: "I am LimitLine") chartView.rightAxis.addLimitLine(jx) chartView.data = chartData // 自定义颜色 // colors 是一个数组, 可以给相应的颜色 chartDataSet.colors = [UIColor.redColor()] chartDataSet.colors = ChartColorTemplates.colorful() chartView.animate(yAxisDuration: 1.0, easingOption: .EaseInBounce) } |
柱状图- BarChartView
1 2 3 4 5 6 7 8 |
func createLineChartView() { chartView = BarChartView.init(frame: CGRectMake(0, 64, s_w, s_h - 64)) // 签协议 chartView.delegate = self chartView.backgroundColor = UIColor.whiteColor() self.view.addSubview(chartView) } |
柱状
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
func setChart(dataPoints: [String], values |