Skip to content

Commit

Permalink
Merge pull request #248 from liuxuan30/BarChart+HorizontalBarFix
Browse files Browse the repository at this point in the history
Fixes bar charts x-index calculations (Fixes #214 and fixes #242)

Specifically when yValues are missing
  • Loading branch information
danielgindi committed Jul 30, 2015
2 parents 7aec807 + 9686ac7 commit 2177f7d
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 19 deletions.
12 changes: 1 addition & 11 deletions Charts/Classes/Charts/BarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,7 @@ public class BarChartView: BarLineChartViewBase, BarChartRendererDelegate
// extend xDelta to make space for multiple datasets (if ther are one)
_deltaX *= CGFloat(_data.dataSetCount)

var maxEntry = 0

for (var i = 0, count = barData.dataSetCount; i < count; i++)
{
var set = barData.getDataSetByIndex(i)

if (maxEntry < set!.entryCount)
{
maxEntry = set!.entryCount
}
}
var maxEntry = barData.xValCount

var groupSpace = barData.groupSpace
_deltaX += CGFloat(maxEntry) * groupSpace
Expand Down
3 changes: 1 addition & 2 deletions Charts/Classes/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,8 @@ public class BarLineChartViewBase: ChartViewBase, UIGestureRecognizerDelegate
{
var bd = _data as! BarChartData
var space = bd.groupSpace
var j = _data.getDataSetByIndex(dataSetIndex)!.entryIndex(entry: entry, isEqual: true)

var x = CGFloat(j * (_data.dataSetCount - 1) + dataSetIndex) + space * CGFloat(j) + space / 2.0
var x = CGFloat(entry.xIndex * (_data.dataSetCount - 1) + dataSetIndex) + space * CGFloat(entry.xIndex) + space / 2.0

xPos += x

Expand Down
4 changes: 2 additions & 2 deletions Charts/Classes/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public class BarChartRenderer: ChartDataRendererBase
var e = entries[j]

// calculate the x-position, depending on datasetcount
var x = CGFloat(e.xIndex + j * dataSetOffset) + CGFloat(index)
+ groupSpace * CGFloat(j) + groupSpaceHalf
var x = CGFloat(e.xIndex + e.xIndex * dataSetOffset) + CGFloat(index)
+ groupSpace * CGFloat(e.xIndex) + groupSpaceHalf
var vals = e.values

if (!containsStacks || vals == nil)
Expand Down
4 changes: 2 additions & 2 deletions Charts/Classes/Renderers/HorizontalBarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public class HorizontalBarChartRenderer: BarChartRenderer
var e = entries[j]

// calculate the x-position, depending on datasetcount
var x = CGFloat(e.xIndex + j * dataSetOffset) + CGFloat(index)
+ groupSpace * CGFloat(j) + groupSpaceHalf
var x = CGFloat(e.xIndex + e.xIndex * dataSetOffset) + CGFloat(index)
+ groupSpace * CGFloat(e.xIndex) + groupSpaceHalf
let values = e.values

if (!containsStacks || values == nil)
Expand Down
4 changes: 2 additions & 2 deletions Charts/Classes/Utils/ChartTransformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public class ChartTransformer: NSObject
var e = entries[j]

// calculate the x-position, depending on datasetcount
var x = CGFloat(e.xIndex + (j * (setCount - 1)) + dataSet) + space * CGFloat(j) + space / 2.0
var x = CGFloat(e.xIndex + (e.xIndex * (setCount - 1)) + dataSet) + space * CGFloat(e.xIndex) + space / 2.0
var y = e.value

valuePoints.append(CGPoint(x: x, y: CGFloat(y) * phaseY))
Expand All @@ -167,7 +167,7 @@ public class ChartTransformer: NSObject
var e = entries[j]

// calculate the x-position, depending on datasetcount
var x = CGFloat(e.xIndex + (j * (setCount - 1)) + dataSet) + space * CGFloat(j) + space / 2.0
var x = CGFloat(e.xIndex + (e.xIndex * (setCount - 1)) + dataSet) + space * CGFloat(e.xIndex) + space / 2.0
var y = e.value

valuePoints.append(CGPoint(x: CGFloat(y) * phaseY, y: x))
Expand Down

0 comments on commit 2177f7d

Please sign in to comment.