diff --git a/Source/YOChartImageKit/YOBarChartImage.h b/Source/YOChartImageKit/YOBarChartImage.h index 0ebf1e6..6984775 100644 --- a/Source/YOChartImageKit/YOBarChartImage.h +++ b/Source/YOChartImageKit/YOBarChartImage.h @@ -29,6 +29,12 @@ typedef NS_ENUM(NSInteger, YOBarChartImageBarStyle){ */ @property (nonnull, nonatomic) NSArray *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. */ diff --git a/Source/YOChartImageKit/YOBarChartImage.m b/Source/YOChartImageKit/YOBarChartImage.m index c03d9ac..d4c10d0 100644 --- a/Source/YOChartImageKit/YOBarChartImage.m +++ b/Source/YOChartImageKit/YOBarChartImage.m @@ -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; diff --git a/Source/YOChartImageKit/YOLineChartImage.h b/Source/YOChartImageKit/YOLineChartImage.h index b6a38ad..aa96f89 100644 --- a/Source/YOChartImageKit/YOLineChartImage.h +++ b/Source/YOChartImageKit/YOLineChartImage.h @@ -13,6 +13,12 @@ */ @property (nonnull, nonatomic) NSArray *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`. diff --git a/Source/YOChartImageKit/YOLineChartImage.m b/Source/YOChartImageKit/YOLineChartImage.m index 77f86c7..9317ada 100644 --- a/Source/YOChartImageKit/YOLineChartImage.m +++ b/Source/YOChartImageKit/YOLineChartImage.m @@ -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 *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;