Search code examples
iosswiftchartsios-charts

Force to show min/max on x axis


I am working with library. Anybody know if is it possible to force show min and max label on x-axis.

What I have:

enter image description here

What I want to achieve:

enter image description here

My implementation:

private func prepareXAxis() {
    lineChart.xAxis.labelPosition = .bottom
    lineChart.xAxis.valueFormatter = ChartXAxisDateValueFormatter()
    lineChart.xAxis.labelFont = UIFont.pmd_robotoRegular(ofSize: 13.0)
    lineChart.xAxis.labelTextColor = UIColor.pmd_darkGrey
}

And implementation of my own ValueFormatter:

import Foundation
import Charts

class ChartXAxisDateValueFormatter: NSObject, IAxisValueFormatter {

    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "hh:mm\ndd MMM"

        return dateFormatter.string(from: Date(timeIntervalSince1970: value))
    }
}

Edit

I added this code but first and last label on x-axis is still skipped:

lineChart.xAxis.axisMinimum = lineDataSet.xMin
lineChart.xAxis.axisMaximum = lineDataSet.xMax
lineChart.xAxis.avoidFirstLastClippingEnabled = false
lineChart.xAxis.granularity = 1.0

I checked lineDataSet.xMin and lineDataSet.xMax. They have valid values.


Solution

  • I found a solution. The lost line:

    combinedChart.xAxis.forceLabelsEnabled = true

    I also changed type of chart from LineChart to CombinedChartView.