我正在使用charts库来创建这样的图形
我正在使用此代码填充图表。
以下代码中发送的数据是
[[1.6667000000000001,2.0],[1.6667000000000001,1.0],[1.6667000000000001,1.0],[2.3333000000000004,2.0],[1.3332999999999999,1.0],[1.6667000000000001,3.0],[1.3332999999999999,2.0],[1.3332999999999999,1.0],[ 2.3333000000000004,3.0],[2.0,2.0],[2.6666999999999996,3.0],[2.3333000000000004,1.0],[2.3333000000000004,2.0]]
func setChart(data:[[Double]]) {
barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [[BarChartDataEntry]] = []
for _ in 0..<data.first!.count{
dataEntries.append([])
}
for i in 0..<data.count {
for g in 0..<dataEntries.count{
dataEntries[g].append(BarChartDataEntry(x: Double(i), y: data[i][g]))
}
}
var dataSet = [BarChartDataSet]()
for i in 0..<dataEntries.count {
let some = BarChartDataSet(values: dataEntries[i], label: nil)
some.colors = ChartColorTemplates.colorful()
dataSet.append(some)
}
let chartData = BarChartData(dataSets: dataSet)
let groupSpace = 0.025
let barSpace = 0.4
let barWidth = 0.8
// (0.05 + 0.03) * 3 + 0.3 = 1.00 -> interval per "group"
//(groupSpace * barSpace) * n + groupSpace = 1
//()*13
let startYear = 0
chartData.barWidth = barWidth;
barChartView.xAxis.axisMinimum = Double(startYear)
let gg = chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace)
print("Groupspace: \(gg)")
// barChartView.xAxis.axisMaximum = Double(startYear) + gg * Double(groupCount)
barChartView.xAxis.axisMaximum = 25
barChartView.groupBars(fromX: Double(startYear), groupSpace: groupSpace, barSpace: barSpace)
//chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace)
barChartView.notifyDataSetChanged()
barChartView.data = chartData
//background color
barChartView.backgroundColor = UIColor(red: 189/255, green: 195/255, blue: 199/255, alpha: 1)
//chart animation
barChartView.animate(xAxisDuration: 1.5, yAxisDuration: 1.5, easingOption: .linear)
}
这无法正常工作,我有重叠的条。 X轴最大值不会自动创建。我不知道我在做什么错。任何人都可以告诉我我错了。
每组的条数为13,并且组数可以变化到50。
最佳答案
非常简单,您可以为barChartView
设置groupBars。这是不对的。
barChartView.groupBars(fromX: Double(startYear), groupSpace: groupSpace, barSpace: barSpace)
您必须为
chartData
设置groupBars。像下面chartData.groupBars(fromX: Double(startYear), groupSpace: groupSpace, barSpace: barSpace)