JonEasy JonEasy - 11 months ago 87
Swift Question

XAxis labels not shown despite stringForValue getting called

I have a LineChartView and I want to show labels for the xAxis.
My code looks like this:

chartView.chartDescription?.enabled = false
chartView.drawGridBackgroundEnabled = false
chartView.dragEnabled = !chartView.isFullyZoomedOut
chartView.setScaleEnabled(true)
chartView.pinchZoomEnabled = true
chartView.setViewPortOffsets(left: 20.0, top: 0.0, right: 20.0, bottom: 0.0)

chartView.legend.enabled = true
chartView.legend.textColor = legendColor
chartView.legend.form = .empty

chartView.leftAxis.enabled = false
chartView.leftAxis.spaceTop = 0.4
chartView.leftAxis.spaceBottom = 0.4
chartView.rightAxis.enabled = false

chartView.xAxis.enabled = true
chartView.xAxis.labelTextColor = UIColor.black
chartView.xAxis.valueFormatter = self
chartView.xAxis.drawGridLinesEnabled = false
chartView.xAxis.drawLabelsEnabled = true
chartView.xAxis.drawAxisLineEnabled = false

chartView.highlightPerTapEnabled = false
chartView.highlightPerDragEnabled = false

if lineChartData.entryCount > 0 {
chartView.data = lineChartData
}
chartView.noDataText = NSLocalizedString("No chart data available", comment: String())
chartView.maxVisibleCount = 12
chartView.delegate = self


and

extension ChartCell: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return "foo"
}


}

In the debugger I see that stringForValue is being called, however in the drawn charts there are no labels.
line chart without xAxis labels
I'm at a loss what I am missing here.

Answer Source

You have to set below :

chartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download