kevinzhow

Lots of new API for Bar Chart

1 -//  
2 -// PNBarChart.h  
3 -// PNChartDemo  
4 -//  
5 -// Created by kevin on 11/7/13.  
6 -// Copyright (c) 2013年 kevinzhow. All rights reserved.  
7 -//  
8 -  
9 -#import <UIKit/UIKit.h>  
10 -#import "PNChartDelegate.h"  
11 -  
12 -#define chartMargin 10  
13 -#define xLabelMargin 15  
14 -#define yLabelMargin 15  
15 -#define yLabelHeight 11  
16 -#define xLabelHeight 20  
17 -  
18 -@interface PNBarChart : UIView  
19 -  
20 -/**  
21 - * This method will call and stroke the line in animation  
22 - */  
23 -  
24 -- (void)strokeChart;  
25 -  
26 -@property (nonatomic) NSArray *xLabels;  
27 -@property (nonatomic) NSArray *yLabels;  
28 -@property (nonatomic) NSArray *yValues;  
29 -@property (nonatomic) CGFloat xLabelWidth;  
30 -@property (nonatomic) int yValueMax;  
31 -@property (nonatomic) UIColor *strokeColor;  
32 -@property (nonatomic) NSArray *strokeColors;  
33 -@property (nonatomic) UIColor *barBackgroundColor;  
34 -@property (nonatomic) BOOL showLabel;  
35 -  
36 -@property (nonatomic, retain) id<PNChartDelegate> delegate;  
37 -@end  
1 -//  
2 -// PNBarChart.m  
3 -// PNChartDemo  
4 -//  
5 -// Created by kevin on 11/7/13.  
6 -// Copyright (c) 2013年 kevinzhow. All rights reserved.  
7 -//  
8 -  
9 -#import "PNBarChart.h"  
10 -#import "PNColor.h"  
11 -#import "PNChartLabel.h"  
12 -#import "PNBar.h"  
13 -  
14 -@interface PNBarChart () {  
15 - NSMutableArray *_bars;  
16 - NSMutableArray *_labels;  
17 -}  
18 -  
19 -- (UIColor *)barColorAtIndex:(NSUInteger)index;  
20 -@end  
21 -  
22 -@implementation PNBarChart  
23 -  
24 -- (id)initWithFrame:(CGRect)frame  
25 -{  
26 - self = [super initWithFrame:frame];  
27 -  
28 - if (self) {  
29 - // Initialization code  
30 - self.backgroundColor = [UIColor whiteColor];  
31 - self.clipsToBounds = YES;  
32 - _showLabel = YES;  
33 - _barBackgroundColor = PNLightGrey;  
34 - _labels = [NSMutableArray array];  
35 - _bars = [NSMutableArray array];  
36 - }  
37 -  
38 - return self;  
39 -}  
40 -  
41 -  
42 -- (void)setYValues:(NSArray *)yValues  
43 -{  
44 - _yValues = yValues;  
45 - [self setYLabels:yValues];  
46 -  
47 - _xLabelWidth = (self.frame.size.width - chartMargin * 2) / [_yValues count];  
48 -}  
49 -  
50 -  
51 -- (void)setYLabels:(NSArray *)yLabels  
52 -{  
53 - NSInteger max = 0;  
54 -  
55 - for (NSString *valueString in yLabels) {  
56 - NSInteger value = [valueString integerValue];  
57 -  
58 - if (value > max) {  
59 - max = value;  
60 - }  
61 - }  
62 -  
63 - //Min value for Y label  
64 - if (max < 5) {  
65 - max = 5;  
66 - }  
67 -  
68 - _yValueMax = (int)max;  
69 -}  
70 -  
71 -  
72 -- (void)setXLabels:(NSArray *)xLabels  
73 -{  
74 - _xLabels = xLabels;  
75 -  
76 - if (_showLabel) {  
77 - _xLabelWidth = (self.frame.size.width - chartMargin * 2) / [xLabels count];  
78 - }  
79 -}  
80 -  
81 -  
82 -- (void)setStrokeColor:(UIColor *)strokeColor  
83 -{  
84 - _strokeColor = strokeColor;  
85 -}  
86 -  
87 -  
88 -- (void)strokeChart  
89 -{  
90 - [self viewCleanupForCollection:_labels];  
91 -  
92 - for (int index = 0; index < _xLabels.count; index++) {  
93 - NSString *labelText = _xLabels[index];  
94 - PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin), self.frame.size.height - xLabelHeight - chartMargin, _xLabelWidth, xLabelHeight)];  
95 - [label setTextAlignment:NSTextAlignmentCenter];  
96 - label.text = labelText;  
97 - [_labels addObject:label];  
98 - [self addSubview:label];  
99 - }  
100 -  
101 - [self viewCleanupForCollection:_bars];  
102 -  
103 - CGFloat chartCavanHeight = self.frame.size.height - chartMargin * 2 - xLabelHeight * 2;  
104 - NSInteger index = 0;  
105 -  
106 - for (NSString *valueString in _yValues) {  
107 - float value = [valueString floatValue];  
108 -  
109 - float grade = (float)value / (float)_yValueMax;  
110 - PNBar *bar;  
111 -  
112 - if (_showLabel) {  
113 - bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight - xLabelHeight - chartMargin, _xLabelWidth * 0.5, chartCavanHeight)];  
114 - }  
115 - else {  
116 - bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight, _xLabelWidth * 0.6, chartCavanHeight)];  
117 - }  
118 -  
119 - bar.backgroundColor = _barBackgroundColor;  
120 - bar.barColor = [self barColorAtIndex:index];  
121 - bar.grade = grade;  
122 - bar.tag = index;  
123 - [_bars addObject:bar];  
124 - [self addSubview:bar];  
125 -  
126 - index += 1;  
127 - }  
128 -}  
129 -  
130 -  
131 -- (void)viewCleanupForCollection:(NSMutableArray *)array  
132 -{  
133 - if (array.count) {  
134 - [array makeObjectsPerformSelector:@selector(removeFromSuperview)];  
135 - [array removeAllObjects];  
136 - }  
137 -}  
138 -  
139 -  
140 -#pragma mark - Class extension methods  
141 -  
142 -- (UIColor *)barColorAtIndex:(NSUInteger)index  
143 -{  
144 - if ([self.strokeColors count] == [self.yValues count]) {  
145 - return self.strokeColors[index];  
146 - }  
147 - else {  
148 - return self.strokeColor;  
149 - }  
150 -}  
151 -  
152 -  
153 -#pragma mark - Touch detection  
154 -  
155 -- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
156 -{  
157 - [self touchPoint:touches withEvent:event];  
158 - [super touchesBegan:touches withEvent:event];  
159 -}  
160 -  
161 -  
162 -- (void)touchPoint:(NSSet *)touches withEvent:(UIEvent *)event  
163 -{  
164 - //Get the point user touched  
165 - UITouch *touch = [touches anyObject];  
166 - CGPoint touchPoint = [touch locationInView:self];  
167 - UIView *subview = [self hitTest:touchPoint withEvent:nil];  
168 -  
169 - if ([subview isKindOfClass:[PNBar class]] && [self.delegate respondsToSelector:@selector(userClickedOnBarCharIndex:)]) {  
170 - [self.delegate userClickedOnBarCharIndex:subview.tag];  
171 - }  
172 -}  
173 -  
174 -  
175 -@end  
@@ -16,4 +16,6 @@ @@ -16,4 +16,6 @@
16 @property (nonatomic) float grade; 16 @property (nonatomic) float grade;
17 @property (nonatomic) CAShapeLayer *chartLine; 17 @property (nonatomic) CAShapeLayer *chartLine;
18 @property (nonatomic) UIColor *barColor; 18 @property (nonatomic) UIColor *barColor;
  19 +@property (nonatomic) CGFloat barRadius;
  20 +
