Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions Source/LineGraph/GKLineGraph.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ @interface GKLineGraph ()

@property (nonatomic, strong) NSArray *titleLabels;
@property (nonatomic, strong) NSArray *valueLabels;
@property (nonatomic, strong) NSNumberFormatter *numFormatter;

@end

Expand Down Expand Up @@ -69,6 +70,7 @@ - (void)_init {
self.lineWidth = kDefaultLineWidth;
self.margin = kDefaultMargin;
self.valueLabelCount = kDefaultValueLabelCount;
self.numFormatter=[[NSNumberFormatter alloc]init];
self.clipsToBounds = YES;
}

Expand Down Expand Up @@ -96,10 +98,12 @@ - (BOOL)_hasValueLabels {
- (void)_constructTitleLabels {

NSInteger count = [[self.dataSource valuesForLineAtIndex:0] count];
// dynamic width
CGFloat adjustedWitdth = MAX((self.frame.size.width - (kAxisMargin + self.margin))/count, kDefaultLabelWidth);
id items = [NSMutableArray arrayWithCapacity:count];
for (NSInteger idx = 0; idx < count; idx++) {

CGRect frame = CGRectMake(0, 0, kDefaultLabelWidth, kDefaultLabelHeight);
CGRect frame = CGRectMake(0, 0, adjustedWitdth, kDefaultLabelHeight);
UILabel *item = [[UILabel alloc] initWithFrame:frame];
item.textAlignment = NSTextAlignmentCenter;
item.font = [UIFont boldSystemFontOfSize:12];
Expand All @@ -124,12 +128,14 @@ - (void)_positionTitleLabels {
id values = [self.dataSource valuesForLineAtIndex:0];
[values mk_each:^(id value) {

CGFloat labelWidth = kDefaultLabelWidth;
UILabel *label = [self.titleLabels objectAtIndex:idx];

// dynamic width
CGFloat labelWidth = label.frame.size.width;
CGFloat labelHeight = kDefaultLabelHeight;
CGFloat startX = [self _pointXForIndex:idx] - (labelWidth / 2);
CGFloat startY = (self.height - labelHeight);

UILabel *label = [self.titleLabels objectAtIndex:idx];
label.x = startX;
label.y = startY;

Expand Down Expand Up @@ -165,22 +171,45 @@ - (void)_constructValueLabels {
CGFloat value = [self _minValue] + (idx * [self _stepValueLabelY]);
item.centerY = [self _positionYForLineValue:value];

item.text = [@(ceil(value)) stringValue];
// item.text = [@(value) stringValue];
//item.text = [@(ceil(value)) stringValue];
//item.text = [@(value) stringValue];
// Fractions as labels on Y axis
item.text = [self formatFloat:value withOptimalDigits:2 maxDecimals:3];

[items addObject:item];
[self addSubview:item];
}
self.valueLabels = items;
}

- (NSString*)formatFloat:(Float32)number withOptimalDigits:(UInt8)optimalDigits maxDecimals:(UInt8)maxDecimals
{
NSString* result;
UInt8 intDigits=(int)log10f(number)+1;
NSLog(@"Formatting %.5f with maxDig: %d maxDec: %d intLength: %d",number,optimalDigits,maxDecimals,intDigits);

self.numFormatter.maximumFractionDigits=maxDecimals;
if(intDigits>=optimalDigits-maxDecimals) {
self.numFormatter.usesSignificantDigits=YES;
self.numFormatter.maximumSignificantDigits=(intDigits>optimalDigits)?intDigits:optimalDigits;
} else {
self.numFormatter.usesSignificantDigits=NO;
}
result = [self.numFormatter stringFromNumber:[NSNumber numberWithFloat:number]];

return result;
}

- (CGFloat)_stepValueLabelY {
return (([self _maxValue] - [self _minValue]) / (self.valueLabelCount - 1));
}

- (CGFloat)_maxValue {
id values = [self _allValues];
return [[values mk_max] floatValue];
// Check if not the same value as minimum
CGFloat min = [self _minValue];
CGFloat max = MAX(min, [[values mk_max] floatValue]);
return (max == min ? max+1.0 :max);
}

- (CGFloat)_minValue {
Expand Down