2017-05-26 137 views
2

我最近升級我的客戶項目之一,從Swift2和iOS-圖表2 Swift3和iOS,圖表3.水平條形圖標籤

大部分代碼都經過手工調整的幾天如預期運行,不過,我仍然遇到了橫向條形圖問題。

的圖表,用於顯示爲:

enter image description here

圖表現在作爲顯示:

enter image description here

核心代碼圖表:

func drawMarginChart() { 
    let labels = marginObject!.getLabelsForChart() 
    let values = marginObject!.getValuesForChart() 

    var dataEntries = [ChartDataEntry]() 

    for i in 0..<values.count { 
     let entry = BarChartDataEntry(x: Double(i), y: values[i]) 
     dataEntries.append(entry) 
    } 

    let barChartDataSet = BarChartDataSet(values: dataEntries, label: "") 
    barChartDataSet.drawValuesEnabled = false 
    barChartDataSet.colors = ChartColorTemplates.joyful() 

    let barChartData = BarChartData(dataSet: barChartDataSet) 
    barChartMargins.data = barChartData 
    barChartMargins.legend.enabled = false 

    barChartMargins.xAxis.valueFormatter = IndexAxisValueFormatter(values: labels) 
    barChartMargins.xAxis.granularityEnabled = true 
    barChartMargins.xAxis.granularity = 1 

    barChartMargins.animate(xAxisDuration: 3.0, yAxisDuration: 3.0, easingOption: .easeInOutBounce) 
} 

什麼我希望能找出是:

1)我怎樣才能標籤出現在圖表

2)怎樣才能圖表自動調整,在每個酒吧顯示標籤

如果這些其他問題可以得到解決,那將是非常美妙的,但我們都願意出貨沒有這些:

1)我如何刪除gridlin e在圖表的左側重疊?

2)如何保持圖表底部截斷?

我已經嘗試調整許多圖表屬性,我發現這裏和其他網站上的每個帖子,但是,沒有任何修復問題。任何幫助圖表出現像舊版本的幫助都會得到我和我的客戶的高度讚賞。

回答

1

我測試了一下,發現當有8個值和標籤時,一切正常。當有9個或更多值和標籤時,只顯示一半標籤。您可以設置可見值的數量與此屏幕:

barChartMargins.setVisibleXRange(minXRange: 8.0, maxXRange: 8.0) 

這意味着圖表將是滾動的,如果你有比maxXRange更多的價值。

,並用這個代碼縮放你會看到所有的標籤:

barChartMargins.zoom(scaleX: 1.1, scaleY: 1.0, x: 0, y: 0) 

按捏縮放到左,右,你會看到所有的標籤出現在某些情況下,但不是所有的(而不是在你的有10個標籤的情況)。但是,如果您首先將maxXRange設置爲8並且允許像我一樣在示例中進行滾動,則它可以工作。 enter image description here 這裏是全碼我用(與iPhone 5和6Plus測試),最後一行解決了截斷問題:

func drawMarginChart() { 

    let values = [1000, 2000, 3000, 5000, 7000, 8000, 15000, 21000, 22000, 35000] 
    let labels = ["Blue Yonder Airlines", "Aaron Fitz Electrical", "Central Communications", "Magnificent Office Images", "Breakthrough Telemarke", "Lawrence Telemarketing", "Vancouver Resort Hotels", "Mahler State University", "Astor Suites", "Plaza One"] 



    var dataEntries = [ChartDataEntry]() 

    for i in 0..<values.count { 
     let entry = BarChartDataEntry(x: Double(i), y: Double(values[i])) 

     dataEntries.append(entry) 
    } 

    let barChartDataSet = BarChartDataSet(values: dataEntries, label: "") 
    barChartDataSet.drawValuesEnabled = false 
    barChartDataSet.colors = ChartColorTemplates.joyful() 

    let barChartData = BarChartData(dataSet: barChartDataSet) 
    barChartMargins.data = barChartData 
    barChartMargins.legend.enabled = false 

    barChartMargins.xAxis.valueFormatter = IndexAxisValueFormatter(values: labels) 
    barChartMargins.xAxis.granularityEnabled = true 
    barChartMargins.xAxis.granularity = 1 

    barChartMargins.animate(xAxisDuration: 3.0, yAxisDuration: 3.0, easingOption: .easeInOutBounce) 

    barChartMargins.chartDescription?.text = "" 


    barChartMargins.zoom(scaleX: 1.1, scaleY: 1.0, x: 0, y: 0) 

    let rightAxis = barChartMargins.rightAxis 
    rightAxis.drawGridLinesEnabled = false 

    let leftAxis = barChartMargins.leftAxis 
    leftAxis.drawGridLinesEnabled = false 

    let xAxis = barChartMargins.xAxis 
    xAxis.drawGridLinesEnabled = false 
    barChartMargins.setVisibleXRange(minXRange: 8.0, maxXRange: 8.0) 

    barChartMargins.setExtraOffsets (left: 0, top: 20.0, right:0.0, bottom: 20.0) 



} 

順便說一句。我沒有遇到標籤在屏幕外的問題,但這可能與您的chartview的尺寸和約束有關,或者可能需要使用barChartMargins.zoom

+0

可能需要更多縮放。請您將答案標記爲接受if這個答案幫助你? – DevB2F