gpt4 book ai didi

ios Charts 3 - 为每个数据点绘制 X 轴标签

转载 作者:行者123 更新时间:2023-11-28 08:08:50 27 4
gpt4 key购买 nike

我在图表上有 30 个数据点,但图表的 X 轴将数字分开,并且没有为每个数据点提供标签。见下文。

![1]: /image/I1vAP.png

class TrendChartViewController: UIViewController {
@IBOutlet var combinedChart: CombinedChartView!
var viewModel: TrendChartViewModelProtocol! {
didSet {
updateChart()
}
}

private func updateChart() {
if let dataset = viewModel.dataset {
let data = CombinedChartData()
data.lineData = LineChartData(dataSets: [dataset.current, dataset.py])
data.barData = getBarChartData(dataset: dataset.budget)
combinedChart.data = data
combinedChart.xAxis.valueFormatter = DateValueFormatter(timeframe: viewModel.timeframe)
removeGridLines()
}
}

private func getBarChartData(dataset: BarChartDataSet) -> BarChartData {
var barChartData = BarChartData(dataSet: dataset)
barChartData.barWidth = 0.45
return barChartData
}

private func removeGridLines() {
combinedChart.xAxis.drawGridLinesEnabled = false
combinedChart.leftAxis.drawGridLinesEnabled = false
combinedChart.rightAxis.drawGridLinesEnabled = false
combinedChart.drawGridBackgroundEnabled = false
}

这是我们数据集之一的示例...

private func getCurrentDataSet() -> LineChartDataSet {
let values = timeframe == .days ? report?.charts[0].data.days : report?.charts[0].data.months
let currentEntries: [ChartDataEntry] = values?
.enumerated()
.flatMap {
return ChartDataEntry(x: Double($0), y: $1.current[unit.rawValue] ?? 0)
} ?? []

let currentDataSet = LineChartDataSet(values: currentEntries, label: NSLocalizedString("Current", comment: ""))
let color = UIColor.Bridgestone.forest.B500
currentDataSet.setColor(color)
currentDataSet.circleColors = [color]
currentDataSet.circleHoleColor = color
currentDataSet.circleRadius = 5
currentDataSet.lineWidth = 2.5
return currentDataSet
}

那么我怎样才能让它显示每个数据点的 X 轴标签值呢?谢谢!

最佳答案

如果你想在 xAxis 上设置标签数量,你需要使用 func xAxis.setLabelCount,例如

combinedChart.xAxis.setLabelCount(countOfMyDataPoints, force: true)

关于ios Charts 3 - 为每个数据点绘制 X 轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44333117/

27 4 0