Merge pull request #250 from pkclsoft/master
Enhancements to Bar and Line Charts
Showing
10 changed files
with
45 additions
and
11 deletions
PNChart/PNBar.h
100644 → 100755
| @@ -26,6 +26,9 @@ | @@ -26,6 +26,9 @@ | ||
| 26 | @property (nonatomic) CAShapeLayer *gradeLayer; | 26 | @property (nonatomic) CAShapeLayer *gradeLayer; |
| 27 | @property (nonatomic) CATextLayer* textLayer; | 27 | @property (nonatomic) CATextLayer* textLayer; |
| 28 | 28 | ||
| 29 | +/** Text color for all bars in the chart. */ | ||
| 30 | +@property (nonatomic) UIColor * labelTextColor; | ||
| 31 | + | ||
| 29 | @property (nonatomic, assign) BOOL isNegative; //!< 是否是负数 | 32 | @property (nonatomic, assign) BOOL isNegative; //!< 是否是负数 |
| 30 | @property (nonatomic, assign) BOOL isShowNumber; //!< 是否显示numbers | 33 | @property (nonatomic, assign) BOOL isShowNumber; //!< 是否显示numbers |
| 31 | @end | 34 | @end |
PNChart/PNBar.m
100644 → 100755
| @@ -181,7 +181,7 @@ | @@ -181,7 +181,7 @@ | ||
| 181 | _textLayer = [[CATextLayer alloc]init]; | 181 | _textLayer = [[CATextLayer alloc]init]; |
| 182 | [_textLayer setString:@"0"]; | 182 | [_textLayer setString:@"0"]; |
| 183 | [_textLayer setAlignmentMode:kCAAlignmentCenter]; | 183 | [_textLayer setAlignmentMode:kCAAlignmentCenter]; |
| 184 | - [_textLayer setForegroundColor:[[UIColor colorWithRed:178/255.0 green:178/255. blue:178/255.0 alpha:1.0] CGColor]]; | 184 | + [_textLayer setForegroundColor:[_labelTextColor CGColor]]; |
| 185 | _textLayer.hidden = YES; | 185 | _textLayer.hidden = YES; |
| 186 | 186 | ||
| 187 | } | 187 | } |
| @@ -189,6 +189,11 @@ | @@ -189,6 +189,11 @@ | ||
| 189 | return _textLayer; | 189 | return _textLayer; |
| 190 | } | 190 | } |
| 191 | 191 | ||
| 192 | +- (void) setLabelTextColor:(UIColor *)labelTextColor { | ||
| 193 | + _labelTextColor = labelTextColor; | ||
| 194 | + [_textLayer setForegroundColor:[_labelTextColor CGColor]]; | ||
| 195 | +} | ||
| 196 | + | ||
| 192 | -(void)setGradeFrame:(CGFloat)grade startPosY:(CGFloat)startPosY | 197 | -(void)setGradeFrame:(CGFloat)grade startPosY:(CGFloat)startPosY |
| 193 | { | 198 | { |
| 194 | CGFloat textheigt = self.bounds.size.height*self.grade; | 199 | CGFloat textheigt = self.bounds.size.height*self.grade; |
PNChart/PNBarChart.h
100644 → 100755
| @@ -63,6 +63,8 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue); | @@ -63,6 +63,8 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue); | ||
| 63 | /** Controls whether the chart border line should be displayed. */ | 63 | /** Controls whether the chart border line should be displayed. */ |
| 64 | @property (nonatomic) BOOL showChartBorder; | 64 | @property (nonatomic) BOOL showChartBorder; |
| 65 | 65 | ||
| 66 | +@property (nonatomic) UIColor *chartBorderColor; | ||
| 67 | + | ||
| 66 | /** Controls whether the chart Horizontal separator should be displayed. */ | 68 | /** Controls whether the chart Horizontal separator should be displayed. */ |
| 67 | @property (nonatomic, assign) BOOL showLevelLine; | 69 | @property (nonatomic, assign) BOOL showLevelLine; |
| 68 | 70 |
PNChart/PNBarChart.m
100644 → 100755
| @@ -62,6 +62,7 @@ | @@ -62,6 +62,7 @@ | ||
| 62 | _chartMarginBottom = 25.0; | 62 | _chartMarginBottom = 25.0; |
| 63 | _barRadius = 2.0; | 63 | _barRadius = 2.0; |
| 64 | _showChartBorder = NO; | 64 | _showChartBorder = NO; |
| 65 | + _chartBorderColor = PNLightGrey; | ||
| 65 | _showLevelLine = NO; | 66 | _showLevelLine = NO; |
| 66 | _yChartLabelWidth = 18; | 67 | _yChartLabelWidth = 18; |
| 67 | _rotateForXAxisText = false; | 68 | _rotateForXAxisText = false; |
| @@ -234,7 +235,7 @@ | @@ -234,7 +235,7 @@ | ||
| 234 | } | 235 | } |
| 235 | 236 | ||
| 236 | bar = [[PNBar alloc] initWithFrame:CGRectMake(barXPosition, //Bar X position | 237 | bar = [[PNBar alloc] initWithFrame:CGRectMake(barXPosition, //Bar X position |
| 237 | - self.frame.size.height - chartCavanHeight - kXLabelHeight - _chartMarginTop , //Bar Y position | 238 | + self.frame.size.height - chartCavanHeight - kXLabelHeight - _chartMarginBottom + _chartMarginTop , //Bar Y position |
| 238 | barWidth, // Bar witdh | 239 | barWidth, // Bar witdh |
| 239 | self.showLevelLine ? chartCavanHeight/2.0:chartCavanHeight)]; //Bar height | 240 | self.showLevelLine ? chartCavanHeight/2.0:chartCavanHeight)]; //Bar height |
| 240 | 241 | ||
| @@ -249,6 +250,10 @@ | @@ -249,6 +250,10 @@ | ||
| 249 | }else{ | 250 | }else{ |
| 250 | bar.barColor = [self barColorAtIndex:index]; | 251 | bar.barColor = [self barColorAtIndex:index]; |
| 251 | } | 252 | } |
| 253 | + | ||
| 254 | + if (self.labelTextColor) { | ||
| 255 | + bar.labelTextColor = self.labelTextColor; | ||
| 256 | + } | ||
| 252 | 257 | ||
| 253 | // Add gradient | 258 | // Add gradient |
| 254 | if (self.isGradientShow) { | 259 | if (self.isGradientShow) { |
| @@ -309,13 +314,13 @@ | @@ -309,13 +314,13 @@ | ||
| 309 | 314 | ||
| 310 | UIBezierPath *progressline = [UIBezierPath bezierPath]; | 315 | UIBezierPath *progressline = [UIBezierPath bezierPath]; |
| 311 | 316 | ||
| 312 | - [progressline moveToPoint:CGPointMake(_chartMarginLeft, self.frame.size.height - kXLabelHeight - _chartMarginTop)]; | 317 | + [progressline moveToPoint:CGPointMake(_chartMarginLeft, self.frame.size.height - kXLabelHeight - _chartMarginBottom + _chartMarginTop)]; |
| 313 | - [progressline addLineToPoint:CGPointMake(self.frame.size.width - _chartMarginRight, self.frame.size.height - kXLabelHeight - _chartMarginTop)]; | 318 | + [progressline addLineToPoint:CGPointMake(self.frame.size.width - _chartMarginRight, self.frame.size.height - kXLabelHeight - _chartMarginBottom + _chartMarginTop)]; |
| 314 | 319 | ||
| 315 | [progressline setLineWidth:1.0]; | 320 | [progressline setLineWidth:1.0]; |
| 316 | [progressline setLineCapStyle:kCGLineCapSquare]; | 321 | [progressline setLineCapStyle:kCGLineCapSquare]; |
| 317 | _chartBottomLine.path = progressline.CGPath; | 322 | _chartBottomLine.path = progressline.CGPath; |
| 318 | - _chartBottomLine.strokeColor = PNLightGrey.CGColor; | 323 | + _chartBottomLine.strokeColor = [_chartBorderColor CGColor];; |
| 319 | 324 | ||
| 320 | CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; | 325 | CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; |
| 321 | pathAnimation.duration = 0.5; | 326 | pathAnimation.duration = 0.5; |
| @@ -344,7 +349,7 @@ | @@ -344,7 +349,7 @@ | ||
| 344 | [progressLeftline setLineWidth:1.0]; | 349 | [progressLeftline setLineWidth:1.0]; |
| 345 | [progressLeftline setLineCapStyle:kCGLineCapSquare]; | 350 | [progressLeftline setLineCapStyle:kCGLineCapSquare]; |
| 346 | _chartLeftLine.path = progressLeftline.CGPath; | 351 | _chartLeftLine.path = progressLeftline.CGPath; |
| 347 | - _chartLeftLine.strokeColor = PNLightGrey.CGColor; | 352 | + _chartLeftLine.strokeColor = [_chartBorderColor CGColor]; |
| 348 | 353 | ||
| 349 | CABasicAnimation *pathLeftAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; | 354 | CABasicAnimation *pathLeftAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; |
| 350 | pathLeftAnimation.duration = 0.5; | 355 | pathLeftAnimation.duration = 0.5; |
PNChart/PNLineChart.h
100644 → 100755
| @@ -45,11 +45,14 @@ | @@ -45,11 +45,14 @@ | ||
| 45 | @property (nonatomic) UIColor *yLabelColor; | 45 | @property (nonatomic) UIColor *yLabelColor; |
| 46 | @property (nonatomic) CGFloat chartCavanHeight; | 46 | @property (nonatomic) CGFloat chartCavanHeight; |
| 47 | @property (nonatomic) CGFloat chartCavanWidth; | 47 | @property (nonatomic) CGFloat chartCavanWidth; |
| 48 | -@property (nonatomic) CGFloat chartMargin; | ||
| 49 | @property (nonatomic) BOOL showLabel; | 48 | @property (nonatomic) BOOL showLabel; |
| 50 | @property (nonatomic) BOOL showGenYLabels; | 49 | @property (nonatomic) BOOL showGenYLabels; |
| 51 | @property (nonatomic) BOOL thousandsSeparator; | 50 | @property (nonatomic) BOOL thousandsSeparator; |
| 52 | 51 | ||
| 52 | +@property (nonatomic) CGFloat chartMarginLeft; | ||
| 53 | +@property (nonatomic) CGFloat chartMarginRight; | ||
| 54 | +@property (nonatomic) CGFloat chartMarginTop; | ||
| 55 | +@property (nonatomic) CGFloat chartMarginBottom; | ||
| 53 | 56 | ||
| 54 | /** | 57 | /** |
| 55 | * Controls whether to show the coordinate axis. Default is NO. | 58 | * Controls whether to show the coordinate axis. Default is NO. |
PNChart/PNLineChart.m
100644 → 100755
This diff is collapsed. Click to expand it.
PNChart/PNLineChartData.h
100644 → 100755
| @@ -25,6 +25,11 @@ typedef PNLineChartDataItem *(^LCLineChartDataGetter)(NSUInteger item); | @@ -25,6 +25,11 @@ typedef PNLineChartDataItem *(^LCLineChartDataGetter)(NSUInteger item); | ||
| 25 | @property (copy) LCLineChartDataGetter getData; | 25 | @property (copy) LCLineChartDataGetter getData; |
| 26 | @property (strong, nonatomic) NSString *dataTitle; | 26 | @property (strong, nonatomic) NSString *dataTitle; |
| 27 | 27 | ||
| 28 | +@property (nonatomic) BOOL showPointLabel; | ||
| 29 | +@property (nonatomic) UIColor *pointLabelColor; | ||
| 30 | +@property (nonatomic) UIFont *pointLabelFont; | ||
| 31 | +@property (nonatomic) NSString *pointLabelFormat; | ||
| 32 | + | ||
| 28 | @property (nonatomic, assign) PNLineChartPointStyle inflexionPointStyle; | 33 | @property (nonatomic, assign) PNLineChartPointStyle inflexionPointStyle; |
| 29 | 34 | ||
| 30 | /** | 35 | /** |
PNChart/PNLineChartData.m
100644 → 100755
| @@ -23,6 +23,9 @@ | @@ -23,6 +23,9 @@ | ||
| 23 | _inflexionPointWidth = 6.f; | 23 | _inflexionPointWidth = 6.f; |
| 24 | _lineWidth = 2.f; | 24 | _lineWidth = 2.f; |
| 25 | _alpha = 1.f; | 25 | _alpha = 1.f; |
| 26 | + _showPointLabel = NO; | ||
| 27 | + _pointLabelColor = [UIColor blackColor]; | ||
| 28 | + _pointLabelFormat = @"%1.f"; | ||
| 26 | } | 29 | } |
| 27 | 30 | ||
| 28 | @end | 31 | @end |
PNChart/PNLineChartDataItem.h
100644 → 100755
| @@ -9,7 +9,9 @@ | @@ -9,7 +9,9 @@ | ||
| 9 | @interface PNLineChartDataItem : NSObject | 9 | @interface PNLineChartDataItem : NSObject |
| 10 | 10 | ||
| 11 | + (PNLineChartDataItem *)dataItemWithY:(CGFloat)y; | 11 | + (PNLineChartDataItem *)dataItemWithY:(CGFloat)y; |
| 12 | ++ (PNLineChartDataItem *)dataItemWithY:(CGFloat)y andRawY:(CGFloat)rawY; | ||
| 12 | 13 | ||
| 13 | @property (readonly) CGFloat y; // should be within the y range | 14 | @property (readonly) CGFloat y; // should be within the y range |
| 15 | +@property (readonly) CGFloat rawY; // this is the raw value, used for point label. | ||
| 14 | 16 | ||
| 15 | @end | 17 | @end |
PNChart/PNLineChartDataItem.m
100644 → 100755
| @@ -7,9 +7,10 @@ | @@ -7,9 +7,10 @@ | ||
| 7 | 7 | ||
| 8 | @interface PNLineChartDataItem () | 8 | @interface PNLineChartDataItem () |
| 9 | 9 | ||
| 10 | -- (id)initWithY:(CGFloat)y; | 10 | +- (id)initWithY:(CGFloat)y andRawY:(CGFloat)rawY; |
| 11 | 11 | ||
| 12 | -@property (readwrite) CGFloat y; // should be within the y range | 12 | +@property (readwrite) CGFloat y; // should be within the y range |
| 13 | +@property (readwrite) CGFloat rawY; // this is the raw value, used for point label. | ||
| 13 | 14 | ||
| 14 | @end | 15 | @end |
| 15 | 16 | ||
| @@ -17,13 +18,18 @@ | @@ -17,13 +18,18 @@ | ||
| 17 | 18 | ||
| 18 | + (PNLineChartDataItem *)dataItemWithY:(CGFloat)y | 19 | + (PNLineChartDataItem *)dataItemWithY:(CGFloat)y |
| 19 | { | 20 | { |
| 20 | - return [[PNLineChartDataItem alloc] initWithY:y]; | 21 | + return [[PNLineChartDataItem alloc] initWithY:y andRawY:y]; |
| 21 | } | 22 | } |
| 22 | 23 | ||
| 23 | -- (id)initWithY:(CGFloat)y | 24 | ++ (PNLineChartDataItem *)dataItemWithY:(CGFloat)y andRawY:(CGFloat)rawY { |
| 25 | + return [[PNLineChartDataItem alloc] initWithY:y andRawY:rawY]; | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +- (id)initWithY:(CGFloat)y andRawY:(CGFloat)rawY | ||
| 24 | { | 29 | { |
| 25 | if ((self = [super init])) { | 30 | if ((self = [super init])) { |
| 26 | self.y = y; | 31 | self.y = y; |
| 32 | + self.rawY = rawY; | ||
| 27 | } | 33 | } |
| 28 | 34 | ||
| 29 | return self; | 35 | return self; |
-
Please register or login to post a comment