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

Add RoundedCornersBarChatRenderer which provides the ability to draw bar charts with rounded corners #4494

Open
wants to merge 8 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
4 changes: 4 additions & 0 deletions Charts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
EB56849433A76B08606B73EB /* ScatterChartDataSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB1DD1A0F64266A10EE94194 /* ScatterChartDataSet.swift */; };
ECE7EAE7179A7F57CE9BBD8F /* Legend.swift in Sources */ = {isa = PBXBuildFile; fileRef = E64A75540C627E09080B402A /* Legend.swift */; };
ECECC58CEF03B1718F8267E8 /* AxisRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = C75935E899183DDFA181E2CC /* AxisRenderer.swift */; };
EE4DF1582552D0A7001C80BA /* RoundedCornersBarChatRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE4DF1562552D0A7001C80BA /* RoundedCornersBarChatRenderer.swift */; };
F100D68395F169B93590FA96 /* HorizontalBarChartRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 539382766378B702660FDFB2 /* HorizontalBarChartRenderer.swift */; };
F103D90FC5DEEA0D7BB4407E /* ChevronUpShapeRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA70259ED16FF80D8EEB0F94 /* ChevronUpShapeRenderer.swift */; };
F37B07008B8AE7F3909FFB9C /* DataRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0216EDC6A1FE272F4EB19FCF /* DataRenderer.swift */; };
Expand Down Expand Up @@ -315,6 +316,7 @@
EAE417AAA0FCA0DD00E77489 /* YAxisRendererHorizontalBarChart.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = YAxisRendererHorizontalBarChart.swift; path = Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift; sourceTree = "<group>"; };
ECE1B1623D3AF69CECAE8562 /* CircleShapeRenderer.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = CircleShapeRenderer.swift; path = Source/Charts/Renderers/Scatter/CircleShapeRenderer.swift; sourceTree = "<group>"; };
EDEAF554FD0D68EA4C0E7E49 /* BubbleChartDataProvider.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = BubbleChartDataProvider.swift; path = Source/Charts/Interfaces/BubbleChartDataProvider.swift; sourceTree = "<group>"; };
EE4DF1562552D0A7001C80BA /* RoundedCornersBarChatRenderer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RoundedCornersBarChatRenderer.swift; path = Source/Charts/Renderers/RoundedCornersBarChatRenderer.swift; sourceTree = "<group>"; };
F22750328058DEC2F019646F /* ChartDataEntry.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ChartDataEntry.swift; path = Source/Charts/Data/Implementations/Standard/ChartDataEntry.swift; sourceTree = "<group>"; };
F368CF209744D8F3B85B1028 /* RadarHighlighter.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = RadarHighlighter.swift; path = Source/Charts/Highlight/RadarHighlighter.swift; sourceTree = "<group>"; };
F3788EC55EF908B0805D7C2F /* BubbleChartDataSetProtocol.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = BubbleChartDataSetProtocol.swift; path = Source/Charts/Data/Interfaces/BubbleChartDataSetProtocol.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -407,6 +409,7 @@
2227EA581FF1F224007D98D9 /* ChartRenderers */ = {
isa = PBXGroup;
children = (
EE4DF1562552D0A7001C80BA /* RoundedCornersBarChatRenderer.swift */,
0216EDC6A1FE272F4EB19FCF /* DataRenderer.swift */,
75F279974FE650E57A061B09 /* BarChartRenderer.swift */,
5B1C588E9DF6FFD56D7ADF8E /* BarLineScatterCandleBubbleRenderer.swift */,
Expand Down Expand Up @@ -886,6 +889,7 @@
2FBA7E982EB57932B9F3E9B5 /* YAxis.swift in Sources */,
E50D291A6B6E69BF0B56A67C /* ChartBaseDataSet.swift in Sources */,
3395682A1E27756651FF6F4D /* BarChartData.swift in Sources */,
EE4DF1582552D0A7001C80BA /* RoundedCornersBarChatRenderer.swift in Sources */,
45C459FA25DFCBE62FA6A06C /* BarChartDataEntry.swift in Sources */,
17E994DA88777AA1D8CCFC58 /* BarChartDataSet.swift in Sources */,
C9AA360A2355F01F00C97D93 /* Platform+Color.swift in Sources */,
Expand Down
218 changes: 0 additions & 218 deletions Source/Charts/Renderers/AxisRendererBase.swift

This file was deleted.

124 changes: 124 additions & 0 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,130 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer

private var _barShadowRectBuffer: CGRect = CGRect()

#if os(iOS)
@objc open func drawDataSet(context: CGContext, dataSet: BarChartDataSetProtocol, index: Int, cornerRadius: CGFloat, roundedCorners: UIRectCorner)
{
guard let dataProvider = dataProvider else { return }

let trans = dataProvider.getTransformer(forAxis: dataSet.axisDependency)

prepareBuffer(dataSet: dataSet, index: index)
trans.rectValuesToPixel(&_buffers[index])

let borderWidth = dataSet.barBorderWidth
let borderColor = dataSet.barBorderColor
let drawBorder = borderWidth > 0.0

context.saveGState()
defer { context.restoreGState() }

// draw the bar shadow before the values
if dataProvider.isDrawBarShadowEnabled
{
guard let barData = dataProvider.barData else { return }

let barWidth = barData.barWidth
let barWidthHalf = barWidth / 2.0
var x: Double = 0.0

let range = (0..<dataSet.entryCount).clamped(to: 0..<Int(ceil(Double(dataSet.entryCount) * animator.phaseX)))
for i in range
{
guard let e = dataSet.entryForIndex(i) as? BarChartDataEntry else { continue }

x = e.x

_barShadowRectBuffer.origin.x = CGFloat(x - barWidthHalf)
_barShadowRectBuffer.size.width = CGFloat(barWidth)

trans.rectValueToPixel(&_barShadowRectBuffer)

guard viewPortHandler.isInBoundsLeft(_barShadowRectBuffer.origin.x + _barShadowRectBuffer.size.width) else { continue }

guard viewPortHandler.isInBoundsRight(_barShadowRectBuffer.origin.x) else { break }

_barShadowRectBuffer.origin.y = viewPortHandler.contentTop
_barShadowRectBuffer.size.height = viewPortHandler.contentHeight

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

let buffer = _buffers[index]

// draw the bar shadow before the values
if dataProvider.isDrawBarShadowEnabled
{
for barRect in buffer where viewPortHandler.isInBoundsLeft(barRect.origin.x + barRect.size.width)
{
guard viewPortHandler.isInBoundsRight(barRect.origin.x) else { break }

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

let isSingleColor = dataSet.colors.count == 1

if isSingleColor
{
context.setFillColor(dataSet.color(atIndex: 0).cgColor)
}

// In case the chart is stacked, we need to accomodate individual bars within accessibilityOrdereredElements
let isStacked = dataSet.isStacked
let stackSize = isStacked ? dataSet.stackSize : 1

for j in buffer.indices
{
let barRect = buffer[j]

let maskPath = UIBezierPath(roundedRect: barRect,
byRoundingCorners: roundedCorners,
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)
)

let cgPath = maskPath.cgPath
context.addPath(cgPath)
context.setFillColor(dataSet.color(atIndex: j).cgColor)

guard viewPortHandler.isInBoundsLeft(barRect.origin.x + barRect.size.width) else { continue }
guard viewPortHandler.isInBoundsRight(barRect.origin.x) else { break }

if !isSingleColor
{
// Set the color for the currently drawn value. If the index is out of bounds, reuse colors.
context.setFillColor(dataSet.color(atIndex: j).cgColor)
}

context.fillPath()

if drawBorder
{
context.setStrokeColor(borderColor.cgColor)
context.setLineWidth(borderWidth)
context.stroke(barRect)
}

// Create and append the corresponding accessibility element to accessibilityOrderedElements
if let chart = dataProvider as? BarChartView
{
let element = createAccessibleElement(withIndex: j,
container: chart,
dataSet: dataSet,
dataSetIndex: index,
stackSize: stackSize)
{ (element) in
element.accessibilityFrame = barRect
}

accessibilityOrderedElements[j/stackSize].append(element)
}
}
}
#endif

@objc open func drawDataSet(context: CGContext, dataSet: BarChartDataSetProtocol, index: Int)
{
guard let dataProvider = dataProvider else { return }
Expand Down
Loading