簡體   English   中英

iOS 圖表水平條形圖不顯示條形值

[英]iOS Chart Horizontal Bar Chart not showing bar values

我正在使用 iOS 圖表 (V3.3) 繪制水平條形圖,用於體育應用程序的球員表現統計。 我希望在每種情況下都將條形值顯示在條形旁邊。 只要玩家同時擁有負數和正數表現點,我就可以做到這一點,但如果玩家只有正數表現點,則不會顯示任何值。

帶有負點和正點的圖表顯示條形值

只有正值的圖表不顯示條形值

我在自己的 class 中有 BarChartView,並且正在使用此代碼

import Charts

class PlayerPerformanceBarChart: HorizontalBarChartView, IValueFormatter {

override init(frame: CGRect) {
    super.init(frame: frame)
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setChart(pf: [PerformanceStat]) {
    noDataTextColor = Style.labelText
    noDataText = gf.getLiteral("noPerformanceData")
    isUserInteractionEnabled = false
    legend.enabled = false
    leftAxis.enabled = false
    rightAxis.enabled = false
    chartDescription?.text = nil
    drawValueAboveBarEnabled = true

    // Data
    var dataEntries: [BarChartDataEntry] = []
    var labels: [String] = []
    for i in 0..<pf.count {
        dataEntries.append(BarChartDataEntry(x: Double(i), y: Double(pf[i].playerPoints)))
        labels.append(pf[i].playerAction)
    }
    let chartDataSet = BarChartDataSet(entries: dataEntries, label: nil)
    chartDataSet.colors = ChartColorTemplates.joyful()
    chartDataSet.valueFormatter = self
    let chartData = BarChartData(dataSets: [chartDataSet])
    chartData.setDrawValues(true)
    
    // X Axis - which for a Horizontal Bar Chart is actually the vertical (Y) axis - I know, crazy but true!!
    let xaxis = self.xAxis
    xaxis.drawGridLinesEnabled = false
    xaxis.labelPosition = .bottom
    xaxis.labelCount = pf.count
    xaxis.axisMinimum = -1
    xaxis.resetCustomAxisMin()
    xaxis.centerAxisLabelsEnabled = false
    xaxis.granularity = 1
    xaxis.granularityEnabled = true
    xaxis.labelTextColor = Style.chartText
    xaxis.valueFormatter = IndexAxisValueFormatter(values: labels)
    clipValuesToContentEnabled = true

    //Chart Format and Display
    data = chartData
}

func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
    return Int(value).description
}

}

function setChart 由包含視圖(這是一個 UIScrollView)調用

它只是一個單一的數據集,所以沒有分組。 我不知道為什么當沒有負值要顯示時這些值不會顯示。 查看圖表代碼,這與欄的 position 超出視口處理程序的范圍有關,但我不明白為什么會這樣。

任何幫助,將不勝感激

最好創建一個單獨的 class 負責值格式化。

import UIKit
import Charts

class ValueFormatter: NSObject, IAxisValueFormatter {
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
       /// your code for formatting the values
    }
}

您應該將自定義 class 分配為您的值格式化程序:

xAxis.valueFormatter = ValueFormatter()

除此之外,請查看有關如何為標簽設置自定義值的精彩教程: https://medium.com/@spmandrus/ios-charts-custom-y-axis-values-e03cac8850c

暫無
暫無

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

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