19 @end 21 @end
@@ -24,12 +24,18 @@ @@ -24,12 +24,18 @@
24 _chartLine.strokeEnd = 0.0; 24 _chartLine.strokeEnd = 0.0;
25 self.clipsToBounds = YES; 25 self.clipsToBounds = YES;
26 [self.layer addSublayer:_chartLine]; 26 [self.layer addSublayer:_chartLine];
27 - self.layer.cornerRadius = 2.0; 27 + self.barRadius = 2.0;
28 } 28 }
29 29
30 return self; 30 return self;
31 } 31 }
32 32
  33 +-(void)setBarRadius:(CGFloat)barRadius
  34 +{
  35 + _barRadius = barRadius;
  36 + self.layer.cornerRadius = _barRadius;
  37 +}
  38 +
33 39
34 - (void)setGrade:(float)grade 40 - (void)setGrade:(float)grade
35 { 41 {
  1 +//
  2 +// PNBarChart.h
  3 +// PNChartDemo
  4 +//
  5 +// Created by kevin on 11/7/13.
  6 +// Copyright (c) 2013年 kevinzhow. All rights reserved.
  7 +//
  8 +
  9 +#import <UIKit/UIKit.h>
  10 +#import "PNChartDelegate.h"
  11 +
  12 +#define xLabelMargin 15
  13 +#define yLabelMargin 15
  14 +#define yLabelHeight 11
  15 +#define xLabelHeight 20
  16 +
  17 +typedef NSString *(^PNyLabelFromatter)(CGFloat yLabelValue);
  18 +
  19 +@interface PNBarChart : UIView
  20 +
  21 +/**
  22 + * This method will call and stroke the line in animation
  23 + */
  24 +
  25 +- (void)strokeChart;
  26 +
  27 +@property (nonatomic) NSArray *xLabels;
  28 +@property (nonatomic) NSArray *yLabels;
  29 +@property (nonatomic) NSArray *yValues;
  30 +@property (nonatomic) CGFloat xLabelWidth;
  31 +@property (nonatomic) int yValueMax;
  32 +@property (nonatomic) UIColor *strokeColor;
  33 +@property (nonatomic) NSArray *strokeColors;
  34 +
  35 +
  36 +/*
  37 + chartMargin changes chart margin
  38 + */
  39 +@property (nonatomic) CGFloat yChartLabelWidth;
  40 +
  41 +/*
  42 + yLabelFormatter will format the ylabel text
  43 + */
  44 +
  45 +@property (copy) PNyLabelFromatter yLabelFormatter;
  46 +
  47 +/*
  48 + chartMargin changes chart margin
  49 + */
  50 +@property (nonatomic) CGFloat chartMargin;
  51 +
  52 +/*
  53 + showLabelDefines if the Labels should be deplay
  54 + */
  55 +@property (nonatomic) BOOL showLabel;
  56 +
  57 +/*
  58 + showChartBorder if the chart border Line should be deplay
  59 + */
  60 +@property (nonatomic) BOOL showChartBorder;
  61 +
  62 +/*
  63 + chartBottomLine the Line at the chart bottom
  64 + */
  65 +@property (nonatomic) CAShapeLayer * chartBottomLine;
  66 +
  67 +/*
  68 + chartLeftLine the Line at the chart left
  69 + */
  70 +@property (nonatomic) CAShapeLayer * chartLeftLine;
  71 +
  72 +/*
  73 + barRadius changes the bar corner radius
  74 + */
  75 +@property (nonatomic) CGFloat barRadius;
  76 +
  77 +/*
  78 + barWidth changes the width of the bar
  79 + */
  80 +@property (nonatomic) CGFloat barWidth;
  81 +
  82 +/*
  83 + labelMarginTop changes the width of the bar
  84 + */
  85 +@property (nonatomic) CGFloat labelMarginTop;
  86 +
  87 +/*
  88 + barBackgroundColor changes the bar background color
  89 + */
  90 +@property (nonatomic) UIColor * barBackgroundColor;
  91 +
  92 +/*
  93 + labelTextColor changes the bar label text color
  94 + */
  95 +@property (nonatomic) UIColor * labelTextColor;
  96 +
  97 +/*
  98 + labelFont changes the bar label font
  99 + */
  100 +@property (nonatomic) UIFont * labelFont;
  101 +
  102 +/*
  103 + xLabelSkip define the label skip number
  104 + */
  105 +@property (nonatomic) NSInteger xLabelSkip;
  106 +
  107 +/*
  108 + yLabelSum define the label skip number
  109 + */
  110 +@property (nonatomic) NSInteger yLabelSum;
  111 +
  112 +/*
  113 + yMaxValue define the max value of the chart
  114 + */
  115 +@property (nonatomic) CGFloat yMaxValue;
  116 +
  117 +@property (nonatomic, retain) id<PNChartDelegate> delegate;
  118 +
  119 +@end
This diff is collapsed. Click to expand it.
@@ -17,13 +17,10 @@ @@ -17,13 +17,10 @@
17 17
18 if (self) { 18 if (self) {
19 // Initialization code 19 // Initialization code
20 - [self setLineBreakMode:NSLineBreakByWordWrapping]; 20 +
21 - [self setMinimumScaleFactor:11.0f];  
22 - [self setNumberOfLines:0];  
23 [self setFont:[UIFont boldSystemFontOfSize:11.0f]]; 21 [self setFont:[UIFont boldSystemFontOfSize:11.0f]];
24 - [self setTextColor:PNDeepGrey];  
25 self.backgroundColor = [UIColor clearColor]; 22 self.backgroundColor = [UIColor clearColor];
26 - [self setTextAlignment:NSTextAlignmentLeft]; 23 + [self setTextAlignment:NSTextAlignmentCenter];
27 self.userInteractionEnabled = YES; 24 self.userInteractionEnabled = YES;
28 } 25 }
29 26
@@ -11,4 +11,5 @@ @@ -11,4 +11,5 @@
11 + (PNLineChartDataItem *)dataItemWithY:(CGFloat)y; 11 + (PNLineChartDataItem *)dataItemWithY:(CGFloat)y;
12 12
13 @property (readonly) CGFloat y; // should be within the y range 13 @property (readonly) CGFloat y; // should be within the y range
  14 +
14 @end 15 @end
This diff is collapsed. Click to expand it.
@@ -106,6 +106,12 @@ @@ -106,6 +106,12 @@
106 106
107 PNBarChart * barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)]; 107 PNBarChart * barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];
108 barChart.backgroundColor = [UIColor clearColor]; 108 barChart.backgroundColor = [UIColor clearColor];
  109 + barChart.yLabelFormatter = ^(CGFloat yValue){
  110 + CGFloat yValueParsed = yValue;
  111 + NSString * labelText = [NSString stringWithFormat:@"%1.f",yValueParsed];
  112 + return labelText;
  113 + };
  114 + barChart.labelMarginTop = 5.0;
