Rounded bars in ios Charts

lifeisfoo picture lifeisfoo · Jun 20, 2016 · Viewed 8.9k times · Source

I've created a BarChart using BarChartView from ios-Charts but I can't figure out howto add rounded corners to bars.

This is the code that I'm using:

let barChart: BarChartView
//...
var xVals = [String]()
var yVals = [BarChartDataEntry]()
//...

let set1 = BarChartDataSet(yVals: yVals, label: "Label")

set1.drawValuesEnabled = false
set1.highlightLineWidth = 3
set1.colors = [UIColor.whiteColor()]

barChart.data = BarChartData(xVals: xVals, dataSet: set1)

I've looked for a property like set1.barCornerRadius to set, but I didn't find anything.

Here is what I have:

squared bar

Here is what I need:

rounded bar

Answer

keyv picture keyv · Jun 21, 2016

In BarChartRenderer.swift you can to modify open func drawDataSet(context: CGContext, dataSet: IBarChartDataSet, index: Int)

Then there's if-statement for stacked and nonstacked bars, choose your case and remove:

context.fill(barRect)

and instead of it add:

let bezierPath = UIBezierPath(roundedRect: barRect, cornerRadius: %YOUR_CORNER_RADIUS%)
context.addPath(bezierPath.cgPath)

context.drawPath(using: .fill)