Skip to content

Commit

Permalink
Merge branch 'label'
Browse files Browse the repository at this point in the history
  • Loading branch information
xhacker committed Dec 21, 2014
2 parents 04cd6f6 + 42b6f7d commit 8be1689
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
3 changes: 3 additions & 0 deletions TEAChart/TEABarChart.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
// Array of NSNumber
@property (nonatomic) NSArray *data;

// Array of NSString, nil if you don't want labels.
@property (nonatomic) NSArray *xLabels;

// Max y value for chart (only works when autoMax is NO)
@property (nonatomic) CGFloat max;

Expand Down
32 changes: 29 additions & 3 deletions TEAChart/TEABarChart.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ - (id)initWithCoder:(NSCoder *)decoder
- (void)loadDefaults
{
self.opaque = NO;


_xLabels = nil;

_autoMax = YES;

_barColor = [UIColor colorWithRed:106.0/255 green:175.0/255 blue:232.0/255 alpha:1];
Expand All @@ -51,10 +53,28 @@ - (void)drawRect:(CGRect)rect
NSInteger numberOfBars = self.data.count;
CGFloat barWidth = (CGRectGetWidth(rect) - self.barSpacing * (numberOfBars - 1)) / numberOfBars;
CGFloat barWidthRounded = ceil(barWidth);


if (self.xLabels) {
CGFloat fontSize = floor(barWidth);
CGFloat labelsTopMargin = ceil(fontSize * 0.33);
barMaxHeight -= (fontSize + labelsTopMargin);

[self.xLabels enumerateObjectsUsingBlock:^(NSString *label, NSUInteger idx, BOOL *stop) {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
paragraphStyle.alignment = NSTextAlignmentCenter;

[label drawInRect:CGRectMake(idx * (barWidth + self.barSpacing), barMaxHeight + labelsTopMargin, barWidth, barWidth)
withAttributes:@{
NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue" size:fontSize],
NSForegroundColorAttributeName:[UIColor colorWithWhite:0.56 alpha:1],
NSParagraphStyleAttributeName:paragraphStyle,
}];
}];
}

for (NSInteger i = 0; i < numberOfBars; i += 1)
{
CGFloat barHeight = barMaxHeight * [self.data[i] floatValue] / max;
CGFloat barHeight = (max == 0 ? 0 : barMaxHeight * [self.data[i] floatValue] / max);
if (barHeight > barMaxHeight) {
barHeight = barMaxHeight;
}
Expand Down Expand Up @@ -83,6 +103,12 @@ - (void)setData:(NSArray *)data
[self setNeedsDisplay];
}

- (void)setXLabels:(NSArray *)xLabels
{
_xLabels = xLabels;
[self setNeedsDisplay];
}

- (void)setMax:(CGFloat)max
{
_max = max;
Expand Down
7 changes: 4 additions & 3 deletions TEAChartDemo/TEAViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ - (void)viewDidLoad
{
[super viewDidLoad];

// Line chart, the Storyboard way
// Bar chart, the Storyboard way
self.barChart.data = @[@3, @1, @4, @1, @5, @9, @2, @6, @5, @3];
self.barChart.barSpacing = 10;
self.barChart.barColors = @[[UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor blueColor]];

// Line chart, the code way
TEABarChart *secondBarChart = [[TEABarChart alloc] initWithFrame:CGRectMake(35, 180, 100, 40)];
// Bar chart, the code way
TEABarChart *secondBarChart = [[TEABarChart alloc] initWithFrame:CGRectMake(35, 180, 100, 56)];
secondBarChart.data = @[@2, @7, @1, @8, @2, @8];
secondBarChart.xLabels = @[@"A", @"B", @"C", @"D", @"E", @"F"];
[self.view addSubview:secondBarChart];

// Contribution graph
Expand Down

0 comments on commit 8be1689

Please sign in to comment.