Swift 很强大的图表库-Charts使用
来源:互联网 发布:贵金属软件 编辑:程序博客网 时间:2024/05/22 06:22
前言: 今天分享一个很漂亮的功能强大的图表库,希望对有需要的同学, 有帮助, 喜欢请点个赞,支持一下.谢谢~
在项目中如何加入Swift库请看我的上一篇文章
http://www.jianshu.com/p/fd91c10c9f55
编译环境: Xcode7.3
添加Charts图表库
// 在Podfile中use_frameworks!pod 'Charts'
import Charts
创建柱状图
func createLineChartView() { chartView = BarChartView.init(frame: CGRectMake(0, 64, screenWidth, screenHeight - 64)) chartView.barData // 签协议 chartView.delegate = self chartView.backgroundColor = UIColor.whiteColor() self.view.addSubview(chartView) }
模拟数据赋值
func setChart(dataPoints: [String], values: [Double]) { var dataEntries: [BarChartDataEntry] = [] for i in 0..<dataPoints.count { let dataEntry = BarChartDataEntry(value: values[i], xIndex: i) dataEntries.append(dataEntry) } let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Units Sold") let chartData = BarChartData(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.blueColor(), UIColor.redColor(), UIColor.cyanColor()] // API 自带颜色模板 // ChartColorTemplates.liberty() // ChartColorTemplates.joyful() // ChartColorTemplates.pastel() // ChartColorTemplates.colorful() // ChartColorTemplates.vordiplom() chartDataSet.colors = ChartColorTemplates.liberty() /** // 动画效果, 简单列举几个, 具体请看API case EaseInBack case EaseOutBack case EaseInOutBack case EaseInBounce case EaseOutBounce case EaseInOutBounce */ // 加上动画 chartView.animate(yAxisDuration: 1.0, easingOption: .EaseInBounce) }
还可以保存当前的图表状态, 写一个保存按钮
func createRightBarButtonItem() { let buttonRight = UIButton.init(type: UIButtonType.Custom) buttonRight.frame = CGRectMake(0, 0, 40, 40) self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: buttonRight) buttonRight.setTitle("Save", forState: UIControlState.Normal) buttonRight.addTarget(self, action:#selector(ViewController.save(_:)), forControlEvents: UIControlEvents.TouchUpInside) }
点击方法中加入以下代码
func save(btn: UIButton) { // 保存到相册 chartView.saveToCameraRoll() print("保存成功") }
你可能还想实现点击单个立柱实现不同的点击事件
你可以 实现ChartViewDelegate
class ViewController: UIViewController, ChartViewDelegate {// 在创建的时候签协议// chartView.delegate = self// 实现协议方法func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) { print("\(entry.value) in \(months[entry.xIndex])") }
这样创建折线图
chartView = LineChartView.init(frame: CGRectMake(0, 64, screenWidth, screenHeight - 64))
模拟数据赋值改成如下代码
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)
总结: 图标支持缩放点击,这里我只给出了两种样式的图标代码示例, 详情请看官方API和Demo请点击
考虑到文章可能思路混乱有些地方不清楚我会放上github地址,方便大家学习下载查看请点击
4 0
- Swift 很强大的图表库-Charts使用
- iOS图表Charts的使用
- iOS图表库Charts集成与使用
- swift 画图 Charts的基本使用
- Swift-Charts 多种样式图表-详细
- Swift-Charts 多种样式图表演示
- 免费使用的图表控件XML/SWF Charts 5.08
- 如何在 Swift 语言下使用 iOS Charts API 制作漂亮图表?
- XCL-Charts强大的仪表盘
- Python Charts库的使用
- IOS OC中使用Swift的Charts第三方控件
- XCL-Charts强大的仪表盘,xcl-charts仪表盘
- OC项目集成Charts (swift)图表第三方插件
- 用于 Windows8 的 Wijmo Charts 图表控件
- 一个强大的图表库 -- ECharts
- ASP.net 使用(动态的有动画效果的)生成图表工具 Fusion Charts
- Sencha Touch Charts 使用 HTML5 构建的十分丰富的交互式图表
- Charts的使用
- 如果json字符串中的key值中没有数据解析用opt,也会出现空指针异常。
- Oracle_expdp_impdp用法详解
- @RequestBody和@ResponseBody的简单使用
- 最大公约数的实现
- 欢迎使用CSDN-markdown编辑器
- Swift 很强大的图表库-Charts使用
- 25.Spark Sort-Based Shuffle内幕彻底解密
- Java基础笔记:Java的基本语法
- 跑偏程序员自学cocos2d-x之二 创建UI 设置监听
- Struts2 - 常用的constant总结
- LeeCode-Sqrt(x)Implement int sqrt(int x). Compute and return the square root of x.
- 记第一次面试——求职在路上
- C# 索引器(Indexer)
- 剑指offer(32):连续子数组的最大和