gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:05:32 27 4
gpt4 key购买 nike

我一直在学习非常简单的 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

最佳答案

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

谢谢斯里拉姆

关于ios - 如何在 iOS 图表上正确显示底轴 (xVals) 上的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41481418/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com