Reputation: 749
Is there a setting in the BarChartView to enable rounded corners for the bars?
I want to achieve a similar effect to this other library iosbarchart
Upvotes: 4
Views: 5755
Reputation: 4127
This is a sample rounded bar chart using UIView in swift. In your .swift file, add the following code.
@IBOutlet weak var barView: UIView!
var BAR_WIDTH_CONSTANT: CGFloat = 9
var BAR_DISTANCE: CGFloat = 14 // distance between your bars
var BAR_MAX_HEIGHT: CGFloat = 50 // maximum height of a bar
let MAX_VALUE: CGFloat = 10 //maximum value of a bar
let MIN_VALUE: CGFloat = 0 // minimum value of a bar
Then add the following code in your viewDidLoad().
var xPos: CGFloat = 0
var yPos: CGFloat = 0
let values: [Int] = [1, 2, 3, 4, 5, 6, 7] //values of a Bar chart
for i in 0..< values.count {
// draw bar
let barHeight = getBarHeight(height: CGFloat(values[i]), maxHeight: BAR_MAX_HEIGHT, maxValue: MAX_VALUE, minValue: MIN_VALUE)
yPos = BAR_MAX_HEIGHT - barHeight
let frame = CGRect(x: xPos, y: yPos, width: BAR_WIDTH_CONSTANT, height: barHeight)
let bar = UIView(frame: frame)
bar.layer.cornerRadius = BAR_WIDTH_CONSTANT < barHeight ? BAR_WIDTH_CONSTANT / 2 : barHeight / 2
bar.layer.masksToBounds = true
self.barView.addSubview(bar)
xPos = xPos + BAR_WIDTH_CONSTANT + BAR_DISTANCE
// bar drawing complete
}
func getBarHeight(height: CGFloat, maxHeight: CGFloat, maxValue: CGFloat, minValue: CGFloat) -> CGFloat {
let barHeight = (maxHeight * height ) / (maxValue - minValue)
return barHeight
}
Hope it will help.
Upvotes: 0
Reputation: 42
i did a fork of ios-charts to roughly accomplish this: https://github.com/mcconkiee/ios-charts
var yVals: [BarChartDataEntry] = []
var xVals: [String] = []
var dataCount = sections.count
for i in 0...(dataCount - 1) {
var dataObjectInstance = sections[i] as SomeCustomDataObject
var entry = BarChartDataEntry(value: dataObjectInstance.numeric!, xIndex: i)
entry.rounded = true //make the end caps round!
if let tintClr = dataObjectInstance.tint as String?{
if let clr = UIColor(hexString: tintClr) as UIColor?{
entry.tint = clr
}
}
yVals.append(entry)
var time = i % 12
if(time == 0) {
time = 12
}
if let objVal = dataObjectInstance.objectValue as String?{
xVals.append(objVal)
}
}
Upvotes: 1