2016-02-15 103 views
3

我有一个BarChartView 31条,因此31标签。我添加所有内容并根据示例进行设置,它的工作原理与此相同 - 除了BarChartView仅显示每个第3个标签。所以我尝试打印出1,2,3,4,5,6,...,30,31,但它可以打印1,4,7,11,...,31。我怎样才能改变呢?我甚至不需要打印所有标签,我真正需要的是第一个和最后一个标签,所以1和31,但我不能在我的生活中弄清楚如何设置它。BarChartView x轴标签不显示所有标签

我已经试过了,是不是第一个或最后每一小节的标签设置为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) 
} 

回答

6

所以,对x轴,有axisLabelModulusAxisModulusCustom显示x轴标签时控制模量。

默认情况下,它会自动计算。

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) 
+0

谢谢,我解决了这个问题! – ClockWise

+0

@Wingzero在哪个版本的库中包含此功能? –

+0

@AbdulRehmanWarraich 2.X. 3.0不同的是 – Wingzero

6

pod 'Charts', '~> 3.0' 

使用本:

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

要显示所有设置为您的数据点数的标签。您可以输入像'9999999'这样的高数字,并且每个数据点都会使用一个标签。但是,如果你有分组集合,并且你想要每个集合有一个标签,这是行不通的。是否有一种简单的方法,像旧的方式来做到这一点,或每次我的数据改变时更新标签数量是唯一的方法? –