[英]How to create a grouped bar chart in Swift?
I am using Charts library to display charts in my app. 我正在使用图表库在我的应用程序中显示图表。 I have a bar chart in my app that used to work fine in Swift 2.3 and Charts version 2.2.5. 我的应用程序中有一个条形图,该条形图过去在Swift 2.3和Charts 2.2.5版中都能正常工作。 However, I recently updated my app to Swift 4.1 and Charts 3.1.0, but breaking changes in Charts library break the functionality in my app. 但是,我最近将应用程序更新为Swift 4.1和Charts 3.1.0,但是Charts库中的重大更改破坏了我应用程序的功能。 How do I properly migrate this block of code to work in new versions? 如何正确迁移此代码块以在新版本中工作?
This is the example chart that I am using: 这是我正在使用的示例图表:
And this is the JSON data that creates this chart: 这是创建此图表的JSON数据:
[{"Items":[{"X":"01.03.2018","Value":10000},{"X":"02.03.2018","Value":2500}],"Name":"Group 1"},{"Items":[{"X":"01.03.2018","Value":5000}],"Name":"Group 2"}]
This is the code that I used in Swift 2.3 that worked fine: 这是我在Swift 2.3中很好用的代码:
var xVals = [String]()
var datasets = [BarChartDataSet]()
for i in 0..<json.count{
var entries = [BarChartDataEntry]()
for j in 0..<json[i]["Items"].count{
if i == 0{
xVals.append(json[i]["Items"][j]["X"].stringValue)
}
if json[i]["Items"][j]["Value"].doubleValue != 0.0{
let entry = BarChartDataEntry(value: json[i]["Items"][j]["Value"].doubleValue, xIndex: j)
entries.append(entry)
}
}
let dataset = BarChartDataSet(yVals: entries, label: json[i]["Name"].stringValue)
datasets.append(dataset)
}
let data = BarChartData(xVals: xVals, dataSets: datasets)
barChart.data = data
This is how it looks after migration: 迁移后的外观如下:
There are 2 problems with this: 这有两个问题:
Bars are stacked instead of grouped. 条堆叠而不是分组。
xAxis does show indexes instead of dates. xAxis确实显示索引而不是日期。
This is the new Swift 4.1 code: 这是新的Swift 4.1代码:
var xVals = [String]()
var datasets = [BarChartDataSet]()
for i in 0..<json.count{
var entries = [BarChartDataEntry]()
for j in 0..<json[i]["Items"].count{
if i == 0{
xVals.append(json[i]["Items"][j]["X"].stringValue)
}
if json[i]["Items"][j]["Value"].doubleValue != 0.0{
let entry = BarChartDataEntry(x: Double(j), y: json[i]["Items"][j]["Value"].doubleValue)
entries.append(entry)
}
}
let dataset = BarChartDataSet(values: entries, label: json[i]["Name"].stringValue)
datasets.append(dataset)
}
let data = BarChartData(dataSets: datasets)
barChart.data = data
So how do I solve those 2 problems? 那么我该如何解决这两个问题呢?
[ Swift 4 ]You need to use groupBars and valueFormatter form BarChart API. [ Swift 4 ]您需要使用groupBars和valueFormatter形式的BarChart API。
This is just an example of code, you need to fine tuning as per your requirement. 这只是代码示例,您需要根据需要进行微调。
[ Edit 1 ] [ 编辑1 ]
Formula : (0.2 + 0.03) * countGourp + 0.08 = 1.00 -> interval per "group" 公式 :(0.2 + 0.03)* countGourp + 0.08 = 1.00->每个“组”的间隔
let data = BarChartData(dataSets: datasets)
// (0.2 + 0.03) * 2 + 0.54 = 1.00
let groupSpace = 0.54
let barSpace = 0.03
let barWidth = 0.2
data.barWidth = barWidth
barView.xAxis.axisMinimum = Double(0)
barView.xAxis.axisMaximum = Double(0) + data.groupWidth(groupSpace: groupSpace, barSpace: barSpace) * Double(2) // group count : 2
data.groupBars(fromX: Double(0), groupSpace: groupSpace, barSpace: barSpace)
barView.data = data
let x_Axis = barView.xAxis
x_Axis.valueFormatter = IndexAxisValueFormatter(values:xVals)
x_Axis.centerAxisLabelsEnabled = true
x_Axis.granularity = 1
barView.xAxis.labelPosition = .top
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.