2023年7月14日发(作者:)
Swift-第三⽅图表库Charts使⽤详解4(折线图3:选中点⾼亮、⼗字线样式)三、选中后⼗字线样式当我们选中某个拐点时,该点会⾼亮(显⽰⼗字线),这个⼗字线的样式是可以修改的。1,修改⼗字线的样式ghtColor = .blue //⼗字线颜⾊ghtLineWidth = 2 //⼗字线线宽ghtLineDashLengths = [4, 2] //使⽤虚线样式的⼗字线2,只显⽰部分⼗字线(1)只显⽰横向⼗字线rticalHighlightIndicatorEnabled = false //不显⽰纵向⼗字线(2)只显⽰纵向⼗字线rizontalHighlightIndicatorEnabled = false //不显⽰横向⼗字线3,不显⽰⼗字线ghtEnabled = false //不启⽤⼗字线附:拐点被选中后⾃动改变颜⾊1,效果图当⼀个拐点被选中后默认只会在它的位置处显⽰⼀个⼗字线,这样可能还不够明显。下⾯我们将选中点的颜⾊做个变化,这样就与其他的点区分开了。2,样例代码import UIKitimport Chartsclass ViewController: UIViewController, ChartViewDelegate {//折线图var chartView: LineChartView!
//所有点的颜⾊var circleColors: [UIColor] = []
override func viewDidLoad() { dLoad()
//创建折线图组件对象 chartView = LineChartView() = CGRect(x: 20, y: 80, width: - 40, height: 270) te = self //设置代理 view(chartView)
//⽣成10条随机数据 var dataEntries = [ChartDataEntry]() for i in 0..<10 { let y = arc4random()%100 let entry = (x: Double(i), y: Double(y)) (entry) //每个点默认颜⾊都是蓝⾊ (.cyan) }
//这10条数据作为1根折线⾥的所有数据 let chartDataSet = LineChartDataSet(values: dataEntries, label: "图例1") //⽬前折线图只包括1根折线 let chartData = LineChartData(dataSets: [chartDataSet]) //设置折点颜⾊ Colors = circleColors //设置折现图数据 = chartData}
//折线上的点选中回调func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) { print("选中了⼀个数据")
//将选中的数据点的颜⾊改成黄⾊ var chartDataSet = LineChartDataSet() chartDataSet = (?.dataSets[0] as? LineChartDataSet)! let values = let index = (where: {$0.x == highlight.x}) //获取索引 Colors = circleColors //还原 Colors[index!] = .orange
//重新渲染表格 ?.notifyDataChanged() DataSetChanged()}
//折线上的点取消选中回调func chartValueNothingSelected(_ chartView: ChartViewBase) { print("取消选中的数据")
//还原所有点的颜⾊ var chartDataSet = LineChartDataSet() chartDataSet = (?.dataSets[0] as? LineChartDataSet)! Colors = circleColors
//重新渲染表格 ?.notifyDataChanged() DataSetChanged()}}
发布者:admin,转转请注明出处:http://www.yc00.com/news/1689264249a226412.html
评论列表(0条)