gpt4 book ai didi

ios - 如何在 ios-charts 中为 BarChartView 添加 XAxis 标签

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

我添加了一个 bar chart到 Storyboard,但我无法为我的数据条目正确设置标签。

这是我的代码:

var names = ["aaa", "bbb", "ccc", "ddd"]
var values = [230.0, 280.0, 450.0, 340.0]

setChart(dataPoints: names, values: values)

设置图表函数:

func setChart(dataPoints: [String], values: [Double])
{
let formatter = BarChartFormatter()
formatter.setValues(values: dataPoints)
let xaxis:XAxis = XAxis()


barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = []

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

let chartDataSet = BarChartDataSet(values: dataEntries, label: "موجودی")

let chartData = BarChartData(dataSet: chartDataSet)


xaxis.valueFormatter = formatter
barChartView.xAxis.labelPosition = .bottom
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.xAxis.valueFormatter = xaxis.valueFormatter
barChartView.chartDescription?.enabled = false
barChartView.legend.enabled = true
barChartView.rightAxis.enabled = false
barChartView.data = chartData
}

最后是格式化程序:

@objc(BarChartFormatter)
public class BarChartFormatter: NSObject, IAxisValueFormatter
{
var names = [String]()

public func stringForValue(_ value: Double, axis: AxisBase?) -> String
{
return names[Int(value)]
}

public func setValues(values: [String])
{
self.names = values
}
}

但效果不佳,如下所示:

enter image description here

如此处所示,它添加了 6 个标签而不是 4 个标签,并且它也有重复项。

我已经阅读了 this solution ,但是,如您所见,它还存在一些问题。

我该如何解决这个问题?

最佳答案

我觉得你可以尝试设置以下属性:

barChartView.xAxis.granularityEnabled = true
barChartView.xAxis.granularity = 1.0 //default granularity is 1.0, but it is better to be explicit
barChartView.xAxis.decimals = 0

源代码告诉你很多关于上面的属性。 https://github.com/danielgindi/Charts/blob/master/Source/Charts/Components/AxisBase.swift

关于ios - 如何在 ios-charts 中为 BarChartView 添加 XAxis 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39960302/

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