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

try to fix bar chart + Horizontal Bar chart wrong render + highlight position bug for issue #214 and #242. #248

Merged
merged 1 commit into from
Jul 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify why this change was made?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each dataSet will have up to xValsCount entries, however, in some cases, if there is no dataSet's entryCount would be xValsCount, it goes wrong. If the specific xIndex doesn't have entry for every dataSet, the maxEntry count would less than x values count.

We cannot rely on dataSet.entryCount to get the maxEntry. maxEntry may not be the proper name for the new logic. But I am not sure what's the better name to have.

The true meaning of "maxEntry" would always be xValsCount, based on the fact we don't insert Null/NaN entry if no data for the xIndex. If we force the entryCount must be equal to xValsCount, the issue is no longer valid I think, but we have to insert Null/NaN and handle them then.

I am using below data code to test in demo multiple bar charts. It only has data at first index and last index. If you use old logic to calculate maxEntry, then it only draws the first index bars. The last one is missing, and the xAxis is not rendered correctly as well. The maxEntry would be 2 in old logic, however I got 11 xIndex. Applying the change, maxEntry will be 11, and the chart looks good.

Acutally, this is the first place I changed when I found the issue. But not enough to fix the bug.

- (void)setDataCount:(int)count range:(double)range
{
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < 11; i++)
    {
        [xVals addObject:[@(i + 2015) stringValue]];
    }
    NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
    NSMutableArray *yVals2 = [[NSMutableArray alloc] init];

    for (int i = 0; i < 11; i++)
    {
        if (i == 0) {
            [yVals1 addObject:[[BarChartDataEntry alloc] initWithValue:2354235 xIndex:i]];
            [yVals2 addObject:[[BarChartDataEntry alloc] initWithValue:3354235 xIndex:i]];
        } else if (i == 10) {
            [yVals1 addObject:[[BarChartDataEntry alloc] initWithValue:6354235 xIndex:i]];
            [yVals2 addObject:[[BarChartDataEntry alloc] initWithValue:7354235 xIndex:i]];
        } else {
            continue;
        }
    }

    BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals1 label:@"Company A"];
    [set1 setColor:[UIColor colorWithRed:104/255.f green:241/255.f blue:175/255.f alpha:1.f]];
    BarChartDataSet *set2 = [[BarChartDataSet alloc] initWithYVals:yVals2 label:@"Company B"];
    [set2 setColor:[UIColor colorWithRed:164/255.f green:228/255.f blue:251/255.f alpha:1.f]];

    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    [dataSets addObject:set1];
    [dataSets addObject:set2];

    BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
    data.groupSpace = 0.8;
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
    _chartView.leftAxis.startAtZeroEnabled = NO;
    _chartView.rightAxis.startAtZeroEnabled = NO;
    _chartView.data = data;
}


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