Skip to content

Commit 8be1689

Browse files
committed
Merge branch 'label'
2 parents 04cd6f6 + 42b6f7d commit 8be1689

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

TEAChart/TEABarChart.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
// Array of NSNumber
1414
@property (nonatomic) NSArray *data;
1515

16+
// Array of NSString, nil if you don't want labels.
17+
@property (nonatomic) NSArray *xLabels;
18+
1619
// Max y value for chart (only works when autoMax is NO)
1720
@property (nonatomic) CGFloat max;
1821

TEAChart/TEABarChart.m

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ - (id)initWithCoder:(NSCoder *)decoder
3131
- (void)loadDefaults
3232
{
3333
self.opaque = NO;
34-
34+
35+
_xLabels = nil;
36+
3537
_autoMax = YES;
3638

3739
_barColor = [UIColor colorWithRed:106.0/255 green:175.0/255 blue:232.0/255 alpha:1];
@@ -51,10 +53,28 @@ - (void)drawRect:(CGRect)rect
5153
NSInteger numberOfBars = self.data.count;
5254
CGFloat barWidth = (CGRectGetWidth(rect) - self.barSpacing * (numberOfBars - 1)) / numberOfBars;
5355
CGFloat barWidthRounded = ceil(barWidth);
54-
56+
57+
if (self.xLabels) {
58+
CGFloat fontSize = floor(barWidth);
59+
CGFloat labelsTopMargin = ceil(fontSize * 0.33);
60+
barMaxHeight -= (fontSize + labelsTopMargin);
61+
62+
[self.xLabels enumerateObjectsUsingBlock:^(NSString *label, NSUInteger idx, BOOL *stop) {
63+
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
64+
paragraphStyle.alignment = NSTextAlignmentCenter;
65+
66+
[label drawInRect:CGRectMake(idx * (barWidth + self.barSpacing), barMaxHeight + labelsTopMargin, barWidth, barWidth)
67+
withAttributes:@{
68+
NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue" size:fontSize],
69+
NSForegroundColorAttributeName:[UIColor colorWithWhite:0.56 alpha:1],
70+
NSParagraphStyleAttributeName:paragraphStyle,
71+
}];
72+
}];
73+
}
74+
5575
for (NSInteger i = 0; i < numberOfBars; i += 1)
5676
{
57-
CGFloat barHeight = barMaxHeight * [self.data[i] floatValue] / max;
77+
CGFloat barHeight = (max == 0 ? 0 : barMaxHeight * [self.data[i] floatValue] / max);
5878
if (barHeight > barMaxHeight) {
5979
barHeight = barMaxHeight;
6080
}
@@ -83,6 +103,12 @@ - (void)setData:(NSArray *)data
83103
[self setNeedsDisplay];
84104
}
85105

106+
- (void)setXLabels:(NSArray *)xLabels
107+
{
108+
_xLabels = xLabels;
109+
[self setNeedsDisplay];
110+
}
111+
86112
- (void)setMax:(CGFloat)max
87113
{
88114
_max = max;

TEAChartDemo/TEAViewController.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ - (void)viewDidLoad
2222
{
2323
[super viewDidLoad];
2424

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

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

3536
// Contribution graph

0 commit comments

Comments
 (0)