我使用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。
答案 0 :(得分:0)
非常简单,您要为barChartView
设置groupBars。错了。
barChartView.groupBars(fromX: Double(startYear), groupSpace: groupSpace, barSpace: barSpace)
您必须为chartData
设置groupBars。如下所示
chartData.groupBars(fromX: Double(startYear), groupSpace: groupSpace, barSpace: barSpace)