Kevin

Merge pull request #54 from jackyzonewen/master

[Mod] : Add Coodinate Axis to PNLineChart
@@ -52,4 +52,15 @@ @@ -52,4 +52,15 @@
52 52
53 @property (nonatomic) BOOL showLabel; 53 @property (nonatomic) BOOL showLabel;
54 54
  55 +
  56 +/**
  57 + * show CoordinateAxis ornot, Default is not
  58 + */
  59 +@property (nonatomic, getter = isShowCoordinateAxis) BOOL showCoordinateAxis;
  60 +@property (nonatomic) UIColor *axisColor;
  61 +@property (nonatomic) CGFloat axisWidth;
  62 +
  63 +@property (nonatomic, strong) NSString *xUnit;
  64 +@property (nonatomic, strong) NSString *yUnit;
  65 +
55 @end 66 @end
@@ -239,7 +239,7 @@ @@ -239,7 +239,7 @@
239 pathAnimation.duration = 1.0; 239 pathAnimation.duration = 1.0;
240 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 240 pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
241 pathAnimation.fromValue = @0.0f; 241 pathAnimation.fromValue = @0.0f;
242 - pathAnimation.toValue = @1.0f; 242 + pathAnimation.toValue = @1.0f;
243 [chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"]; 243 [chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
244 244
245 chartLine.strokeEnd = 1.0; 245 chartLine.strokeEnd = 1.0;
@@ -305,6 +305,53 @@ @@ -305,6 +305,53 @@
305 } 305 }
306 } 306 }
307 307
  308 +#define IOS7_OR_LATER [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0
  309 +
  310 +- (void)drawRect:(CGRect)rect
  311 +{
  312 + if (self.isShowCoordinateAxis) {
  313 +
  314 + CGContextRef ctx = UIGraphicsGetCurrentContext();
  315 + UIGraphicsPushContext(ctx);
  316 + CGContextSetLineWidth(ctx, self.axisWidth);
  317 + CGContextSetStrokeColorWithColor(ctx, [self.axisColor CGColor]);
  318 +
  319 + // draw coordinate axis
  320 + CGContextMoveToPoint(ctx, _chartMargin + 10, 0);
  321 + CGContextAddLineToPoint(ctx, _chartMargin + 10, _chartMargin + _chartCavanHeight);
  322 + CGContextAddLineToPoint(ctx, CGRectGetWidth(rect) - _chartMargin, _chartMargin + _chartCavanHeight);
  323 + CGContextStrokePath(ctx);
  324 +
  325 + // draw y axis arrow
  326 + CGContextMoveToPoint(ctx, _chartMargin + 6, 8);
  327 + CGContextAddLineToPoint(ctx, _chartMargin + 10, 0);
  328 + CGContextAddLineToPoint(ctx, _chartMargin + 14, 8);
  329 + CGContextStrokePath(ctx);
  330 +
  331 + // draw x axis arrow
  332 + CGContextMoveToPoint(ctx, CGRectGetWidth(rect) - _chartMargin - 8, _chartMargin + _chartCavanHeight - 4);
  333 + CGContextAddLineToPoint(ctx, CGRectGetWidth(rect) - _chartMargin, _chartMargin + _chartCavanHeight);
  334 + CGContextAddLineToPoint(ctx, CGRectGetWidth(rect) - _chartMargin - 8, _chartMargin + _chartCavanHeight + 4);
  335 + CGContextStrokePath(ctx);
  336 +
  337 + UIFont *font = [UIFont systemFontOfSize:11];
  338 + // draw y unit
  339 + if ([self.yUnit length]) {
  340 + CGFloat height = [PNLineChart heightOfString:self.yUnit withWidth:30.f font:font];
  341 + CGRect drawRect = CGRectMake(_chartMargin + 10 + 5, 0, 30.f, height);
  342 + [self drawTextInContext:ctx text:self.yUnit inRect:drawRect font:font];
  343 + }
  344 +
  345 + // draw x unit
  346 + if ([self.xUnit length]) {
  347 + CGFloat height = [PNLineChart heightOfString:self.xUnit withWidth:30.f font:font];
  348 + CGRect drawRect = CGRectMake(CGRectGetWidth(rect) - _chartMargin + 5, _chartMargin + _chartCavanHeight - height/2, 25.f, height);
  349 + [self drawTextInContext:ctx text:self.xUnit inRect:drawRect font:font];
  350 + }
  351 + }
  352 +
  353 + [super drawRect:rect];
  354 +}
308 355
309 #pragma mark private methods 356 #pragma mark private methods
310 357
@@ -325,6 +372,58 @@ @@ -325,6 +372,58 @@
325 372
326 _chartCavanWidth = self.frame.size.width - _chartMargin * 2; 373 _chartCavanWidth = self.frame.size.width - _chartMargin * 2;
327 _chartCavanHeight = self.frame.size.height - _chartMargin * 2; 374 _chartCavanHeight = self.frame.size.height - _chartMargin * 2;
  375 +
  376 + // Coordinate Axis Default Values
  377 + _showCoordinateAxis = NO;
  378 + _axisColor = [UIColor colorWithRed:0.4f green:0.4f blue:0.4f alpha:1.f];
  379 + _axisWidth = 1.f;
  380 +}
  381 +
  382 +#pragma mark - tools
  383 +
  384 ++ (float)heightOfString:(NSString *)text withWidth:(float)width font:(UIFont *)font
  385 +{
  386 + NSInteger ch;
  387 + //设置字体
  388 + CGSize size = CGSizeMake(width, MAXFLOAT);
  389 + if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])
  390 + {
  391 + NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
  392 + size =[text boundingRectWithSize:size
  393 + options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
  394 + attributes:tdic
  395 + context:nil].size;
  396 + }
  397 + else
  398 + {
  399 +#pragma clang diagnostic push
  400 +#pragma clang diagnostic ignored "-Wdeprecated-declarations"
  401 + size = [text sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping]; //ios7以上已经摒弃的这个方法
  402 +#pragma clang diagnostic pop
  403 + }
  404 + ch = size.height;
  405 +
  406 + return ch;
  407 +}
  408 +
  409 +- (void)drawTextInContext:(CGContextRef )ctx text:(NSString *)text inRect:(CGRect)rect font:(UIFont *)font
  410 +{
  411 + if (IOS7_OR_LATER) {
  412 + NSMutableParagraphStyle *priceParagraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
  413 + priceParagraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
  414 + priceParagraphStyle.alignment = NSTextAlignmentLeft;
  415 +
  416 + [text drawInRect:rect
  417 + withAttributes:@{NSParagraphStyleAttributeName:priceParagraphStyle, NSFontAttributeName:font}];
  418 + } else {
  419 +#pragma clang diagnostic push
  420 +#pragma clang diagnostic ignored "-Wdeprecated-declarations"
  421 + [text drawInRect:rect
  422 + withFont:font
  423 + lineBreakMode:NSLineBreakByTruncatingTail
  424 + alignment:NSTextAlignmentLeft];
  425 +#pragma clang diagnostic pop
  426 + }
328 } 427 }
329 428
330 429