109 [barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]]; 115 [barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]];
110 [barChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]]; 116 [barChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]];
111 [barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]]; 117 [barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]];
@@ -129,18 +135,13 @@ @@ -129,18 +135,13 @@
129 circleChartLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:23.0]; 135 circleChartLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:23.0];
130 circleChartLabel.textAlignment = NSTextAlignmentCenter; 136 circleChartLabel.textAlignment = NSTextAlignmentCenter;
131 137
132 - PNCircleChart * circleChart = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 80.0, SCREEN_WIDTH, 100.0) andTotal:[NSNumber numberWithInt:100] andCurrent:[NSNumber numberWithInt:60] andClockwise:YES andShadow:NO]; 138 + PNCircleChart * circleChart = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 80.0, SCREEN_WIDTH, 100.0) andTotal:[NSNumber numberWithInt:100] andCurrent:[NSNumber numberWithInt:60] andClockwise:YES andShadow:YES];
133 circleChart.backgroundColor = [UIColor clearColor]; 139 circleChart.backgroundColor = [UIColor clearColor];
134 [circleChart setStrokeColor:PNGreen]; 140 [circleChart setStrokeColor:PNGreen];
135 [circleChart strokeChart]; 141 [circleChart strokeChart];
136 142
137 - PNCircleChart * circleChart2 = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 80.0, SCREEN_WIDTH, 100.0) andTotal:[NSNumber numberWithInt:100] andCurrent:[NSNumber numberWithInt:90] andClockwise:YES andShadow:YES];  
138 - circleChart2.backgroundColor = [UIColor clearColor];  
139 - [circleChart2 setStrokeColor:PNBlue];  
140 - [circleChart2 strokeChart];  
141 -  
142 [viewController.view addSubview:circleChartLabel]; 143 [viewController.view addSubview:circleChartLabel];
143 - [viewController.view addSubview:circleChart2]; 144 +
144 [viewController.view addSubview:circleChart]; 145 [viewController.view addSubview:circleChart];
145 viewController.title = @"Circle Chart"; 146 viewController.title = @"Circle Chart";
146 147