Skip to content

Commit

Permalink
Correctly position 0 in stacked bar (Closes ChartsOrg#1195, #closes C…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Dec 19, 2016
1 parent 8217f06 commit fee8cd4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ - (void)viewDidLoad
_chartView.pinchZoomEnabled = NO;

_chartView.drawBarShadowEnabled = NO;
_chartView.drawValueAboveBarEnabled = YES;

_chartView.leftAxis.enabled = NO;
_chartView.rightAxis.axisMaximum = 25.0;
Expand Down
20 changes: 16 additions & 4 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,18 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
var negY = -e.negativeSum
var yStart = 0.0


// fill the stack
for k in 0 ..< vals!.count
{
let value = vals![k]

if value >= 0.0
if value == 0.0 && (posY == 0.0 || negY == 0.0)
{
// Take care of the situation of a 0.0 value, which overlaps a non-zero bar
y = value
yStart = y
}
else if value >= 0.0
{
y = posY
yStart = posY + value
Expand Down Expand Up @@ -494,7 +499,12 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
let value = vals[k]
var y: Double

if value >= 0.0
if value == 0.0 && (posY == 0.0 || negY == 0.0)
{
// Take care of the situation of a 0.0 value, which overlaps a non-zero bar
y = value
}
else if value >= 0.0
{
posY += value
y = posY
Expand All @@ -512,7 +522,9 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer

for k in 0 ..< transformed.count
{
let y = transformed[k].y + (vals[k] >= 0 ? posOffset : negOffset)
let val = vals[k]
let drawBelow = (val == 0.0 && negY == 0.0 && posY > 0.0) || val < 0.0
let y = transformed[k].y + (drawBelow ? negOffset : posOffset)

if !viewPortHandler.isInBoundsRight(x)
{
Expand Down
19 changes: 16 additions & 3 deletions Source/Charts/Renderers/HorizontalBarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,13 @@ open class HorizontalBarChartRenderer: BarChartRenderer
{
let value = vals![k]

if value >= 0.0
if value == 0.0 && (posY == 0.0 || negY == 0.0)
{
// Take care of the situation of a 0.0 value, which overlaps a non-zero bar
y = value
yStart = y
}
else if value >= 0.0
{
y = posY
yStart = posY + value
Expand Down Expand Up @@ -467,7 +473,12 @@ open class HorizontalBarChartRenderer: BarChartRenderer
let value = vals[k]
var y: Double

if value >= 0.0
if value == 0.0 && (posY == 0.0 || negY == 0.0)
{
// Take care of the situation of a 0.0 value, which overlaps a non-zero bar
y = value
}
else if value >= 0.0
{
posY += value
y = posY
Expand Down Expand Up @@ -503,7 +514,9 @@ open class HorizontalBarChartRenderer: BarChartRenderer
negOffset = -negOffset - valueTextWidth
}

let x = transformed[k].x + (val >= 0 ? posOffset : negOffset)
let drawBelow = (val == 0.0 && negY == 0.0 && posY > 0.0) || val < 0.0

let x = transformed[k].x + (drawBelow ? negOffset : posOffset)
let y = rect.origin.y + rect.size.height / 2.0

if (!viewPortHandler.isInBoundsTop(y))
Expand Down

0 comments on commit fee8cd4

Please sign in to comment.