Chris Campbell
Chris Campbell

Reputation: 313

Swift Charts X-Axis Not Displaying?

I am using Swift Charts to create charts for an app but I cant seem to get the X-Axis labels to display.

The chart is configured as follows:

func configure (dataPoints: [String], values: [Double]) {
    ChartIMG.noDataText = "Please Insert Some Data!"
    ChartIMG.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)
    ChartIMG.xAxis.granularityEnabled = true
    ChartIMG.xAxis.granularity = 1


    ChartIMG.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBounce)

    print(dataPoints)

    var dataEntries: [BarChartDataEntry] = []

    for i in 0..<dataPoints.count {
        let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
        dataEntries.append(dataEntry)
    }

    let chartDataSet = BarChartDataSet(values: dataEntries, label: nil)
    ChartIMG.chartDescription?.enabled = false
    self.ChartIMG.legend.enabled = false
    let chartData = BarChartData(dataSet: chartDataSet)
    ChartIMG.data = chartData


}

and the datasets are as follows:

datapoints: moodList = ["Great", "Good", "Meh", "Sad", "Awful"]

Values: valueList = [1.0, 2.0, 3.0, 1.0, 3.0]

The chart X-Axis currently shows nothing and I cant seem to figure out why? Any help is much appreciated.

Upvotes: 0

Views: 1789

Answers (1)

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16446

You have to use IAxisValueFormatter to show the values on xAxis

I have created Following class

class BarChartFormatter: NSObject,IAxisValueFormatter,IValueFormatter {


    var values : [String]
    required init (values : [String]) {
        self.values = values
        super.init()
    }


    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return values[Int(value)]

    }

    func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
        return values[Int(entry.x)]

    }
}

Update your method like below

   for i in 0..<dataPoints.count {
    let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
    dataEntries.append(dataEntry)
} 


let chartDataSet = BarChartDataSet(values: dataEntries, label: nil)
ChartIMG.chartDescription?.enabled = false
self.ChartIMG.legend.enabled = false

let formatter = BarChartFormatter(values: dataPoints)
let xAxis = XAxis()
xAxis.valueFormatter = formatter
ChartIMG.xAxis.valueFormatter = xAxis.valueFormatter

let chartData = BarChartData(dataSet: chartDataSet)
ChartIMG.data = chartData

Hope it is helpful

OR

I have created a extension that is ready to use Just copy and paste it new swift file

import  Charts


extension BarChartView {

    private class BarChartFormatter: NSObject,IAxisValueFormatter {

        var values : [String]
        required init (values : [String]) {
            self.values = values
            super.init()
        }


        func stringForValue(_ value: Double, axis: AxisBase?) -> String {
            return values[Int(value)]
        }
    }

    func setChartValues (xAxisValues : [String] , values : [Double],label : String) {

        var barChartDataEntries = [BarChartDataEntry]()

        for i in 0..<values.count {
            let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
            barChartDataEntries.append(dataEntry)
        }
        let chartDataSet = BarChartDataSet(values: barChartDataEntries, label: label)
        chartDataSet.colors = ChartColorTemplates.colorful()
        let chartData = BarChartData(dataSet: chartDataSet)

        let formatter = BarChartFormatter(values: xAxisValues)
        let xAxis = XAxis()
        xAxis.valueFormatter = formatter
        self.xAxis.valueFormatter = xAxis.valueFormatter
        self.xAxis.labelPosition = .bottom

        self.data = chartData
        self.data?.notifyDataChanged()
        self.notifyDataSetChanged()

        self.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .linear)

    }

}

You can use it like

   @IBOutlet weak var barChartView: BarChartView!

    let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    let unitsSold = [20.0, 4.0, 3.0, 6.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
    self.barChartView.setChartValues(xAxisValues: months, values: unitsSold, label: "Monthly Sales")

Upvotes: 2

Related Questions