From 330c31f26891e51a74f5e5944a772f8ffdd9a38e Mon Sep 17 00:00:00 2001 From: Derek Johnson Date: Wed, 27 Jan 2016 10:44:42 -0800 Subject: [PATCH 1/2] Added ability to override maxValue for line and bar charts Setting maxValue overrides default behavior of using the highest value as the max y value for the charts. --- Source/YOChartImageKit/YOBarChartImage.h | 6 ++++++ Source/YOChartImageKit/YOBarChartImage.m | 2 +- Source/YOChartImageKit/YOLineChartImage.h | 6 ++++++ Source/YOChartImageKit/YOLineChartImage.m | 2 +- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Source/YOChartImageKit/YOBarChartImage.h b/Source/YOChartImageKit/YOBarChartImage.h index 702734e..66e3055 100644 --- a/Source/YOChartImageKit/YOBarChartImage.h +++ b/Source/YOChartImageKit/YOBarChartImage.h @@ -29,6 +29,12 @@ typedef NS_ENUM(NSInteger, YOBarChartImageBarStyle){ */ @property (nonnull) 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 NSNumber* maxValue; + /** * The style of the bar chart. */ diff --git a/Source/YOChartImageKit/YOBarChartImage.m b/Source/YOChartImageKit/YOBarChartImage.m index c03d9ac..a60bab3 100644 --- a/Source/YOChartImageKit/YOBarChartImage.m +++ b/Source/YOChartImageKit/YOBarChartImage.m @@ -22,7 +22,7 @@ - (instancetype)init - (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 d768518..b9d9b24 100644 --- a/Source/YOChartImageKit/YOLineChartImage.h +++ b/Source/YOChartImageKit/YOLineChartImage.h @@ -13,6 +13,12 @@ */ @property (nonnull) 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 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..510f2cc 100644 --- a/Source/YOChartImageKit/YOLineChartImage.m +++ b/Source/YOChartImageKit/YOLineChartImage.m @@ -18,7 +18,7 @@ - (UIImage *)drawImage:(CGRect)frame scale:(CGFloat)scale { 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; From c861aea8e9188abcb2947c33b3c55c11adb6e8b0 Mon Sep 17 00:00:00 2001 From: Derek Johnson Date: Fri, 29 Jan 2016 01:12:19 -0800 Subject: [PATCH 2/2] Getter method and nonnull, nonatomic property --- Source/YOChartImageKit/YOBarChartImage.h | 2 +- Source/YOChartImageKit/YOBarChartImage.m | 4 ++++ Source/YOChartImageKit/YOLineChartImage.h | 2 +- Source/YOChartImageKit/YOLineChartImage.m | 4 ++++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Source/YOChartImageKit/YOBarChartImage.h b/Source/YOChartImageKit/YOBarChartImage.h index 66e3055..4ce9524 100644 --- a/Source/YOChartImageKit/YOBarChartImage.h +++ b/Source/YOChartImageKit/YOBarChartImage.h @@ -33,7 +33,7 @@ typedef NS_ENUM(NSInteger, YOBarChartImageBarStyle){ * The maximum value to use for the chart. Setting this will override the * default behavior of using the highest value as maxValue. */ -@property NSNumber* 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 a60bab3..d4c10d0 100644 --- a/Source/YOChartImageKit/YOBarChartImage.m +++ b/Source/YOChartImageKit/YOBarChartImage.m @@ -17,6 +17,10 @@ - (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 { diff --git a/Source/YOChartImageKit/YOLineChartImage.h b/Source/YOChartImageKit/YOLineChartImage.h index b9d9b24..48bbb3e 100644 --- a/Source/YOChartImageKit/YOLineChartImage.h +++ b/Source/YOChartImageKit/YOLineChartImage.h @@ -17,7 +17,7 @@ * The maximum value to use for the chart. Setting this will override the * default behavior of using the highest value as maxValue. */ -@property NSNumber* maxValue; +@property (nonnull, nonatomic) NSNumber* maxValue; /** * The width of chart's stroke. diff --git a/Source/YOChartImageKit/YOLineChartImage.m b/Source/YOChartImageKit/YOLineChartImage.m index 510f2cc..9317ada 100644 --- a/Source/YOChartImageKit/YOLineChartImage.m +++ b/Source/YOChartImageKit/YOLineChartImage.m @@ -12,6 +12,10 @@ - (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");