'iOS Charts - Bar Chart repeating labels on X axis

I have made a bar chart with 6 groups and I have manage to hack my way into centering them exactly below each group. Now, the problem is that the value inside of each label is repeating. My values array contains 6 different values and my label count is on 7. The labelCount needed to be 7 in order for chart to center them correctly. More over, I did set this value to 6 too, thinking that there is an indexing issue when the value formatter is picking values, and labels are still repeating.

I have already tried changing the granularity to all sorts of values and it does not seam to work. Also I fiddled around toggling granularity to true or false, but it does not work either.

Screenshot
Click here for screen shot

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]

lazy var barChart: BarChartView = {
    let chart = BarChartView()
    chart.translatesAutoresizingMaskIntoConstraints = false
    var inComing = [BarChartDataEntry]()
    var outgoing = [BarChartDataEntry]()
    for i in 0..<6 {
        let entry = BarChartDataEntry(x: Double(i), y: Double.random(in: 10..<100))
        inComing.append(entry)
    }
    for i in 6..<12 {
        let entry = BarChartDataEntry(x: Double(i), y: Double.random(in: 10..<100))
        outgoing.append(entry)
    }
    let set1 = BarChartDataSet(entries: inComing, label: "Incoming")
    let set2 = BarChartDataSet(entries: outgoing, label: "Outgoing")
    let incomingColor = UIColor(red: 51 / 255, green: 51 / 255, blue: 51 / 255, alpha: 1)
    set1.colors = [incomingColor]
    let outgoingColor = UIColor(red: 153 / 255, green: 0, blue: 0, alpha: 1)
    set2.colors = [outgoingColor]
    let data = BarChartData(dataSets: [set1, set2])
    let barWidth = 0.004
    let barSpace = 0.007
    let groupSpace = 0.032
    let groupCount = 6
    data.barWidth = barWidth
    let gg = data.groupWidth(groupSpace: groupSpace, barSpace: barSpace)

    chart.data = data
    chart.xAxis.axisMinimum = 0
    chart.xAxis.axisMaximum = gg * Double(groupCount)

    set1.drawValuesEnabled = false
    set2.drawValuesEnabled = false
    chart.xAxis.drawGridLinesEnabled = false
    chart.xAxis.drawAxisLineEnabled = true
    chart.drawBordersEnabled = true

    let formater = IndexAxisValueFormatter.with(values: months)
    chart.xAxis.centerAxisLabelsEnabled = true
    chart.xAxis.setLabelCount(7, force: true)

    chart.xAxis.granularityEnabled = true
    chart.xAxis.axisMaxLabels = 6
    chart.xAxis.axisMinLabels  = 0
    chart.xAxis.labelPosition = .bottom
    chart.xAxis.valueFormatter = formater
    chart.xAxis.drawLimitLinesBehindDataEnabled = false
    chart.leftAxis.drawGridLinesEnabled = false
    chart.leftAxis.drawAxisLineEnabled = false
    chart.rightAxis.drawGridLinesEnabled = false
    chart.rightAxis.drawAxisLineEnabled = false
    chart.rightAxis.drawLabelsEnabled = false

    data.groupBars(fromX: 0, groupSpace: groupSpace, barSpace: barSpace)
    chart.drawGridBackgroundEnabled = false
    chart.legend.enabled = false
    chart.notifyDataSetChanged()
    return chart
}()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source