Skip to content

Commit

Permalink
Fixed negative stacked bars when there's more than two values in stac…
Browse files Browse the repository at this point in the history
…k (Fixed issue ChartsOrg#212)
  • Loading branch information
danielgindi committed Jul 16, 2015
1 parent 28dbd12 commit 2792408
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 44 deletions.
42 changes: 20 additions & 22 deletions Charts/Classes/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ public class BarChartRenderer: ChartDataRendererBase
}
else
{
var allPos = e.positiveSum
var allNeg = e.negativeSum
var posY = 0.0
var negY = 0.0
var y2 = 0.0

// if drawing the bar shadow is enabled
if (drawBarShadowEnabled)
Expand Down Expand Up @@ -197,29 +198,25 @@ public class BarChartRenderer: ChartDataRendererBase

if value >= 0.0
{
allPos -= value
y = value + allPos
y = posY
y2 = posY + value
posY = y2
}
else
{
allNeg -= abs(value)
y = value + allNeg
y = negY
y2 = negY + value
negY = y2
}

var left = x - barWidth + barSpaceHalf
var right = x + barWidth - barSpaceHalf
var top = y >= 0.0 ? CGFloat(y) : 0
var bottom = y <= 0.0 ? CGFloat(y) : 0
var top = y >= y2 ? CGFloat(y) : CGFloat(y2)
var bottom = y <= y2 ? CGFloat(y) : CGFloat(y2)

// multiply the height of the rect with the phase
if (top > 0)
{
top *= phaseY
}
else
{
bottom *= phaseY
}
top *= phaseY
bottom *= phaseY

barRect.origin.x = left
barRect.size.width = right - left
Expand Down Expand Up @@ -385,8 +382,9 @@ public class BarChartRenderer: ChartDataRendererBase
else
{
var transformed = [CGPoint]()
var allPos = e.positiveSum
var allNeg = e.negativeSum

var posY = 0.0
var negY = 0.0

for (var k = 0; k < vals.count; k++)
{
Expand All @@ -395,13 +393,13 @@ public class BarChartRenderer: ChartDataRendererBase

if value >= 0.0
{
allPos -= value
y = value + allPos
posY += value
y = posY
}
else
{
allNeg -= abs(value)
y = value + allNeg
negY += value
y = negY
}

transformed.append(CGPoint(x: 0.0, y: CGFloat(y) * _animator.phaseY))
Expand Down
42 changes: 20 additions & 22 deletions Charts/Classes/Renderers/HorizontalBarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ public class HorizontalBarChartRenderer: BarChartRenderer
}
else
{
var allPos = e.positiveSum
var allNeg = e.negativeSum
var posY = 0.0
var negY = 0.0
var y2 = 0.0

// if drawing the bar shadow is enabled
if (drawBarShadowEnabled)
Expand Down Expand Up @@ -158,29 +159,25 @@ public class HorizontalBarChartRenderer: BarChartRenderer

if value >= 0.0
{
allPos -= value
y = value + allPos
y = posY
y2 = posY + value
posY = y2
}
else
{
allNeg -= abs(value)
y = value + allNeg
y = negY
y2 = negY + value
negY = y2
}

var bottom = x - barWidth + barSpaceHalf
var top = x + barWidth - barSpaceHalf
var right = y >= 0.0 ? CGFloat(y) : 0.0
var left = y <= 0.0 ? CGFloat(y) : 0.0
var right = y >= y2 ? CGFloat(y) : CGFloat(y2)
var left = y <= y2 ? CGFloat(y) : CGFloat(y2)

// multiply the height of the rect with the phase
if (right > 0)
{
right *= phaseY
}
else
{
left *= phaseY
}
right *= phaseY
left *= phaseY

barRect.origin.x = left
barRect.size.width = right - left
Expand Down Expand Up @@ -378,8 +375,9 @@ public class HorizontalBarChartRenderer: BarChartRenderer
else
{
var transformed = [CGPoint]()
var allPos = e.positiveSum
var allNeg = e.negativeSum

var posY = 0.0
var negY = 0.0

for (var k = 0; k < vals.count; k++)
{
Expand All @@ -388,13 +386,13 @@ public class HorizontalBarChartRenderer: BarChartRenderer

if value >= 0.0
{
allPos -= value
y = value + allPos
posY += value
y = posY
}
else
{
allNeg -= abs(value)
y = value + allNeg
negY += value
y = negY
}

transformed.append(CGPoint(x: CGFloat(y) * _animator.phaseY, y: 0.0))
Expand Down

0 comments on commit 2792408

Please sign in to comment.