如何在 iOS 图表上正确显示底轴 (xVals) 上的标签?

How do I display labels on bottom axis (xVals) correctly on iOS Chart?

我一直在学习非常简单的 iOS 图表教程。

我的图表中的值现在可以正确显示,但底部的标签没有显示。

    override func viewDidLoad() {

    results =  ["Won", "Drawn", "Lost"]
    let games = [totalWins, totalDraws, totalLosses]
    setChart(dataPoints: results, values: games)

    }

    // CHART FUNCTION ************

    func setChart(dataPoints: [String], values: [Double]){

    barChartView.noDataText = "you need to provide some data for the chart."

    var dataEntries: [BarChartDataEntry] = Array()

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


    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Games Played")

    //let chartData = BarChartData(xVals: self.results, dataSet: dataEntries)
    let chartData = BarChartData()
    self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
    barChartView.leftAxis.granularityEnabled = true
    barChartView.rightAxis.enabled = false
    barChartView.leftAxis.granularity = 1.0
    chartData.addDataSet(chartDataSet)
    barChartView.data = chartData

}

// END OF CHART FUNCTION ***********

如您所见,它显示的是数字,而不是 "Won, Drawn, Lost"

我相信这是因为我需要在这样的命令中分配标签:

let chartData = BarChartData(xVals: self.results, dataSet: dataSet)
chartView.data = chartData

但是我收到错误并且我不知道数据集中需要什么,因为我从另一个线程中获取了该解决方案并且似乎无法修改它以使其工作。

临时图像:

您交换了 x 和 y 值。

let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])

要获得底部的 X 轴,您必须添加以下内容。

self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom

请找到我的样本数据集工作代码。

class BarChartViewController: UIViewController {
  @IBOutlet weak var barChartView: BarChartView!
      override func viewDidLoad() {
      super.viewDidLoad()
      let values = [11.00, 90.95, 250.00, 40.90, 60.88, 99.99, 25.00]
      setChart(values: values)
    }

    func setChart(values: [Double]) {
        var daysEntries: [BarChartDataEntry] = []

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

        let data = BarChartData()

        let ds1 = BarChartDataSet(values: daysEntries, label: "Days")
        ds1.colors = [NSUIColor.red]

        data.addDataSet(ds1)

        self.barChartView.data = data
        self.barChartView.gridBackgroundColor = NSUIColor.white
        self.barChartView.chartDescription?.text = "Barchart Days and Gross"
        self.barChartView.rightAxis.enabled = true
        self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
        self.barChartView.xAxis.axisRange = 5.0
    }

    override func viewWillAppear(_ animated: Bool) {
        self.barChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)
    }
}

谢谢 斯里拉姆

我遇到了同样的问题,我用这三行修复了它:

self.xAxis.granularity = 1
self.xAxis.granularityEnabled = true
self.xAxis.labelCount = // number of points on X axis 
    // xAxis.labelFont = UIFont.systemFont(ofSize: 13.0)
      // xAxis.labelTextColor = .lightGray
       xAxis.labelPosition = .bottom
      xAxis.setLabelCount(dataPoints.count, force: false)
    if xAxis.labelPosition == .bottom {
     xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)
    }
    xAxis.granularity = 1.0

希望这对您控制 xAxis 标签位置有所帮助。

  • 第一种方法

    chartView.setExtraOffsets(left: 20, top: 0, right: 0, bottom: 0)
    
  • 第二种方法

    chartView.xAxis.avoidFirstLastClippingEnabled = true