diff --git a/TEAChart/TEABarChart.h b/TEAChart/TEABarChart.h index 27c5235..98c16dc 100644 --- a/TEAChart/TEABarChart.h +++ b/TEAChart/TEABarChart.h @@ -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; diff --git a/TEAChart/TEABarChart.m b/TEAChart/TEABarChart.m index 0d1a5c2..b325c55 100644 --- a/TEAChart/TEABarChart.m +++ b/TEAChart/TEABarChart.m @@ -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]; @@ -51,7 +53,25 @@ - (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; @@ -83,6 +103,12 @@ - (void)setData:(NSArray *)data [self setNeedsDisplay]; } +- (void)setXLabels:(NSArray *)xLabels +{ + _xLabels = xLabels; + [self setNeedsDisplay]; +} + - (void)setMax:(CGFloat)max { _max = max; diff --git a/TEAChartDemo/TEAViewController.m b/TEAChartDemo/TEAViewController.m index fc4e2f1..c2adfd3 100644 --- a/TEAChartDemo/TEAViewController.m +++ b/TEAChartDemo/TEAViewController.m @@ -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