ios 图表 3 - 绘制每个数据点的 X 轴标签
ios Charts 3 - Plot X Axis Labels per Data Point
我在图表上有 30 个数据点,但图表的 X 轴将数字分开,并且没有为每个数据点提供标签。见下文。
![1]: https://i.stack.imgur.com/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([=12=]), y: .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)
我在图表上有 30 个数据点,但图表的 X 轴将数字分开,并且没有为每个数据点提供标签。见下文。
![1]: https://i.stack.imgur.com/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([=12=]), y: .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)