五、Y 轴的样式设置

1,只显示左侧的 Y 轴

(1)默认情况下表格左右两侧均有 Y 轴显示:左侧 Y 轴为 leftAxis、右侧 Y 轴为 rightAxis。

swift model 在线生成 swift chart_文字颜色

(2)下面不显示右侧 Y 轴的刻度文字。

swift model 在线生成 swift chart_文字颜色_02

chartView.rightAxis.drawLabelsEnabled = false //不绘制右侧Y轴文字

(3)右侧 Y 轴的文字和轴线均不显示。

swift model 在线生成 swift chart_swift model 在线生成_03

chartView.rightAxis.enabled = false //禁用右侧的Y轴
 //chartView.rightAxis.drawLabelsEnabled = false //不绘制右侧Y轴文字
 //chartView.rightAxis.drawAxisLineEnabled = false //不显示右侧Y轴

2,值反向排列

默认情况下,Y 轴刻度是自下而上逐渐变大的。通过 inverted 属性可以将其反转,即最小值在最上方。

swift model 在线生成 swift chart_swift model 在线生成_04

chartView.leftAxis.inverted = true //刻度值反向排列

3,设置坐标轴刻度文字显示位置

和 X 轴一样,我们也可以将 Y 轴文字显示在轴线内侧。

swift model 在线生成 swift chart_文字颜色_05

chartView.leftAxis.labelPosition = .insideChart //文字显示在内侧

4,设置 Y 轴的颜色和线宽

swift model 在线生成 swift chart_文字颜色_06

chartView.leftAxis.axisLineWidth = 2 //左x轴宽度
 chartView.leftAxis.axisLineColor = .orange //左x轴颜色

5,指定最小、最大刻度值

swift model 在线生成 swift chart_swift model 在线生成_07

chartView.leftAxis.axisMinimum = -100 //最小刻度值
 chartView.leftAxis.axisMaximum = 100 //最大刻度值

6,指定刻度间的最小间隔

swift model 在线生成 swift chart_文字颜色_08

chartView.leftAxis.axisMinimum = -100 //最小刻度值
 chartView.leftAxis.axisMaximum = 100 //最大刻度值
 chartView.leftAxis.granularity = 50 //最小间隔

7,绘制 0 刻度线

(1)将 drawZeroLineEnabled 设置为 true 则会在 0 刻度位置绘制一条有别于其它网格线的横线。

swift model 在线生成 swift chart_最小值_09

chartView.leftAxis.drawZeroLineEnabled = true //绘制0刻度线

(2)0 刻度线的样式也是可以修改的:
原文:Swift - 第三方图表库Charts使用详解6(折线图5:Y轴的样式设置)

chartView.leftAxis.drawZeroLineEnabled = true //绘制0刻度线
 chartView.leftAxis.zeroLineColor = .orange //0刻度线颜色
 chartView.leftAxis.zeroLineWidth = 2 //0刻度线线宽
 chartView.leftAxis.zeroLineDashLengths = [4, 2] //0刻度线使用虚线样式

8,设置刻度文字的样式

swift model 在线生成 swift chart_折线图_10

chartView.leftAxis.labelTextColor = .orange //刻度文字颜色
 chartView.leftAxis.labelFont = .systemFont(ofSize: 14) //刻度文字大小

9,设置对应网格线的样式

(1)修改网格线的颜色和粗细。

swift model 在线生成 swift chart_swift model 在线生成_11

chartView.leftAxis.gridColor = .orange //左Y轴对应网格线的颜色
 chartView.leftAxis.gridLineWidth = 2 //右Y轴对应网格线的大小

(2)显示虚线形式的网格线。

swift model 在线生成 swift chart_折线图_12

chartView.xAxis.gridLineDashLengths = [4,2] //虚线各段长度

(3)不显示网格线

swift model 在线生成 swift chart_折线图_13

chartView.leftAxis.drawGridLinesEnabled = false //不绘制网格线

10,刻度文字的格式化

swift model 在线生成 swift chart_折线图_14

let formatter = NumberFormatter() //自定义格式
 formatter.positiveSuffix = “%” //数字后缀
 chartView.leftAxis.valueFormatter = DefaultAxisValueFormatter(formatter: formatter)