-
-
Notifications
You must be signed in to change notification settings - Fork 6k
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
try to fix bar chart + Horizontal Bar chart wrong render + highlight position bug for issue #214 and #242. #248
Conversation
This looks pretty good to me. Is it tested? |
I just tested on my side with a few bar charts. I am not sure about other parts either. |
@@ -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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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;
}
@PhilJay I went through it - looks like the change in BarChart view is legit! |
Alright, I managed to squash commits and merge v2.1.2 and #221. I will paste old message from #221 below.
Try to fix wrong position bug. use entry.xIndex instead of j of dataSet.entryCount to calculate the offset and position. Issue #214 + #242
The idea is, because not every xIndex will have a dataEntry, if the data is invalid (nil/nan/null), the entryCount will be less than xValsCount.
For example, xVals: [0,1,2,3,4,5,6,7,8,9,10], if there are two dataSets, like below:
xIndex:0 value:100, xIndex:10 value:200
xIndex:0 value:1000, xIndex:10 value:2000
Current logic will use entryCount instead of xValsCount to calculate the matrix and position, which will lead to shorter xPosition value on xAxis. issue #214 has the screenshot.
What I changed is I always use xValsCount and actual xIndex value to calculate.
@danielgindi There might be something I missed if I am not correct. On the other hand, if you think I am right, I would also miss places to apply the new logic, because while I am testing, I found I missed to change getMarkerPosition. There could be more.
merge from upstream master (+3 squashed commits)
Squashed commits:
[c861fea] add fix for horizontal bar chart
[2f75778] apply the same logic for getMarkerPosition
[baca6dd] try to fix wrong position bug. use entry.xIndex instead of j of dataSet.entryCount to calculate the offset and position