2017-05-31 161 views
0

我有一個LineChartView的標籤leftAxis。我想在圖表上有一個偏移量,然後使用可用空間顯示標籤。目前,我有以下幾點:左軸標籤的位置

chartView.leftAxis.labelPosition = YAxis.LabelPosition.outsideChart 
chartView.leftAxis.xOffset = 12 

labelPosition只提供由外向內和insideChart。

但我想將標籤向左移一點,是否可以爲標籤指定偏移量或以某種方式將它們向左移動一點?

enter image description here

回答

0

這裏是一個更清潔溶液: 1.創建此類:

class MyIndexFormatter: IndexAxisValueFormatter { 

    open override func stringForValue(_ value: Double, axis: AxisBase?) -> String 
    { 
     return "\(value)  " 
    }  
} 
  • 在你的控制器:

    chartView.leftAxis.valueFormatter = MyIndexFormatter() 
    
  • 結果: enter image description here

    +0

    是的,我已經有了左側Axis的IndexAxisValueFormatter子類,正如我在下面寫的。我想現在我會採取這個解決方案。謝謝。 –

    0

    在y軸上的值可以被存儲爲字符串,然後可以添加空格到每個值將它們轉移到左側。

    var yvalues: [String] = [String]() 
    yvalues.append("0 ") 
    yvalues.append("1 ") 
    yvalues.append("2 ") 
    yvalues.append("3 ") 
    yvalues.append("4 ") 
    yvalues.append("5 ") 
    yvalues.append("6 ") 
    yvalues.append("7 ") 
    yvalues.append("8 ") 
    yvalues.append("9 ") 
    yvalues.append("10 ") 
    chartView.leftAxis.valueFormatter = IndexAxisValueFormatter(values: yvalues) 
    

    現在存在leftaxis和圖形之間更多的空間,比rightaxis和圖形之間: enter image description here

    +0

    我想過做這個爲好,因爲我已經有一個valueFormatter爲實現'stringForValue'的leftAxis。但是,這並不是一個乾淨的解決方案,但我想知道是否有其他解決方法。 –