Skip to content

Commit

Permalink
Merge pull request #3 from EpicDraws/master
Browse files Browse the repository at this point in the history
Added ability to override maxValue for line and bar charts
  • Loading branch information
yasuoza committed Jan 29, 2016
2 parents 87f4c83 + c861aea commit e2a3bd7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Source/YOChartImageKit/YOBarChartImage.h
Expand Up @@ -29,6 +29,12 @@ typedef NS_ENUM(NSInteger, YOBarChartImageBarStyle){
*/
@property (nonnull, nonatomic) NSArray<NSNumber *> *values;

/**
* The maximum value to use for the chart. Setting this will override the
* default behavior of using the highest value as maxValue.
*/
@property (nonnull, nonatomic) NSNumber* maxValue;

/**
* The style of the bar chart.
*/
Expand Down
6 changes: 5 additions & 1 deletion Source/YOChartImageKit/YOBarChartImage.m
Expand Up @@ -17,12 +17,16 @@ - (instancetype)init
return self;
}

- (NSNumber *) maxValue {
return _maxValue ? _maxValue : [NSNumber numberWithFloat:[[_values valueForKeyPath:@"@max.floatValue"] floatValue]];
}

const CGFloat kBarPaddingMultipler = 20.0f;

- (UIImage *)drawImage:(CGRect)frame scale:(CGFloat)scale {
NSAssert(_values.count > 0, @"YOBarChartImage // must assign values property which is an array of NSNumber");

CGFloat maxValue = [[_values valueForKeyPath:@"@max.floatValue"] floatValue];
CGFloat maxValue = _maxValue ? [_maxValue floatValue] : [[_values valueForKeyPath:@"@max.floatValue"] floatValue];
CGFloat dataCount = (CGFloat)_values.count;

CGFloat padding;
Expand Down
6 changes: 6 additions & 0 deletions Source/YOChartImageKit/YOLineChartImage.h
Expand Up @@ -13,6 +13,12 @@
*/
@property (nonnull, nonatomic) NSArray<NSNumber *> *values;

/**
* The maximum value to use for the chart. Setting this will override the
* default behavior of using the highest value as maxValue.
*/
@property (nonnull, nonatomic) NSNumber* maxValue;

/**
* The width of chart's stroke.
* The default width is `1.0`.
Expand Down
6 changes: 5 additions & 1 deletion Source/YOChartImageKit/YOLineChartImage.m
Expand Up @@ -12,13 +12,17 @@ - (instancetype)init {
return self;
}

- (NSNumber *) maxValue {
return _maxValue ? _maxValue : [NSNumber numberWithFloat:[[_values valueForKeyPath:@"@max.floatValue"] floatValue]];
}

- (UIImage *)drawImage:(CGRect)frame scale:(CGFloat)scale {
NSAssert(_values.count > 0, @"YOLineChartImage // must assign values property which is an array of NSNumber");

NSUInteger valuesCount = _values.count;
CGFloat pointX = frame.size.width / (valuesCount - 1);
NSMutableArray<NSValue *> *points = [NSMutableArray array];
CGFloat maxValue = [[_values valueForKeyPath:@"@max.floatValue"] floatValue];
CGFloat maxValue = _maxValue ? [_maxValue floatValue] : [[_values valueForKeyPath:@"@max.floatValue"] floatValue];

[_values enumerateObjectsUsingBlock:^(NSNumber *number, NSUInteger idx, BOOL *_) {
CGFloat ratioY = number.floatValue / maxValue;
Expand Down

0 comments on commit e2a3bd7

Please sign in to comment.