BarChartView xAxis标签不显示所有标签

时间:2016-02-15 16:06:57

标签: ios swift ios-charts

我有一个BarChartView,有31个小节,因此有31个标签。我添加了所有内容并根据示例进行设置,它可以正常工作 - 除了BarChartView仅显示每个第三个标签。所以我尝试打印出1,2,3,4,5,6,......,30,31,但是它打印了1,4,7,11,......,31。我怎么能改变这个?我甚至不需要打印所有标签,我真正需要的是第一个和最后一个标签,所以1和31,但我不能为我的生活弄清楚如何设置它。

我已尝试为每个不是第一个或最后一个条形的条形设置标签nil,但这也没有做任何事情。我相信它打印每个第三个标签的原因是因为它不适合它们,对吗?我对BarChartView的设置是:

    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

我添加xValues就像这样:

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)
}

2 个答案:

答案 0 :(得分:6)

因此,对于x轴,在显示x轴标签时有axisLabelModulusAxisModulusCustom来控制模数。

默认情况下会自动计算。

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

您可以使用setLabelsToSkip()来设置要跳过的标签数量。

/// 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)

答案 1 :(得分:6)

pod 'Charts', '~> 3.0'

使用它:

public func setLabelCount(count: Int, force: Bool)