Reputation: 1304
I am using CHARTS library in swift. https://github.com/danielgindi/Charts
I got suceess in creating Pie chart and bar chart. Now I want to create bar chart with one "x" value and 2 "y" values.
I created the bar chart with two different values.
But I am not able to set the colours for them
I passed the colour array to delegate method setColors
but it takes the last colour from the array.
Can any one please guide me for having different colours on bar chart.
Here is my code to call BarChart library
months = ["HR","Operations","iOS","Android","PHP","Support","Testing","Designer"]
let unitsSold = [10.0,20.0,30.0,20.0,20.0,10.0,15.0,20.0]
let unitsSold1 = [20.0,30.0,50.0,40.0,30.0,20.0,45.0,50.0]
let barChartClass = self.storyboard!.instantiateViewControllerWithIdentifier("BarChartViewControllerID") as! BarChartViewController
self.addChildViewController(barChartClass)
barChartClass.view.frame = CGRectMake(20, 20, 740, 900)
barChartClass.barChartView.doubleTapToZoomEnabled = false
barChartClass.barChartView.setScaleEnabled(false)
self.view.addSubview(barChartClass.view)
barChartClass.barChartView.legend.enabled = false
barChartClass.setChartBarGroupDataSet(months, values: unitsSold, values2: unitsSold1, sortIndex: 0)
And this is the method call for setChartBarGroupDataSet
func setChartBarGroupDataSet(dataPoints: [String], values: [Double], values2: [Double],sortIndex:Int)
{
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count
{
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
let dataEntry1 = BarChartDataEntry(value: values2[i], xIndex: i)
dataEntries.append(dataEntry)
dataEntries.append(dataEntry1)
}
let chartDataSet = BarChartDataSet(yVals: dataEntries, label: " ")
let COLOR_SET : [UIColor]!
COLOR_SET = [UIColor.redColor(),UIColor.greenColor()]
chartDataSet.setColors(COLOR_SET, alpha: 1.0)
let dataSets: [BarChartDataSet] = [chartDataSet]
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)
}
What I want is something like this
Thanks in advance.
Upvotes: 3
Views: 6187
Reputation: 3496
This is a stacked bar chart. In Charts' demos, you can see a stacked bar chart.
Basically, each BarChartDataEntry
received either one value, or an array of values. Pass in an array of values - and you are good to go!
let entry = BarChartDataEntry(values: [4.6, 3.8], xIndex: 0)
dataset.colors = [UIColor.cyanColor(), UIColor.greenColor()]
dataset.stackLabels = ["Inactive inventory", "Active inventory"]
Answered in the duplicate issue too: https://github.com/danielgindi/Charts/issues/1324
Upvotes: 5