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

improvements in barRect height calculation #3650

Merged
merged 24 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e2fa55b
fixed barRectCaculation
potato04 Sep 19, 2018
ed9d70d
fixed offset calculation
potato04 Sep 27, 2018
6cbecb8
Fix the mess caused by the setting the min&min value of the y-axis b…
potato04 Sep 27, 2018
526a73a
Fix the mess caused by the setting the min&min value of the y-axis by…
potato04 Sep 27, 2018
4548474
Revert "Fix the mess caused by the setting the min&min value of the y…
potato04 Sep 27, 2018
59d6986
Fix the mess caused by the setting the min&min value of the y-axis by…
potato04 Sep 27, 2018
c774d20
update offset calculation
potato04 Sep 28, 2018
1b6cc38
update code style
potato04 Sep 28, 2018
daff645
update code style
potato04 Sep 28, 2018
a77a358
update offset calculation
potato04 Sep 28, 2018
b67c251
keep barRect calculation untouched
liuxuan30 Sep 29, 2018
9a9edcb
After the correction of min and max , they should be assigned back t…
potato04 Oct 6, 2018
bf86f2c
add demo for bar chart unit test
potato04 Oct 6, 2018
f69c6ee
update unit test
potato04 Oct 7, 2018
ce578a0
revert last commit
potato04 Oct 9, 2018
afac36e
update unit test
potato04 Oct 9, 2018
b57cd0b
make sure max is greater than min &
potato04 Oct 9, 2018
0e7ff42
add new UT and fix some issues.
liuxuan30 Oct 11, 2018
104c0c4
update tvOS images
liuxuan30 Oct 11, 2018
e6098f3
update tolerance to 1% & more swift-y
liuxuan30 Oct 16, 2018
76b1f6f
update tvOS images, removing "Description Label", (not rendered anymore)
liuxuan30 Oct 16, 2018
d5d9abd
update iOS test images, "Description Label" no longer rendered.
liuxuan30 Oct 16, 2018
8da5272
fixed offset calculation in some cases.
potato04 Oct 19, 2018
8ae48b9
Update Source/Charts/Renderers/BarChartRenderer.swift
jjatie Oct 22, 2018
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
17 changes: 17 additions & 0 deletions Source/Charts/Components/YAxis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,23 @@ open class YAxis: AxisBase
var min = _customAxisMin ? _axisMinimum : dataMin
var max = _customAxisMax ? _axisMaximum : dataMax

// Make sure max is greater than min
// Discussion: https://github.com/danielgindi/Charts/pull/3650#discussion_r221409991
if min > max
{
switch(_customAxisMax, _customAxisMin)
{
case(true, true):
(min, max) = (max, min)
case(true, false):
min = max < 0 ? max * 1.5 : max * 0.5
case(false, true):
max = min < 0 ? min * 0.5 : min * 1.5
case(false, false):
break
}
}

// temporary range (before calculations)
let range = abs(max - min)

Expand Down
54 changes: 33 additions & 21 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,32 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
var barRect = CGRect()
var x: Double
var y: Double



// When drawing each bar, the renderer actually draws each bar from 0 to the required value.
// This drawn bar is then clipped to the visible chart rect in BarLineChartViewBase's draw(rect:) using clipDataToContent.
// While this works fine when calculating the bar rects for drawing, it causes the accessibilityFrames to be oversized in some cases.
// This offset attempts to undo that unnecessary drawing when calculating barRects
// This offset calculation is not necessary when bars won't be clipped (when y-axis minimum <= 0 and y-axis maximum >= 0)
var heightOffset: CGFloat = 0.0
var originYOffset: CGFloat = 0.0
if let offsetView = dataProvider as? BarChartView
{
let offsetAxis = dataSet.axisDependency == .left ? offsetView.leftAxis : offsetView.rightAxis
if offsetAxis.axisMinimum > 0
{
heightOffset = CGFloat(offsetAxis.axisMinimum)
}
if offsetAxis.axisMaximum < 0
{
originYOffset = CGFloat(offsetAxis.axisMaximum)
}
if isInverted
potato04 marked this conversation as resolved.
Show resolved Hide resolved
{
(heightOffset, originYOffset) = (originYOffset, heightOffset)
}
}

for i in stride(from: 0, to: min(Int(ceil(Double(dataSet.entryCount) * animator.phaseX)), dataSet.entryCount), by: 1)
{
Expand All @@ -125,12 +151,16 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
var top = isInverted
? (y <= 0.0 ? CGFloat(y) : 0)
: (y >= 0.0 ? CGFloat(y) : 0)
top += originYOffset

var bottom = isInverted
? (y >= 0.0 ? CGFloat(y) : 0)
: (y <= 0.0 ? CGFloat(y) : 0)
bottom += heightOffset

// multiply the height of the rect with the phase
if top > 0
// explicitly add 0 + originYOffset to indicate this is changed after adding accessibility support (#3650, #3520)
if top > 0 + originYOffset
{
top *= CGFloat(phaseY)
}
Expand All @@ -139,28 +169,10 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
bottom *= CGFloat(phaseY)
}

// When drawing with an auto calculated y-axis minimum, the renderer actually draws each bar from 0
// to the required value. This drawn bar is then clipped to the visible chart rect in BarLineChartViewBase's draw(rect:) using clipDataToContent.
// While this works fine when calculating the bar rects for drawing, it causes the accessibilityFrames to be oversized in some cases.
// This offset attempts to undo that unnecessary drawing when calculating barRects, particularly when not using custom axis minima.
// This allows the minimum to still be visually non zero, but the rects are only drawn where necessary.
// This offset calculation also avoids cases where there are positive/negative values mixed, since those won't need this offset.
var offset: CGFloat = 0.0
if let offsetView = dataProvider as? BarChartView {

let offsetAxis = offsetView.leftAxis.isEnabled ? offsetView.leftAxis : offsetView.rightAxis

if barData.yMin.sign != barData.yMax.sign { offset = 0.0 }
else if !offsetAxis._customAxisMin {
offset = CGFloat(offsetAxis.axisMinimum)
}
}

barRect.origin.x = left
barRect.size.width = right - left
barRect.origin.y = top
barRect.size.height = bottom == top ? 0 : bottom - top + offset

barRect.size.width = right - left
barRect.size.height = bottom - top
buffer.rects[bufferIndex] = barRect
bufferIndex += 1
}
Expand Down
Loading