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

Posibility to add Corner Radius for rounded bars in bar chart #5163

Open
wants to merge 3 commits 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
28 changes: 28 additions & 0 deletions Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ 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

/// array of corners to be rounded
open var roundedCorners: UIRectCorner = [] {
didSet {
var invertedCorners: UIRectCorner = []
if roundedCorners.contains(.topLeft) {
invertedCorners.insert(.bottomLeft)
}
if roundedCorners.contains(.topRight) {
invertedCorners.insert(.bottomRight)
}
if roundedCorners.contains(.bottomLeft) {
invertedCorners.insert(.topLeft)
}
if roundedCorners.contains(.bottomRight) {
invertedCorners.insert(.topRight)
}
if roundedCorners.contains(.allCorners) {
invertedCorners.insert(.allCorners)
}
roundedCornersInverted = invertedCorners
}
}

open private(set) var roundedCornersInverted: UIRectCorner = []

/// 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
13 changes: 13 additions & 0 deletions Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import Foundation
import CoreGraphics

#if canImport(UIKit)
import UIKit
#endif

@objc
public protocol BarChartDataSetProtocol: BarLineScatterCandleBubbleChartDataSetProtocol
{
Expand Down Expand Up @@ -39,4 +43,13 @@ 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 }

/// array of corners to be rounded
var roundedCorners: UIRectCorner { get set }

/// array of corners to be rounded
var roundedCornersInverted: UIRectCorner { get }
}
31 changes: 28 additions & 3 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,17 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
guard viewPortHandler.isInBoundsRight(barRect.origin.x) else { break }

context.setFillColor(dataSet.barShadowColor.cgColor)
context.fill(barRect)

var roundedCorners = dataSet.roundedCorners
if let i = buffer.firstIndex(of: barRect),
let entry = dataSet.entryForIndex(i),
entry.y < 0 {
roundedCorners = dataSet.roundedCornersInverted
}
let bezierPath = UIBezierPath(roundedRect: barRect, byRoundingCorners: roundedCorners,
cornerRadii: .init(width: dataSet.cornerRadius, height: dataSet.cornerRadius))
context.addPath(bezierPath.cgPath)
context.drawPath(using: .fill)
}
}

Expand Down Expand Up @@ -379,7 +389,15 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
context.setFillColor(dataSet.color(atIndex: j).cgColor)
}

context.fill(barRect)
var roundedCorners = dataSet.roundedCorners
if let entry = dataSet.entryForIndex(j),
entry.y < 0 {
roundedCorners = dataSet.roundedCornersInverted
}
let bezierPath = UIBezierPath(roundedRect: barRect, byRoundingCorners: roundedCorners,
cornerRadii: .init(width: dataSet.cornerRadius, height: dataSet.cornerRadius))
context.addPath(bezierPath.cgPath)
context.drawPath(using: .fill)

if drawBorder
{
Expand Down Expand Up @@ -744,7 +762,14 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer

setHighlightDrawPos(highlight: high, barRect: barRect)

context.fill(barRect)
var roundedCorners = set.roundedCorners
if e.y < 0 {
roundedCorners = set.roundedCornersInverted
}
let bezierPath = UIBezierPath(roundedRect: barRect, byRoundingCorners: roundedCorners,
cornerRadii: .init(width: set.cornerRadius, height: set.cornerRadius))
context.addPath(bezierPath.cgPath)
context.drawPath(using: .fill)
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions Source/Charts/Renderers/HorizontalBarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,15 @@ open class HorizontalBarChartRenderer: BarChartRenderer
_barShadowRectBuffer.size.width = viewPortHandler.contentWidth

context.setFillColor(dataSet.barShadowColor.cgColor)
context.fill(_barShadowRectBuffer)

var roundedCorners = dataSet.roundedCorners
if e.x < 0 {
roundedCorners = dataSet.roundedCornersInverted
}
let bezierPath = UIBezierPath(roundedRect: _barShadowRectBuffer, byRoundingCorners: roundedCorners,
cornerRadii: .init(width: dataSet.cornerRadius, height: dataSet.cornerRadius))
context.addPath(bezierPath.cgPath)
context.drawPath(using: .fill)
}
}

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

context.fill(barRect)
var roundedCorners = dataSet.roundedCorners
if let entry = dataSet.entryForIndex(j),
entry.x < 0 {
roundedCorners = dataSet.roundedCornersInverted
}
let bezierPath = UIBezierPath(roundedRect: barRect, byRoundingCorners: roundedCorners,
cornerRadii: .init(width: dataSet.cornerRadius, height: dataSet.cornerRadius))
context.addPath(bezierPath.cgPath)
context.drawPath(using: .fill)

if drawBorder
{
Expand Down