Orlando Aleman Ortiz

Reformatting

... ... @@ -11,12 +11,9 @@
@interface PNBar : UIView
@property (nonatomic) float grade;
@property (nonatomic,strong) CAShapeLayer * chartLine;
@property (nonatomic, strong) UIColor * barColor;
-(void)rollBack;
- (void)rollBack;
@property (nonatomic) float grade;
@property (nonatomic, strong) CAShapeLayer *chartLine;
@property (nonatomic, strong) UIColor *barColor;
@end
... ...
... ... @@ -14,6 +14,7 @@
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_chartLine = [CAShapeLayer layer];
... ... @@ -25,16 +26,18 @@
[self.layer addSublayer:_chartLine];
self.layer.cornerRadius = 2.0;
}
return self;
}
-(void)setGrade:(float)grade
- (void)setGrade:(float)grade
{
_grade = grade;
UIBezierPath *progressline = [UIBezierPath bezierPath];
[progressline moveToPoint:CGPointMake(self.frame.size.width/2.0, self.frame.size.height)];
[progressline addLineToPoint:CGPointMake(self.frame.size.width/2.0, (1 - grade) * self.frame.size.height)];
[progressline moveToPoint:CGPointMake(self.frame.size.width / 2.0, self.frame.size.height)];
[progressline addLineToPoint:CGPointMake(self.frame.size.width / 2.0, (1 - grade) * self.frame.size.height)];
[progressline setLineWidth:1.0];
[progressline setLineCapStyle:kCGLineCapSquare];
... ... @@ -42,7 +45,8 @@
if (_barColor) {
_chartLine.strokeColor = [_barColor CGColor];
}else{
}
else {
_chartLine.strokeColor = [PNGreen CGColor];
}
... ... @@ -56,13 +60,15 @@
_chartLine.strokeEnd = 1.0;
}
-(void)rollBack{
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
- (void)rollBack
{
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations: ^{
_chartLine.strokeColor = [UIColor clearColor].CGColor;
} completion:nil];
}
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
... ... @@ -72,7 +78,6 @@
CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor);
CGContextFillRect(context, rect);
}
... ...
... ... @@ -20,24 +20,15 @@
* This method will call and stroke the line in animation
*/
-(void)strokeChart;
@property (strong, nonatomic) NSArray * xLabels;
@property (strong, nonatomic) NSArray * yLabels;
@property (strong, nonatomic) NSArray * yValues;
- (void)strokeChart;
@property (strong, nonatomic) NSArray *xLabels;
@property (strong, nonatomic) NSArray *yLabels;
@property (strong, nonatomic) NSArray *yValues;
@property (nonatomic) CGFloat xLabelWidth;
@property (nonatomic) int yValueMax;
@property (nonatomic, strong) UIColor * strokeColor;
@property (nonatomic, strong) NSArray * strokeColors;
@property (nonatomic, strong) UIColor * barBackgroundColor;
@property (nonatomic, strong) UIColor *strokeColor;
@property (nonatomic, strong) NSArray *strokeColors;
@property (nonatomic, strong) UIColor *barBackgroundColor;
@property (nonatomic) BOOL showLabel;
@end
... ...
... ... @@ -11,9 +11,9 @@
#import "PNChartLabel.h"
#import "PNBar.h"
@interface PNBarChart() {
NSMutableArray* _bars;
NSMutableArray* _labels;
@interface PNBarChart () {
NSMutableArray *_bars;
NSMutableArray *_labels;
}
- (UIColor *)barColorAtIndex:(NSUInteger)index;
... ... @@ -24,6 +24,7 @@
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor whiteColor];
... ... @@ -37,23 +38,26 @@
return self;
}
-(void)setYValues:(NSArray *)yValues
- (void)setYValues:(NSArray *)yValues
{
_yValues = yValues;
[self setYLabels:yValues];
_xLabelWidth = (self.frame.size.width - chartMargin*2)/[_yValues count];
_xLabelWidth = (self.frame.size.width - chartMargin * 2) / [_yValues count];
}
-(void)setYLabels:(NSArray *)yLabels
- (void)setYLabels:(NSArray *)yLabels
{
NSInteger max = 0;
for (NSString * valueString in yLabels) {
for (NSString *valueString in yLabels) {
NSInteger value = [valueString integerValue];
if (value > max) {
max = value;
}
}
//Min value for Y label
... ... @@ -64,52 +68,54 @@
_yValueMax = (int)max;
}
-(void)setXLabels:(NSArray *)xLabels
- (void)setXLabels:(NSArray *)xLabels
{
_xLabels = xLabels;
if (_showLabel) {
_xLabelWidth = (self.frame.size.width - chartMargin*2)/[xLabels count];
_xLabelWidth = (self.frame.size.width - chartMargin * 2) / [xLabels count];
}
}
-(void)setStrokeColor:(UIColor *)strokeColor
- (void)setStrokeColor:(UIColor *)strokeColor
{
_strokeColor = strokeColor;
}
-(void)strokeChart
- (void)strokeChart
{
[self viewCleanupForCollection:_labels];
for(int index = 0; index < _xLabels.count; index++)
{
NSString* labelText = _xLabels[index];
PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin), self.frame.size.height - xLabelHeight - chartMargin, _xLabelWidth, xLabelHeight)];
for (int index = 0; index < _xLabels.count; index++) {
NSString *labelText = _xLabels[index];
PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin), self.frame.size.height - xLabelHeight - chartMargin, _xLabelWidth, xLabelHeight)];
[label setTextAlignment:NSTextAlignmentCenter];
label.text = labelText;
[_labels addObject:label];
[self addSubview:label];
}
[self viewCleanupForCollection:_bars];
CGFloat chartCavanHeight = self.frame.size.height - chartMargin*2 - xLabelHeight*2;
CGFloat chartCavanHeight = self.frame.size.height - chartMargin * 2 - xLabelHeight * 2;
NSInteger index = 0;
for (NSString * valueString in _yValues) {
for (NSString *valueString in _yValues) {
float value = [valueString floatValue];
float grade = (float)value / (float)_yValueMax;
PNBar * bar;
PNBar *bar;
if (_showLabel) {
bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight - xLabelHeight - chartMargin, _xLabelWidth * 0.5, chartCavanHeight)];
}
else{
bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight , _xLabelWidth * 0.6, chartCavanHeight)];
else {
bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight, _xLabelWidth * 0.6, chartCavanHeight)];
}
bar.backgroundColor = _barBackgroundColor;
bar.barColor = [self barColorAtIndex:index];
bar.grade = grade;
... ... @@ -120,7 +126,8 @@
}
}
- (void)viewCleanupForCollection:(NSMutableArray*)array
- (void)viewCleanupForCollection:(NSMutableArray *)array
{
if (array.count) {
[array makeObjectsPerformSelector:@selector(removeFromSuperview)];
... ... @@ -128,15 +135,18 @@
}
}
#pragma mark - Class extension methods
- (UIColor *)barColorAtIndex:(NSUInteger)index
{
if ([self.strokeColors count] == [self.yValues count]) {
return self.strokeColors[index];
} else {
}
else {
return self.strokeColor;
}
}
@end
... ...
... ... @@ -14,13 +14,13 @@
* When user click on the chart line
*
*/
- (void)userClickedOnLinePoint:(CGPoint )point lineIndex:(NSInteger)lineIndex;
- (void)userClickedOnLinePoint:(CGPoint)point lineIndex:(NSInteger)lineIndex;
/**
* When user click on the chart line key point
*
*/
- (void)userClickedOnLineKeyPoint:(CGPoint )point lineIndex:(NSInteger)lineIndex andPointIndex:(NSInteger)pointIndex;
- (void)userClickedOnLineKeyPoint:(CGPoint)point lineIndex:(NSInteger)lineIndex andPointIndex:(NSInteger)pointIndex;
@end
... ...
... ... @@ -14,27 +14,20 @@
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setLineBreakMode:NSLineBreakByWordWrapping];
[self setMinimumScaleFactor:11.0f];
[self setNumberOfLines:0];
[self setFont:[UIFont boldSystemFontOfSize:11.0f]];
[self setTextColor: PNDeepGrey];
[self setTextColor:PNDeepGrey];
self.backgroundColor = [UIColor clearColor];
[self setTextAlignment:NSTextAlignmentLeft];
self.userInteractionEnabled = YES;
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
return self;
}
*/
@end
... ...
... ... @@ -14,17 +14,17 @@
@interface PNCircleChart : UIView
-(void)strokeChart;
- (void)strokeChart;
- (id)initWithFrame:(CGRect)frame andTotal:(NSNumber *)total andCurrent:(NSNumber *)current andClockwise:(BOOL)clockwise;
@property (nonatomic, strong) UIColor *strokeColor;
@property (nonatomic, strong) UIColor *labelColor;
@property (nonatomic, strong) NSNumber * total;
@property (nonatomic, strong) NSNumber * current;
@property (nonatomic, strong) NSNumber * lineWidth;
@property (nonatomic, strong) NSNumber *total;
@property (nonatomic, strong) NSNumber *current;
@property (nonatomic, strong) NSNumber *lineWidth;
@property (nonatomic) BOOL clockwise;
@property(nonatomic,strong) CAShapeLayer * circle;
@property(nonatomic,strong) CAShapeLayer * circleBG;
@property (nonatomic, strong) CAShapeLayer *circle;
@property (nonatomic, strong) CAShapeLayer *circleBG;
@end
... ...
... ... @@ -22,11 +22,13 @@
if (!_labelColor) {
_labelColor = PNDeepGrey;
}
return _labelColor;
}
- (id)initWithFrame:(CGRect)frame andTotal:(NSNumber *)total andCurrent:(NSNumber *)current andClockwise:(BOOL)clockwise {
- (id)initWithFrame:(CGRect)frame andTotal:(NSNumber *)total andCurrent:(NSNumber *)current andClockwise:(BOOL)clockwise
{
self = [super initWithFrame:frame];
if (self) {
... ... @@ -39,7 +41,7 @@
CGFloat endAngle = clockwise ? -90.01f : 270.01f;
_lineWidth = [NSNumber numberWithFloat:8.0];
UIBezierPath* circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.center.x,self.center.y) radius:self.frame.size.height*0.5 startAngle:DEGREES_TO_RADIANS(startAngle) endAngle:DEGREES_TO_RADIANS(endAngle) clockwise:clockwise];
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.center.x, self.center.y) radius:self.frame.size.height * 0.5 startAngle:DEGREES_TO_RADIANS(startAngle) endAngle:DEGREES_TO_RADIANS(endAngle) clockwise:clockwise];
_circle = [CAShapeLayer layer];
_circle.path = circlePath.CGPath;
... ... @@ -61,21 +63,20 @@
[self.layer addSublayer:_circleBG];
_gradeLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(0, 0, 50.0, 50.0)];
}
return self;
}
-(void)strokeChart
- (void)strokeChart
{
//Add count label
[_gradeLabel setTextAlignment:NSTextAlignmentCenter];
[_gradeLabel setFont:[UIFont boldSystemFontOfSize:13.0f]];
[_gradeLabel setTextColor:self.labelColor];
[_gradeLabel setCenter:CGPointMake(self.center.x,self.center.y)];
[_gradeLabel setCenter:CGPointMake(self.center.x, self.center.y)];
_gradeLabel.method = UILabelCountingMethodEaseInOut;
_gradeLabel.format = @"%d%%";
... ... @@ -94,12 +95,12 @@
pathAnimation.duration = 1.0;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:[_current floatValue]/[_total floatValue]];
pathAnimation.toValue = [NSNumber numberWithFloat:[_current floatValue] / [_total floatValue]];
[_circle addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
_circle.strokeEnd = [_current floatValue]/[_total floatValue];
[_gradeLabel countFrom:0 to:[_current floatValue]/[_total floatValue]*100 withDuration:1.0];
_circle.strokeEnd = [_current floatValue] / [_total floatValue];
[_gradeLabel countFrom:0 to:[_current floatValue] / [_total floatValue] * 100 withDuration:1.0];
}
@end
... ...
... ... @@ -7,45 +7,44 @@
//
#import <Foundation/Foundation.h>
/*
* System Versioning Preprocessor Macros
*/
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define PNGrey [UIColor colorWithRed:246.0/255.0 green:246.0/255.0 blue:246.0/255.0 alpha:1.0f]
#define PNLightBlue [UIColor colorWithRed:94.0/255.0 green:147.0/255.0 blue:196.0/255.0 alpha:1.0f]
#define PNGreen [UIColor colorWithRed:77.0/255.0 green:186.0/255.0 blue:122.0/255.0 alpha:1.0f]
#define PNTitleColor [UIColor colorWithRed:0.0/255.0 green:189.0/255.0 blue:113.0/255.0 alpha:1.0f]
#define PNButtonGrey [UIColor colorWithRed:141.0/255.0 green:141.0/255.0 blue:141.0/255.0 alpha:1.0f]
#define PNFreshGreen [UIColor colorWithRed:77.0/255.0 green:196.0/255.0 blue:122.0/255.0 alpha:1.0f]
#define PNRed [UIColor colorWithRed:245.0/255.0 green:94.0/255.0 blue:78.0/255.0 alpha:1.0f]
#define PNMauve [UIColor colorWithRed:88.0/255.0 green:75.0/255.0 blue:103.0/255.0 alpha:1.0f]
#define PNBrown [UIColor colorWithRed:119.0/255.0 green:107.0/255.0 blue:95.0/255.0 alpha:1.0f]
#define PNBlue [UIColor colorWithRed:82.0/255.0 green:116.0/255.0 blue:188.0/255.0 alpha:1.0f]
#define PNDarkBlue [UIColor colorWithRed:121.0/255.0 green:134.0/255.0 blue:142.0/255.0 alpha:1.0f]
#define PNYellow [UIColor colorWithRed:242.0/255.0 green:197.0/255.0 blue:117.0/255.0 alpha:1.0f]
#define PNWhite [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0f]
#define PNDeepGrey [UIColor colorWithRed:99.0/255.0 green:99.0/255.0 blue:99.0/255.0 alpha:1.0f]
#define PNPinkGrey [UIColor colorWithRed:200.0/255.0 green:193.0/255.0 blue:193.0/255.0 alpha:1.0f]
#define PNHealYellow [UIColor colorWithRed:245.0/255.0 green:242.0/255.0 blue:238.0/255.0 alpha:1.0f]
#define PNLightGrey [UIColor colorWithRed:225.0/255.0 green:225.0/255.0 blue:225.0/255.0 alpha:1.0f]
#define PNCleanGrey [UIColor colorWithRed:251.0/255.0 green:251.0/255.0 blue:251.0/255.0 alpha:1.0f]
#define PNLightYellow [UIColor colorWithRed:241.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0f]
#define PNDarkYellow [UIColor colorWithRed:152.0/255.0 green:150.0/255.0 blue:159.0/255.0 alpha:1.0f]
#define PNPinkDark [UIColor colorWithRed:170.0/255.0 green:165.0/255.0 blue:165.0/255.0 alpha:1.0f]
#define PNCloudWhite [UIColor colorWithRed:244.0/255.0 green:244.0/255.0 blue:244.0/255.0 alpha:1.0f]
#define PNBlack [UIColor colorWithRed:45.0/255.0 green:45.0/255.0 blue:45.0/255.0 alpha:1.0f]
#define PNStarYellow [UIColor colorWithRed:252.0/255.0 green:223.0/255.0 blue:101.0/255.0 alpha:1.0f]
#define PNTwitterColor [UIColor colorWithRed:0.0/255.0 green:171.0/255.0 blue:243.0/255.0 alpha:1.0]
#define PNWeiboColor [UIColor colorWithRed:250.0/255.0 green:0.0/255.0 blue:33.0/255.0 alpha:1.0]
#define PNiOSGreenColor [UIColor colorWithRed:98.0/255.0 green:247.0/255.0 blue:77.0/255.0 alpha:1.0]
#define PNGrey [UIColor colorWithRed:246.0 / 255.0 green:246.0 / 255.0 blue:246.0 / 255.0 alpha:1.0f]
#define PNLightBlue [UIColor colorWithRed:94.0 / 255.0 green:147.0 / 255.0 blue:196.0 / 255.0 alpha:1.0f]
#define PNGreen [UIColor colorWithRed:77.0 / 255.0 green:186.0 / 255.0 blue:122.0 / 255.0 alpha:1.0f]
#define PNTitleColor [UIColor colorWithRed:0.0 / 255.0 green:189.0 / 255.0 blue:113.0 / 255.0 alpha:1.0f]
#define PNButtonGrey [UIColor colorWithRed:141.0 / 255.0 green:141.0 / 255.0 blue:141.0 / 255.0 alpha:1.0f]
#define PNFreshGreen [UIColor colorWithRed:77.0 / 255.0 green:196.0 / 255.0 blue:122.0 / 255.0 alpha:1.0f]
#define PNRed [UIColor colorWithRed:245.0 / 255.0 green:94.0 / 255.0 blue:78.0 / 255.0 alpha:1.0f]
#define PNMauve [UIColor colorWithRed:88.0 / 255.0 green:75.0 / 255.0 blue:103.0 / 255.0 alpha:1.0f]
#define PNBrown [UIColor colorWithRed:119.0 / 255.0 green:107.0 / 255.0 blue:95.0 / 255.0 alpha:1.0f]
#define PNBlue [UIColor colorWithRed:82.0 / 255.0 green:116.0 / 255.0 blue:188.0 / 255.0 alpha:1.0f]
#define PNDarkBlue [UIColor colorWithRed:121.0 / 255.0 green:134.0 / 255.0 blue:142.0 / 255.0 alpha:1.0f]
#define PNYellow [UIColor colorWithRed:242.0 / 255.0 green:197.0 / 255.0 blue:117.0 / 255.0 alpha:1.0f]
#define PNWhite [UIColor colorWithRed:255.0 / 255.0 green:255.0 / 255.0 blue:255.0 / 255.0 alpha:1.0f]
#define PNDeepGrey [UIColor colorWithRed:99.0 / 255.0 green:99.0 / 255.0 blue:99.0 / 255.0 alpha:1.0f]
#define PNPinkGrey [UIColor colorWithRed:200.0 / 255.0 green:193.0 / 255.0 blue:193.0 / 255.0 alpha:1.0f]
#define PNHealYellow [UIColor colorWithRed:245.0 / 255.0 green:242.0 / 255.0 blue:238.0 / 255.0 alpha:1.0f]
#define PNLightGrey [UIColor colorWithRed:225.0 / 255.0 green:225.0 / 255.0 blue:225.0 / 255.0 alpha:1.0f]
#define PNCleanGrey [UIColor colorWithRed:251.0 / 255.0 green:251.0 / 255.0 blue:251.0 / 255.0 alpha:1.0f]
#define PNLightYellow [UIColor colorWithRed:241.0 / 255.0 green:240.0 / 255.0 blue:240.0 / 255.0 alpha:1.0f]
#define PNDarkYellow [UIColor colorWithRed:152.0 / 255.0 green:150.0 / 255.0 blue:159.0 / 255.0 alpha:1.0f]
#define PNPinkDark [UIColor colorWithRed:170.0 / 255.0 green:165.0 / 255.0 blue:165.0 / 255.0 alpha:1.0f]
#define PNCloudWhite [UIColor colorWithRed:244.0 / 255.0 green:244.0 / 255.0 blue:244.0 / 255.0 alpha:1.0f]
#define PNBlack [UIColor colorWithRed:45.0 / 255.0 green:45.0 / 255.0 blue:45.0 / 255.0 alpha:1.0f]
#define PNStarYellow [UIColor colorWithRed:252.0 / 255.0 green:223.0 / 255.0 blue:101.0 / 255.0 alpha:1.0f]
#define PNTwitterColor [UIColor colorWithRed:0.0 / 255.0 green:171.0 / 255.0 blue:243.0 / 255.0 alpha:1.0]
#define PNWeiboColor [UIColor colorWithRed:250.0 / 255.0 green:0.0 / 255.0 blue:33.0 / 255.0 alpha:1.0]
#define PNiOSGreenColor [UIColor colorWithRed:98.0 / 255.0 green:247.0 / 255.0 blue:77.0 / 255.0 alpha:1.0]
@interface PNColor : NSObject
- (UIImage *)imageFromColor:(UIColor *)color;
@end
... ...
... ... @@ -10,19 +10,10 @@
@implementation PNColor
-(id)init{
if (self = [super init]) {
}
return self;
}
-(UIImage *)imageFromColor:(UIColor*)color
- (UIImage *)imageFromColor:(UIColor *)color
{
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,
... ... @@ -35,6 +26,4 @@
}
@end
... ...
... ... @@ -6,14 +6,11 @@
// Copyright (c) 2013年 kevinzhow. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "PNChartDelegate.h"
@interface PNLineChart : UIView
/**
... ... @@ -22,18 +19,18 @@
- (void)strokeChart;
@property(nonatomic,retain) id<PNChartDelegate> delegate;
@property (nonatomic, retain) id<PNChartDelegate> delegate;
@property (strong, nonatomic) NSArray * xLabels;
@property (strong, nonatomic) NSArray *xLabels;
@property (strong, nonatomic) NSArray * yLabels;
@property (strong, nonatomic) NSArray *yLabels;
/**
* Array of `LineChartData` objects, one for each line.
*/
@property (strong, nonatomic) NSArray *chartData;
@property (strong, nonatomic) NSMutableArray * pathPoints;
@property (strong, nonatomic) NSMutableArray *pathPoints;
@property (nonatomic) CGFloat xLabelWidth;
... ...
... ... @@ -18,7 +18,7 @@
//------------------------------------------------------------------------------------------------
@interface PNLineChart ()
@property (nonatomic,strong) NSMutableArray *chartLineArray; // Array[CAShapeLayer]
@property (nonatomic, strong) NSMutableArray *chartLineArray; // Array[CAShapeLayer]
@property (strong, nonatomic) NSMutableArray *chartPath; //Array of line path, one for each line.
... ... @@ -34,79 +34,89 @@
#pragma mark initialization
- (id)initWithCoder:(NSCoder *)coder {
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self setDefaultValues];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setDefaultValues];
}
return self;
}
#pragma mark instance methods
-(void)setYLabels:(NSArray *)yLabels
- (void)setYLabels:(NSArray *)yLabels
{
CGFloat yStep = (_yValueMax-_yValueMin) / _yLabelNum;
CGFloat yStep = (_yValueMax - _yValueMin) / _yLabelNum;
CGFloat yStepHeight = _chartCavanHeight / _yLabelNum;
NSInteger index = 0;
NSInteger num = _yLabelNum+1;
NSInteger num = _yLabelNum + 1;
while (num > 0) {
PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (_chartCavanHeight - index * yStepHeight), _chartMargin, _yLabelHeight)];
PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (_chartCavanHeight - index * yStepHeight), _chartMargin, _yLabelHeight)];
[label setTextAlignment:NSTextAlignmentRight];
label.text = [NSString stringWithFormat:@"%1.f",_yValueMin + (yStep * index)];
label.text = [NSString stringWithFormat:@"%1.f", _yValueMin + (yStep * index)];
[self addSubview:label];
index +=1 ;
index += 1;
num -= 1;
}
}
-(void)setXLabels:(NSArray *)xLabels
- (void)setXLabels:(NSArray *)xLabels
{
_xLabels = xLabels;
NSString* labelText;
if(_showLabel){
_xLabelWidth = _chartCavanWidth/[xLabels count];
NSString *labelText;
if (_showLabel) {
_xLabelWidth = _chartCavanWidth / [xLabels count];
for(int index = 0; index < xLabels.count; index++)
{
for (int index = 0; index < xLabels.count; index++) {
labelText = xLabels[index];
PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(2*_chartMargin + (index * _xLabelWidth) - (_xLabelWidth / 2), _chartMargin + _chartCavanHeight, _xLabelWidth, _chartMargin)];
PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake(2 * _chartMargin + (index * _xLabelWidth) - (_xLabelWidth / 2), _chartMargin + _chartCavanHeight, _xLabelWidth, _chartMargin)];
[label setTextAlignment:NSTextAlignmentCenter];
label.text = labelText;
[self addSubview:label];
}
}else{
_xLabelWidth = (self.frame.size.width)/[xLabels count];
}
else {
_xLabelWidth = (self.frame.size.width) / [xLabels count];
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchPoint:touches withEvent:event];
[self touchKeyPoint:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchPoint:touches withEvent:event];
[self touchKeyPoint:touches withEvent:event];
}
-(void)touchPoint:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchPoint:(NSSet *)touches withEvent:(UIEvent *)event
{
//Get the point user touched
UITouch *touch = [touches anyObject];
... ... @@ -117,11 +127,11 @@
for (int i = 0; i < linePointsArray.count - 1; i += 1) {
CGPoint p1 = [linePointsArray[i] CGPointValue];
CGPoint p2 = [linePointsArray[i+1] CGPointValue];
CGPoint p2 = [linePointsArray[i + 1] CGPointValue];
// Closest distance from point to line
float distance = fabsf(((p2.x - p1.x)*(touchPoint.y - p1.y))-((p1.x-touchPoint.x)*(p1.y-p2.y)));
distance /= hypot(p2.x-p1.x, p1.y-p2.y);
float distance = fabsf(((p2.x - p1.x) * (touchPoint.y - p1.y)) - ((p1.x - touchPoint.x) * (p1.y - p2.y)));
distance /= hypot(p2.x - p1.x, p1.y - p2.y);
if (distance <= 5.0) {
// Conform to delegate parameters, figure out what bezier path this CGPoint belongs to.
... ... @@ -138,7 +148,8 @@
}
}
-(void)touchKeyPoint:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchKeyPoint:(NSSet *)touches withEvent:(UIEvent *)event
{
//Get the point user touched
UITouch *touch = [touches anyObject];
... ... @@ -149,7 +160,7 @@
for (int i = 0; i < linePointsArray.count - 1; i += 1) {
CGPoint p1 = [linePointsArray[i] CGPointValue];
CGPoint p2 = [linePointsArray[i+1] CGPointValue];
CGPoint p2 = [linePointsArray[i + 1] CGPointValue];
float distanceToP1 = fabsf(hypot(touchPoint.x - p1.x, touchPoint.y - p1.y));
float distanceToP2 = hypot(touchPoint.x - p2.x, touchPoint.y - p2.y);
... ... @@ -159,52 +170,48 @@
if (distance <= 10.0) {
[_delegate userClickedOnLineKeyPoint:touchPoint
lineIndex:p
andPointIndex:(distance == distanceToP2 ? i+1 : i)];
andPointIndex:(distance == distanceToP2 ? i + 1 : i)];
return;
}
}
}
}
-(void)strokeChart
- (void)strokeChart
{
_chartPath = [[NSMutableArray alloc] init];
//Draw each line
for (NSUInteger lineIndex = 0; lineIndex < self.chartData.count; lineIndex++) {
PNLineChartData *chartData = self.chartData[lineIndex];
CAShapeLayer *chartLine = (CAShapeLayer *) self.chartLineArray[lineIndex];
CAShapeLayer *chartLine = (CAShapeLayer *)self.chartLineArray[lineIndex];
CGFloat yValue;
CGFloat innerGrade;
CGPoint point;
UIGraphicsBeginImageContext(self.frame.size);
UIBezierPath * progressline = [UIBezierPath bezierPath];
UIBezierPath *progressline = [UIBezierPath bezierPath];
[_chartPath addObject:progressline];
if(!_showLabel){
_chartCavanHeight = self.frame.size.height - 2*_yLabelHeight;
if (!_showLabel) {
_chartCavanHeight = self.frame.size.height - 2 * _yLabelHeight;
_chartCavanWidth = self.frame.size.width;
_chartMargin = 0.0;
_xLabelWidth = (_chartCavanWidth / ([_xLabels count] -1));
_xLabelWidth = (_chartCavanWidth / ([_xLabels count] - 1));
}
NSMutableArray * linePointsArray = [[NSMutableArray alloc] init];
NSMutableArray *linePointsArray = [[NSMutableArray alloc] init];
[progressline setLineWidth:3.0];
[progressline setLineCapStyle:kCGLineCapRound];
[progressline setLineJoinStyle:kCGLineJoinRound];
for (NSUInteger i = 0; i < chartData.itemCount; i++) {
yValue = chartData.getData(i).y;
innerGrade = (yValue - _yValueMin) / ( _yValueMax - _yValueMin);
innerGrade = (yValue - _yValueMin) / (_yValueMax - _yValueMin);
point = CGPointMake(2*_chartMargin + (i * _xLabelWidth), _chartCavanHeight - (innerGrade * _chartCavanHeight) + ( _yLabelHeight /2 ));
point = CGPointMake(2 * _chartMargin + (i * _xLabelWidth), _chartCavanHeight - (innerGrade * _chartCavanHeight) + (_yLabelHeight / 2));
if (i != 0) {
[progressline addLineToPoint:point];
... ... @@ -213,11 +220,14 @@
[progressline moveToPoint:point];
[linePointsArray addObject:[NSValue valueWithCGPoint:point]];
}
[_pathPoints addObject:[linePointsArray copy]];
// setup the color of the chart line
if (chartData.color) {
chartLine.strokeColor = [chartData.color CGColor];
}else{
}
else {
chartLine.strokeColor = [PNGreen CGColor];
}
... ... @@ -238,21 +248,23 @@
}
}
- (void)setChartData:(NSArray *)data {
if (data != _chartData) {
- (void)setChartData:(NSArray *)data
{
if (data != _chartData) {
NSMutableArray *yLabelsArray = [NSMutableArray arrayWithCapacity:data.count];
CGFloat yMax = 0.0f;
CGFloat yMin = MAXFLOAT;
CGFloat yValue;
// remove all shape layers before adding new ones
for (CALayer *layer in self.chartLineArray) {
[layer removeFromSuperlayer];
}
self.chartLineArray = [NSMutableArray arrayWithCapacity:data.count];
for (PNLineChartData *chartData in data) {
// create as many chart line layers as there are data-lines
CAShapeLayer *chartLine = [CAShapeLayer layer];
chartLine.lineCap = kCALineCapRound;
... ... @@ -275,7 +287,8 @@
if (yMax < 5) {
yMax = 5.0f;
}
if (yMin < 0){
if (yMin < 0) {
yMin = 0.0f;
}
... ... @@ -292,9 +305,11 @@
}
}
#pragma mark private methods
- (void)setDefaultValues {
- (void)setDefaultValues
{
// Initialization code
self.backgroundColor = [UIColor whiteColor];
self.clipsToBounds = YES;
... ... @@ -308,8 +323,9 @@
_chartMargin = 30;
_chartCavanWidth = self.frame.size.width - _chartMargin *2;
_chartCavanWidth = self.frame.size.width - _chartMargin * 2;
_chartCavanHeight = self.frame.size.height - _chartMargin * 2;
}
@end
... ...
... ... @@ -7,18 +7,12 @@
@class PNLineChartDataItem;
typedef PNLineChartDataItem *(^LCLineChartDataGetter)(NSUInteger item);
/**
*
*/
@interface PNLineChartData : NSObject
@property (strong) UIColor *color;
@property NSUInteger itemCount;
@property (copy) LCLineChartDataGetter getData;
@end
... ...
... ... @@ -5,10 +5,10 @@
#import <Foundation/Foundation.h>
@interface PNLineChartDataItem : NSObject
@property (readonly) CGFloat y; // should be within the y range
@interface PNLineChartDataItem : NSObject
+ (PNLineChartDataItem *)dataItemWithY:(CGFloat)y;
@property (readonly) CGFloat y; // should be within the y range
@end
... ...
... ... @@ -5,30 +5,35 @@
#import "PNLineChartDataItem.h"
//------------------------------------------------------------------------------------------------
// private interface declaration
//------------------------------------------------------------------------------------------------
@interface PNLineChartDataItem ()
- (id)initWithY:(CGFloat)y;
@property (readwrite) CGFloat y; // should be within the y range
@end
- (id)initWithY:(CGFloat)y;
@end
//------------------------------------------------------------------------------------------------
// public interface implementation
//------------------------------------------------------------------------------------------------
@implementation PNLineChartDataItem
+ (PNLineChartDataItem *)dataItemWithY:(CGFloat)y {
+ (PNLineChartDataItem *)dataItemWithY:(CGFloat)y
{
return [[PNLineChartDataItem alloc] initWithY:y];
}
- (id)initWithY:(CGFloat)y {
- (id)initWithY:(CGFloat)y
{
if ((self = [super init])) {
self.y = y;
}
return self;
}
... ...