Skip to content

Commit

Permalink
[Fix #4] Round x values for bar chart to avoid artifacts.
Browse files Browse the repository at this point in the history
  • Loading branch information
xhacker committed Jan 31, 2014
1 parent f2303a4 commit d5c43cd
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions TEAChart/TEABarChart.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ - (void)drawRect:(CGRect)rect
CGFloat barMaxHeight = CGRectGetHeight(rect);
NSInteger numberOfBars = self.data.count;
CGFloat barWidth = (CGRectGetWidth(rect) - self.barSpacing * (numberOfBars - 1)) / numberOfBars;
CGFloat barWidthRounded = ceil(barWidth);

for (NSInteger i = 0; i < numberOfBars; i += 1) {
CGFloat barHeight = barMaxHeight * [self.data[i] floatValue] / max;
Expand All @@ -59,12 +60,14 @@ - (void)drawRect:(CGRect)rect
barHeight = (int)barHeight;
}

CGFloat x = floor(i * (barWidth + self.barSpacing));

[self.backgroundColor setFill];
CGRect backgroundRect = CGRectMake(i * (barWidth + self.barSpacing), 0, barWidth, barMaxHeight);
CGRect backgroundRect = CGRectMake(x, 0, barWidthRounded, barMaxHeight);
CGContextFillRect(context, backgroundRect);

[self.barColor setFill];
CGRect barRect = CGRectMake(i * (barWidth + self.barSpacing), barMaxHeight - barHeight, barWidth, barHeight);
CGRect barRect = CGRectMake(x, barMaxHeight - barHeight, barWidthRounded, barHeight);
CGContextFillRect(context, barRect);
}
}
Expand Down

0 comments on commit d5c43cd

Please sign in to comment.