我的图形使用自定义IAxisValueFormatters
来设置左右轴标签(如下)。我尝试过使用左右两边的labelTextColor
属性(如下),但是它们没有改变标签的字体颜色。
class myViewController {
func initalizeGraph() {
//omitted for brevity
let leftYAxisValueFormatter = SpeedAndHRLeftYAxisValueFormatter(measurementSystem: measurementSystem)
lineChartView.leftAxis.labelTextColor = iPhoneThemeColor1
lineChartView.leftAxis.valueFormatter = leftYAxisValueFormatter
let rightYAxisValueFormatter = SpeedAndHRRightYAxisValueFormatter()
lineChartView.rightAxis.labelTextColor = iPhoneThemeColor2
lineChartView.rightAxis.valueFormatter = rightYAxisValueFormatter
}
}
class SpeedAndHRLeftYAxisValueFormatter: IAxisValueFormatter {
//left side is speed
//multiplu value x 40 (.5 * x = 20?)
var measurementSystem: MeasurementSystem
init(measurementSystem: MeasurementSystem) {
self.measurementSystem = measurementSystem
}
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
let intValue = Int(value * 40)
var suffixString = ""
switch measurementSystem {
case .US:
suffixString = "mph"
case .Metric:
suffixString = "km/h"
}
return "\(intValue)" + " " + suffixString
}
}
class SpeedAndHRRightYAxisValueFormatter: IAxisValueFormatter {
//right side is HR
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
let intValue = Int(value * 210)
return "\(intValue)" + " " + "bpm"
}
}
答案 0 :(得分:2)
您必须在viewDidLoad()
方法中添加以下代码。在我的一个项目中,这对我来说确实很有效。
override func viewDidLoad() {
let leftYAxisValueFormatter = SpeedAndHRLeftYAxisValueFormatter(measurementSystem: measurementSystem)
lineChartView.leftAxis.valueFormatter = leftYAxisValueFormatter
lineChartView.leftAxis.labelTextColor = iPhoneThemeColor1
let rightYAxisValueFormatter = SpeedAndHRRightYAxisValueFormatter()
lineChartView.rightAxis.valueFormatter = rightYAxisValueFormatter
lineChartView.rightAxis.labelTextColor = iPhoneThemeColor2
}
此外,请按照以下步骤更新用于创建类的代码:
@objc(LineChartFormatter)
class SpeedAndHRRightYAxisValueFormatter:NSObject, IAxisValueFormatter {
....
//Your Code
....
}
@objc(LineChartFormatter)
class SpeedAndHRLeftYAxisValueFormatter:NSObject, IAxisValueFormatter {
....
//Your Code
....
}
我希望这会对您有所帮助。