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

时间:2017-01-05 09:32:37

标签: ios swift charts ios-charts

我一直在关注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 ***********

正如你所看到的那样,它显示的是数字,而不是“赢,拉,失去”

enter image description here

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

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

但是我得到了错误,我不知道在dataSet中需要什么,因为我从另一个线程中获取了该解决方案,似乎无法将其修改为工作。

临时图片:

enter image description here

4 个答案:

答案 0 :(得分:2)

您可以互换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)
    }
}

由于 斯利拉姆

答案 1 :(得分:2)

我有同样的问题,我用这三行修正了它:

[ "123-12 abc, qwerty", "pol, T. top", "14-89 toy, rap"]

答案 2 :(得分:0)

AttributeError: 'bytes' object has no attribute 'n'

答案 3 :(得分:0)

我希望这可以帮助您控制xAxis标签的位置。

  • 第一种方法

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

    chartView.xAxis.avoidFirstLastClippingEnabled = true