My graph is using custom IAxisValueFormatters
to set the left and right axis labels (below). I've tried using the labelTextColor
properties on left and right axis (below) but they're not changing the label's font color.
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"
}
}
You have to add following code in viewDidLoad()
method. This is working perfactly for me in one of my project.
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
}
Also, update you code for class create as follow:
@objc(LineChartFormatter)
class SpeedAndHRRightYAxisValueFormatter:NSObject, IAxisValueFormatter {
....
//Your Code
....
}
@objc(LineChartFormatter)
class SpeedAndHRLeftYAxisValueFormatter:NSObject, IAxisValueFormatter {
....
//Your Code
....
}
I hope this will help you.