Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for rounded bar chart and grid edges #5109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartData
/// the overall entry count, including counting each stack-value individually
private var _entryCountStacks = 0

/// the corner radius applied to each data set
public var cornerRadius: CGFloat = 0.0

/// Calculates the total number of entries this DataSet represents, including
/// stacks. All values belonging to a stack are calculated separately.
private func calcEntryCountIncludingStacks(entries: [BarChartDataEntry])
Expand Down
3 changes: 3 additions & 0 deletions Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ public protocol BarChartDataSetProtocol: BarLineScatterCandleBubbleChartDataSetP

/// array of labels used to describe the different values of the stacked bars
var stackLabels: [String] { get set }

/// the corner radius applied to each data set
var cornerRadius: CGFloat { get set }
}
8 changes: 7 additions & 1 deletion Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
context.setFillColor(dataSet.color(atIndex: j).cgColor)
}

context.fill(barRect)
setRoundedCorners(context: context, barRect: barRect, dataSet: dataSet as! BarChartDataSet)

if drawBorder
{
Expand All @@ -406,6 +406,12 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
}
}

func setRoundedCorners(context: CGContext, barRect: CGRect, dataSet: BarChartDataSet) {
let bezierPath = UIBezierPath(roundedRect: barRect, cornerRadius: barRect.width * dataSet.cornerRadius)
context.addPath(bezierPath.cgPath)
context.drawPath(using: .fill)
}

open func prepareBarHighlight(
x: Double,
y1: Double,
Expand Down
6 changes: 4 additions & 2 deletions Source/Charts/Renderers/HorizontalBarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ open class HorizontalBarChartRenderer: BarChartRenderer
_barShadowRectBuffer.size.width = viewPortHandler.contentWidth

context.setFillColor(dataSet.barShadowColor.cgColor)
context.fill(_barShadowRectBuffer)
// to round the edges for each bar's shadow
setRoundedCorners(context: context, barRect: _barShadowRectBuffer, dataSet: dataSet as! BarChartDataSet)
}
}

Expand Down Expand Up @@ -265,7 +266,8 @@ open class HorizontalBarChartRenderer: BarChartRenderer
context.setFillColor(dataSet.color(atIndex: j).cgColor)
}

context.fill(barRect)
// to round the edges for each bar
setRoundedCorners(context: context, barRect: barRect, dataSet: dataSet as! BarChartDataSet)

if drawBorder
{
Expand Down
5 changes: 4 additions & 1 deletion Source/Charts/Renderers/XAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,10 @@ open class XAxisRenderer: NSObject, AxisRenderer
context.saveGState()
defer { context.restoreGState() }

context.clip(to: self.gridClippingRect)
// clip when line cap is not round to have rounded edges for grid
if axis.gridLineCap != .round {
context.clip(to: self.gridClippingRect)
}

context.setShouldAntialias(axis.gridAntialiasEnabled)
context.setStrokeColor(axis.gridColor.cgColor)
Expand Down