-
-
Notifications
You must be signed in to change notification settings - Fork 6k
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 support for rounded corners for BarChart #3754
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -377,9 +377,9 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer | |
// draw the bar shadow before the values | ||
if dataProvider.isDrawBarShadowEnabled | ||
{ | ||
for j in stride(from: 0, to: buffer.rects.count, by: 1) | ||
for firstIndexInBar in stride(from: 0, to: buffer.rects.count, by: 1) | ||
{ | ||
let barRect = buffer.rects[j] | ||
let barRect = buffer.rects[firstIndexInBar] | ||
|
||
if (!viewPortHandler.isInBoundsLeft(barRect.origin.x + barRect.size.width)) | ||
{ | ||
|
@@ -402,53 +402,81 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer | |
{ | ||
context.setFillColor(dataSet.color(atIndex: 0).cgColor) | ||
} | ||
|
||
context.setStrokeColor(borderColor.cgColor) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it seems you are using But what if people disable |
||
context.setLineWidth(borderWidth) | ||
context.setLineCap(.square) | ||
|
||
// 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 stride(from: 0, to: buffer.rects.count, by: 1) | ||
for firstIndexInBar in stride(from: 0, to: buffer.rects.count, by: stackSize) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why stackSize here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello @panncherry |
||
{ | ||
let barRect = buffer.rects[j] | ||
context.saveGState() | ||
|
||
let lastIndexInBar = firstIndexInBar + stackSize - 1 | ||
|
||
|
||
if (!viewPortHandler.isInBoundsLeft(barRect.origin.x + barRect.size.width)) | ||
{ | ||
continue | ||
} | ||
let topRectInBar = findTopRectInBar(barRects: buffer.rects, | ||
firstIndexInBar: firstIndexInBar, | ||
lastIndexInBar: lastIndexInBar) | ||
|
||
let path = createBarPath(for: topRectInBar, roundedCorners: dataSet.roundedCorners) | ||
|
||
if (!viewPortHandler.isInBoundsRight(barRect.origin.x)) | ||
{ | ||
break | ||
} | ||
context.addPath(path.cgPath) | ||
context.clip() | ||
|
||
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) | ||
for index in firstIndexInBar...lastIndexInBar { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
let barRect = buffer.rects[index] | ||
|
||
if (!viewPortHandler.isInBoundsLeft(barRect.origin.x + barRect.size.width)) | ||
{ | ||
continue | ||
} | ||
|
||
if (!viewPortHandler.isInBoundsRight(barRect.origin.x)) | ||
{ | ||
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: index).cgColor) | ||
} | ||
|
||
context.addRect(barRect) | ||
context.fillPath() | ||
|
||
if drawBorder { | ||
context.stroke(barRect) | ||
} | ||
|
||
// Create and append the corresponding accessibility element to accessibilityOrderedElements | ||
if let chart = dataProvider as? BarChartView | ||
{ | ||
let element = createAccessibleElement(withIndex: index, | ||
container: chart, | ||
dataSet: dataSet, | ||
dataSetIndex: index, | ||
stackSize: stackSize) | ||
{ (element) in | ||
element.accessibilityFrame = barRect | ||
} | ||
|
||
accessibilityOrderedElements[index/stackSize].append(element) | ||
} | ||
|
||
} | ||
|
||
context.fill(barRect) | ||
context.restoreGState() | ||
|
||
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) | ||
context.addPath(path.cgPath) | ||
context.strokePath() | ||
} | ||
} | ||
|
||
|
@@ -804,12 +832,22 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer | |
y1 = e.y | ||
y2 = 0.0 | ||
} | ||
|
||
prepareBarHighlight(x: e.x, y1: e.positiveSum, y2: -e.negativeSum, barWidthHalf: barData.barWidth / 2.0, trans: trans, rect: &barRect) | ||
|
||
prepareBarHighlight(x: e.x, y1: y1, y2: y2, barWidthHalf: barData.barWidth / 2.0, trans: trans, rect: &barRect) | ||
|
||
setHighlightDrawPos(highlight: high, barRect: barRect) | ||
var highlightRect = CGRect() | ||
prepareBarHighlight(x: e.x, y1: y1, y2: y2, barWidthHalf: barData.barWidth / 2.0, trans: trans, rect: &highlightRect) | ||
setHighlightDrawPos(highlight: high, barRect: highlightRect) | ||
|
||
context.fill(barRect) | ||
let path = createBarPath(for: barRect, roundedCorners: set.roundedCorners) | ||
|
||
context.saveGState() | ||
|
||
context.addPath(path.cgPath) | ||
context.clip() | ||
context.fill(highlightRect) | ||
|
||
context.restoreGState() | ||
} | ||
} | ||
|
||
|
@@ -896,4 +934,32 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer | |
|
||
return element | ||
} | ||
|
||
private func findTopRectInBar(barRects: [CGRect], firstIndexInBar: Int, lastIndexInBar: Int) -> CGRect { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's put all |
||
var topRectInBar = barRects[firstIndexInBar] | ||
if barRects[lastIndexInBar].origin.y < topRectInBar.origin.y { | ||
topRectInBar = barRects[lastIndexInBar] | ||
} | ||
|
||
var height: CGFloat = 0 | ||
for index in firstIndexInBar...lastIndexInBar { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
height += barRects[index].height | ||
} | ||
|
||
topRectInBar.size.height = height | ||
|
||
return topRectInBar | ||
} | ||
|
||
/// Creates path for bar in rect with rounded corners | ||
internal func createBarPath(for rect: CGRect, roundedCorners: UIRectCorner) -> UIBezierPath { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
let cornerRadius = rect.width / 2.0 | ||
|
||
let path = UIBezierPath(roundedRect: rect, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @skarol I see a problem here when the rect is smaller than the cornerRadius, in this case you'll have a smashed rounded rectangle that looks bad(I haven't tested this code, but I worked in this feature in my fork, and at the start I had used the UIBezierPath like you with the result that I have commented, some screenshots at the end, apologies for the quality).
|
||
byRoundingCorners: roundedCorners, | ||
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)) | ||
|
||
return path | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -240,53 +240,80 @@ open class HorizontalBarChartRenderer: BarChartRenderer | |
{ | ||
context.setFillColor(dataSet.color(atIndex: 0).cgColor) | ||
} | ||
|
||
context.setStrokeColor(borderColor.cgColor) | ||
context.setLineWidth(borderWidth) | ||
context.setLineCap(.square) | ||
|
||
// In case the chart is stacked, we need to accomodate individual bars within accessibilityOrdereredElements | ||
// In case the chart is stacked, we need to accomodate individual bars within accessibilityOrderedElements | ||
let isStacked = dataSet.isStacked | ||
let stackSize = isStacked ? dataSet.stackSize : 1 | ||
|
||
for j in stride(from: 0, to: buffer.rects.count, by: 1) | ||
for firstIndexInBar in stride(from: 0, to: buffer.rects.count, by: stackSize) | ||
{ | ||
let barRect = buffer.rects[j] | ||
context.saveGState() | ||
|
||
if (!viewPortHandler.isInBoundsTop(barRect.origin.y + barRect.size.height)) | ||
{ | ||
break | ||
} | ||
let lastIndexInBar = firstIndexInBar + stackSize - 1 | ||
|
||
let leftRectInBar = findMostLeftRectInBar(barRects: buffer.rects, | ||
firstIndexInBar: firstIndexInBar, | ||
lastIndexInBar: lastIndexInBar) | ||
|
||
let path = createBarPath(for: leftRectInBar, roundedCorners: dataSet.roundedCorners) | ||
|
||
if (!viewPortHandler.isInBoundsBottom(barRect.origin.y)) | ||
{ | ||
continue | ||
} | ||
context.addPath(path.cgPath) | ||
context.clip() | ||
|
||
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) | ||
for index in firstIndexInBar...lastIndexInBar { | ||
|
||
let barRect = buffer.rects[index] | ||
|
||
if (!viewPortHandler.isInBoundsLeft(barRect.origin.x + barRect.size.width)) | ||
{ | ||
continue | ||
} | ||
|
||
if (!viewPortHandler.isInBoundsRight(barRect.origin.x)) | ||
{ | ||
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: index).cgColor) | ||
} | ||
|
||
context.addRect(barRect) | ||
context.fillPath() | ||
|
||
if drawBorder { | ||
context.stroke(barRect) | ||
} | ||
|
||
// Create and append the corresponding accessibility element to accessibilityOrderedElements | ||
if let chart = dataProvider as? BarChartView | ||
{ | ||
let element = createAccessibleElement(withIndex: index, | ||
container: chart, | ||
dataSet: dataSet, | ||
dataSetIndex: index, | ||
stackSize: stackSize) | ||
{ (element) in | ||
element.accessibilityFrame = barRect | ||
} | ||
|
||
accessibilityOrderedElements[index/stackSize].append(element) | ||
} | ||
|
||
} | ||
|
||
context.fill(barRect) | ||
|
||
context.restoreGState() | ||
if drawBorder | ||
{ | ||
context.setStrokeColor(borderColor.cgColor) | ||
context.setLineWidth(borderWidth) | ||
context.stroke(barRect) | ||
} | ||
|
||
// Create and append the corresponding accessibility element to accessibilityOrderedElements (see BarChartRenderer) | ||
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) | ||
context.addPath(path.cgPath) | ||
context.strokePath() | ||
} | ||
} | ||
|
||
|
@@ -628,4 +655,31 @@ open class HorizontalBarChartRenderer: BarChartRenderer | |
{ | ||
high.setDraw(x: barRect.midY, y: barRect.origin.x + barRect.size.width) | ||
} | ||
|
||
override internal func createBarPath(for rect: CGRect, roundedCorners: UIRectCorner) -> UIBezierPath { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could bar renderer and horizontal bar renderer share the same |
||
|
||
let cornerRadius = rect.height / 2.0 | ||
|
||
let path = UIBezierPath(roundedRect: rect, | ||
byRoundingCorners: roundedCorners, | ||
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)) | ||
|
||
return path | ||
} | ||
|
||
private func findMostLeftRectInBar(barRects: [CGRect], firstIndexInBar: Int, lastIndexInBar: Int) -> CGRect { | ||
var leftRectInBar = barRects[firstIndexInBar] | ||
if barRects[lastIndexInBar].origin.x < leftRectInBar.origin.x { | ||
leftRectInBar = barRects[lastIndexInBar] | ||
} | ||
|
||
var width: CGFloat = 0 | ||
for index in firstIndexInBar...lastIndexInBar { | ||
width += barRects[index].width | ||
} | ||
|
||
leftRectInBar.size.width = width | ||
|
||
return leftRectInBar | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -339,4 +339,12 @@ class BarChartTests: FBSnapshotTestCase | |
chart.notifyDataSetChanged() | ||
FBSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), tolerance: Snapshot.tolerance) | ||
} | ||
|
||
func testRoundedCorners() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not seeing updated images for testing rounded corners. |
||
let dataEntries = setupDefaultValuesDataEntries() | ||
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries) | ||
let chart = setupDefaultChart(dataSets: [dataSet]) | ||
dataSet.roundedCorners = [.allCorners] | ||
FBSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), tolerance: Snapshot.tolerance) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why change name here? seems nothing about
first index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have the same question. If anything the change should be: