Search code examples
iosswiftcharts

ios Charts change grid lines color


I have made a small application measuring Heart Rate from iWatch. The chart is working as intended showing current heart rate, but I have several issues with the design. Below you can find the image of my chart and its setting in swift.

enter image description here

// chart set up
self.chtChart.delegate = self as? ChartViewDelegate
self.chtChart.noDataTextColor = UIColor.white
self.chtChart.noDataText = "Press Start button to display the chart"

let set_a: LineChartDataSet = .init(entries: [ChartDataEntry(x: Double(Int(0)), y: self.valueHR)], label: "BPM")
set_a.drawCirclesEnabled = false
set_a.setColor(UIColor.systemPink)
set_a.drawValuesEnabled = false
set_a.lineWidth = 5.0

// remove vertical grid and labels
self.chtChart.xAxis.drawGridLinesEnabled = false
self.chtChart.rightAxis.drawLabelsEnabled = false
self.chtChart.xAxis.drawLabelsEnabled = false

// change label 'BPM' color
self.chtChart.legend.textColor = UIColor.white
// change left numbers (bpm values)
self.chtChart.leftAxis.labelTextColor = UIColor.white
// change right border line color
self.chtChart.rightAxis.axisLineColor = UIColor.white
// change left border line to white
self.chtChart.leftAxis.axisLineColor = UIColor.white

// change values from decimal to int on left axis
let fmt = NumberFormatter()
fmt.numberStyle = .decimal
fmt.maximumFractionDigits = 0
fmt.groupingSeparator = ","
fmt.decimalSeparator = "."
self.chtChart.leftAxis.valueFormatter = DefaultAxisValueFormatter(formatter: fmt)

self.chtChart.data = LineChartData(dataSets: [set_a])

As you can see, my chart does not have the bottom border and I also can't change the color of the grid line inside the chart (I want to change it to white). I have tried following commands but nothing was changed:

self.chtChart.xAxis.axisLineColor = UIColor.white
self.chtChart.borderColor = UIColor.white
self.chtChart.xAxis.gridColor = UIColor.white

Solution

  • I was going through all possible attributes and found that adding the lines:

    self.chtChart.xAxis.axisLineColor = UIColor.white self.chtChart.rightAxis.gridColor = UIColor.white

    solves the problem with the bottom border and also changes the color of the grid lines to white.

    Edit: the bottom line problem still appears to be an issue. It does not show on the screen.

    Edit2: adding the all borders at once and setting their color seems to solve the issue:

    self.chtChart.drawBordersEnabled = true self.chtChart.borderColor = UIColor.white