Skip to content

Commit c14ed45

Browse files
author
Oleg
committed
Add logic to make cornerRadius on BarChart
1 parent 29e4f58 commit c14ed45

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartData
5151
/// the overall entry count, including counting each stack-value individually
5252
private var _entryCountStacks = 0
5353

54+
/// the corner radius applied to each data set
55+
public var cornerRadius: CGFloat = 0.0
56+
57+
/// array of corners to be rounded
58+
open var roundedCorners: UIRectCorner = []
59+
5460
/// Calculates the total number of entries this DataSet represents, including
5561
/// stacks. All values belonging to a stack are calculated separately.
5662
private func calcEntryCountIncludingStacks(entries: [BarChartDataEntry])

Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
import Foundation
1313
import CoreGraphics
1414

15+
#if canImport(UIKit)
16+
import UIKit
17+
#endif
18+
1519
@objc
1620
public protocol BarChartDataSetProtocol: BarLineScatterCandleBubbleChartDataSetProtocol
1721
{
@@ -39,4 +43,10 @@ public protocol BarChartDataSetProtocol: BarLineScatterCandleBubbleChartDataSetP
3943

4044
/// array of labels used to describe the different values of the stacked bars
4145
var stackLabels: [String] { get set }
46+
47+
/// the corner radius applied to each data set
48+
var cornerRadius: CGFloat { get set }
49+
50+
/// array of corners to be rounded
51+
var roundedCorners: UIRectCorner { get set }
4252
}

Source/Charts/Renderers/BarChartRenderer.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,11 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
351351
guard viewPortHandler.isInBoundsRight(barRect.origin.x) else { break }
352352

353353
context.setFillColor(dataSet.barShadowColor.cgColor)
354-
context.fill(barRect)
354+
355+
let bezierPath = UIBezierPath(roundedRect: barRect, byRoundingCorners: dataSet.roundedCorners,
356+
cornerRadii: .init(width: dataSet.cornerRadius, height: dataSet.cornerRadius))
357+
context.addPath(bezierPath.cgPath)
358+
context.drawPath(using: .fill)
355359
}
356360
}
357361

@@ -379,7 +383,10 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
379383
context.setFillColor(dataSet.color(atIndex: j).cgColor)
380384
}
381385

382-
context.fill(barRect)
386+
let bezierPath = UIBezierPath(roundedRect: barRect, byRoundingCorners: dataSet.roundedCorners,
387+
cornerRadii: .init(width: dataSet.cornerRadius, height: dataSet.cornerRadius))
388+
context.addPath(bezierPath.cgPath)
389+
context.drawPath(using: .fill)
383390

384391
if drawBorder
385392
{
@@ -744,7 +751,10 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
744751

745752
setHighlightDrawPos(highlight: high, barRect: barRect)
746753

747-
context.fill(barRect)
754+
let bezierPath = UIBezierPath(roundedRect: barRect, byRoundingCorners: set.roundedCorners,
755+
cornerRadii: .init(width: set.cornerRadius, height: set.cornerRadius))
756+
context.addPath(bezierPath.cgPath)
757+
context.drawPath(using: .fill)
748758
}
749759
}
750760
}

Source/Charts/Renderers/HorizontalBarChartRenderer.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ open class HorizontalBarChartRenderer: BarChartRenderer
228228
_barShadowRectBuffer.size.width = viewPortHandler.contentWidth
229229

230230
context.setFillColor(dataSet.barShadowColor.cgColor)
231-
context.fill(_barShadowRectBuffer)
231+
232+
let bezierPath = UIBezierPath(roundedRect: _barShadowRectBuffer, byRoundingCorners: dataSet.roundedCorners,
233+
cornerRadii: .init(width: dataSet.cornerRadius, height: dataSet.cornerRadius))
234+
context.addPath(bezierPath.cgPath)
235+
context.drawPath(using: .fill)
232236
}
233237
}
234238

@@ -265,7 +269,10 @@ open class HorizontalBarChartRenderer: BarChartRenderer
265269
context.setFillColor(dataSet.color(atIndex: j).cgColor)
266270
}
267271

268-
context.fill(barRect)
272+
let bezierPath = UIBezierPath(roundedRect: barRect, byRoundingCorners: dataSet.roundedCorners,
273+
cornerRadii: .init(width: dataSet.cornerRadius, height: dataSet.cornerRadius))
274+
context.addPath(bezierPath.cgPath)
275+
context.drawPath(using: .fill)
269276

270277
if drawBorder
271278
{

0 commit comments

Comments
 (0)