I try to build a stacked bar chart with chart framework (Swift) in my iOS project. All I can build at the moment is a grouped bar chart. Does someone know how to do this?
我尝试在iOS项目中使用图表框架(Swift)构建堆积条形图。我现在所能建立的只是一个分组的条形图。有人知道怎么做吗?
All I found is: Convert bar chart to a grouped bar chart with danielgindi/ios-charts and Swift
我发现的全部是:使用danielgindi / ios-charts和Swift将条形图转换为分组条形图
Here is my code:
这是我的代码:
@IBOutlet var barChartView: BarChartView!
var receivedDate1 : String = ""
var receivedDate2 : String = ""
var receivedDate3 : String = ""
var receivedDate4 : String = ""
var receivedDate5 : String = ""
var months: [String]!
override func viewDidLoad() {
super.viewDidLoad()
months = [receivedDate1, receivedDate2, receivedDate3, receivedDate4, receivedDate5]
let dominanz1 = [1.0, 2.0, 3.0, 4.0, 5.0]
let dominanz2 = [5.0, 4.0, 3.0, 2.0, 1.0]
setChartBarGroupDataSet(months, values: dominanz1, values2: dominanz2, sortIndex: 1)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setChartBarGroupDataSet(dataPoints: [String], values: [Double], values2: [Double],sortIndex:Int) {
var dataEntries: [BarChartDataEntry] = []
var dataEntries2: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(value: values2[i], xIndex: i)
dataEntries2.append(dataEntry)
}
let chartDataSet = BarChartDataSet(yVals: dataEntries, label: " ")
let chartDataSet2 = BarChartDataSet(yVals: dataEntries2, label: " ")
chartDataSet2.colors = [UIColor(red: 255/255, green: 70/255, blue: 108/255, alpha: 1)]
chartDataSet.colors = [UIColor(red: 49/255, green: 27/255, blue: 146/255, alpha: 1)]
let dataSets: [BarChartDataSet] = [chartDataSet,chartDataSet2]
let data = BarChartData(xVals: dataPoints, dataSets: dataSets)
barChartView.data = data
barChartView.descriptionText = ""
barChartView.rightAxis.drawGridLinesEnabled = false
barChartView.rightAxis.drawAxisLineEnabled = false
barChartView.rightAxis.drawLabelsEnabled = false
barChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .EaseInBounce)
}
The Framework I use is Charts 2.2.4 from cocoapods. This is what it currently looks like: grouped bar chart And this is how it should look like: stacked bar chart
我使用的框架是cocoapods的图表2.2.4。这就是它目前的样子:分组条形图这就是它应该是这样的:堆积条形图
2 个解决方案
#1
1
prepare BarchartDataEntry with two values in yAxis like
准备BarchartDataEntry,在yAxis中有两个值
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
let unitsBought = [10.0, 14.0, 60.0, 13.0, 2.0, 1.0, 5.0, 1.0, 12.0, 14.0, 15.0, 14.0]
let dataEntry = BarChartDataEntry(x: Double(i), yValues: [self.unitsSold[i],self.unitsBought[i]], label: "groupChart")
var dataEntries: [BarChartDataEntry] = []
for i in 0..<self.months.count {
let dataEntry = BarChartDataEntry(x: Double(i) , y: self.unitsSold[i])
dataEntries.append(dataEntry)
}
#2
0
Just released a new CocoaPod called CHRadarGraph. It's a circular bar graph that allows dense amount of data to be viewed in a smaller space.
刚刚发布了一款名为CHRadarGraph的新CocoaPod。它是一个圆形条形图,允许在较小的空间中查看大量数据。
Take a look and see if this works for you!
看一看,看看这是否适合您!
#1
1
prepare BarchartDataEntry with two values in yAxis like
准备BarchartDataEntry,在yAxis中有两个值
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
let unitsBought = [10.0, 14.0, 60.0, 13.0, 2.0, 1.0, 5.0, 1.0, 12.0, 14.0, 15.0, 14.0]
let dataEntry = BarChartDataEntry(x: Double(i), yValues: [self.unitsSold[i],self.unitsBought[i]], label: "groupChart")
var dataEntries: [BarChartDataEntry] = []
for i in 0..<self.months.count {
let dataEntry = BarChartDataEntry(x: Double(i) , y: self.unitsSold[i])
dataEntries.append(dataEntry)
}
#2
0
Just released a new CocoaPod called CHRadarGraph. It's a circular bar graph that allows dense amount of data to be viewed in a smaller space.
刚刚发布了一款名为CHRadarGraph的新CocoaPod。它是一个圆形条形图,允许在较小的空间中查看大量数据。
Take a look and see if this works for you!
看一看,看看这是否适合您!