Search code examples
iosswiftios-charts

BarChartView xAxis label doesn't display all labels


I have a BarChartView with 31 bars, and thus 31 labels. I add everything and set it up according to examples and it works as it should - except that the BarChartView only displays each 3rd label. So I try to print out 1, 2, 3, 4, 5, 6,..., 30, 31 but It prints 1, 4, 7, 11,..., 31. How can I change this? I don't even need to print all labels, all I really need is the first and last label, so 1 and 31, but I can't for the life of me figure out how to set this.

I've tried setting the label to nil for each bar that is not the first or the last, but that doesn't do anything either. I believe the reason it prints each 3rd label is because it can't fit them all in, correct? My setup for the BarChartView is:

    barChartView.descriptionText = ""
    barChartView.noDataTextDescription = ""
    barChartView.drawGridBackgroundEnabled = false
    barChartView.xAxis.drawAxisLineEnabled = false
    barChartView.xAxis.drawGridLinesEnabled = false
    barChartView.xAxis.drawLabelsEnabled = true
    barChartView.drawBordersEnabled = false
    barChartView.leftAxis.enabled = false
    barChartView.rightAxis.enabled = false
    barChartView.legend.enabled = false
    barChartView.xAxis.labelPosition = .Bottom
    barChartView.xAxis.labelTextColor = UIColor.whiteColor()
    barChartView.xAxis.labelFont = UIFont(name: timesNewRoman, size: barChartView.xAxis.labelFont.pointSize)!
    barChartView.dragEnabled = false
    barChartView.highlightEnabled = false
    barChartView.scaleXEnabled = false
    barChartView.scaleYEnabled = false

and I add the xValues like this:

for (index, value) in plotData.enumerate() {
        var xValue: String?
        xValue = index == 0 || index == plotData.count-1 ? "\(index + 1)" : nil


        barChartData.addXValue(xValue)
        let dataEntry = BarChartDataEntry(value: Double(value), xIndex: index)

        dataEntries.append(dataEntry)
}

Solution

  • So, for x axis, there is axisLabelModulus and AxisModulusCustom to control the modulus when displaying x axis labels.

    By default it's automatically calculated.

    public var axisLabelModulus = Int(1)
    /// Is axisLabelModulus a custom value or auto calculated? If false, then it's auto, if true, then custom.
    /// 
    /// **default**: false (automatic modulus)
    private var _isAxisModulusCustom = false
    

    you can use setLabelsToSkip() to set how many labels to be skipped.

    /// Sets the number of labels that should be skipped on the axis before the next label is drawn. 
    /// This will disable the feature that automatically calculates an adequate space between the axis labels and set the number of labels to be skipped to the fixed number provided by this method. 
    /// Call `resetLabelsToSkip(...)` to re-enable automatic calculation.
    public func setLabelsToSkip(count: Int)