Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounded bars in ios Charts

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

like image 833
lifeisfoo Avatar asked Jun 20 '16 10:06

lifeisfoo


1 Answers

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)
like image 121
keyv Avatar answered Sep 19 '22 16:09

keyv