簡體   English   中英

Swift iOS 圖表 - 隱藏某些條形圖值標簽

[英]Swift iOS Charts - Hide certain Bar Chart value labels

我正在使用 swift 為我的 iOS 應用程序使用“圖表”框架,我在我的一個 VC 上使用條形圖,但目前看起來很亂,因為它顯示每個值的值標簽,即使它是 0。我想知道如果值大於 0 是否可以只顯示標簽? 我一直在尋找其他問題,但我似乎無法找到答案。 我知道我可以使用chartData.setDrawValues(false)隱藏所有標簽,但是,我仍然想在值高於 0 時顯示它們。

在此處輸入圖像描述

這是我當前用於格式化圖表的代碼 -

    barChart.highlightPerTapEnabled = false
    barChart.highlightPerDragEnabled = false
    barChart.leftAxis.drawAxisLineEnabled = true
    barChart.leftAxis.drawGridLinesEnabled = false
    barChart.xAxis.labelPosition = .bottom
    barChart.xAxis.centerAxisLabelsEnabled = true
    barChart.xAxis.axisMinimum = 0
    barChart.xAxis.axisMaximum = 7
    barChart.xAxis.drawAxisLineEnabled = true
    barChart.xAxis.drawGridLinesEnabled = true
    barChart.rightAxis.enabled = false
    barChart.leftAxis.axisMinimum = 0

我想過做這樣的事情 -

 for value in chartDataSet.entries {
    if value.y == 0 {
      chartDataSet.drawValuesEnabled = false
    }
    else {
      chartDataSet.drawValuesEnabled = true
    }
 }

但當然這不起作用,因為它為整個集合設置了 boolean。

任何幫助將非常感激! 謝謝

您只需向BarChartData提供您的自定義IValueFormatter ,如下所示,

public class XValueFormatter: NSObject, IValueFormatter {

    public func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
        return value <= 0.0 ? "" : String(describing: value)
    }
}

let chartData = BarChartData(dataSet: yourSet)
chartData.setValueFormatter(XValueFormatter())

注意: String(describing: value)只是為了提供一個可行的例子。 您可以應用適當的數字格式化程序將Double轉換為String

 barChartView.data?.setDrawValues(false)
// to disable the data in the bars

在此處輸入圖像描述

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM