MrWooJ

Adding Scatter Graph to PNCHart

* this is compatible with new release.
* because of your huge change, I had to change the position of my codes
from viewController to tableViewController.
* minor changing and improvement.
@@ -56,4 +56,10 @@ @@ -56,4 +56,10 @@
56 - (void) setup; 56 - (void) setup;
57 - (void) drawLineFromPoint : (CGPoint) startPoint ToPoint : (CGPoint) endPoint WithLineWith : (CGFloat) lineWidth AndWithColor : (UIColor*) color; 57 - (void) drawLineFromPoint : (CGPoint) startPoint ToPoint : (CGPoint) endPoint WithLineWith : (CGFloat) lineWidth AndWithColor : (UIColor*) color;
58 58
  59 +/**
  60 + * Update Chart Value
  61 + */
  62 +
  63 +- (void)updateChartData:(NSArray *)data;
  64 +
59 @end 65 @end
@@ -15,6 +15,8 @@ @@ -15,6 +15,8 @@
15 @interface PNScatterChart () 15 @interface PNScatterChart ()
16 16
17 @property (nonatomic, weak) CAShapeLayer *pathLayer; 17 @property (nonatomic, weak) CAShapeLayer *pathLayer;
  18 +@property (nonatomic, weak) NSMutableArray *verticalLineLayer;
  19 +@property (nonatomic, weak) NSMutableArray *horizentalLinepathLayer;
18 20
19 @property (nonatomic) CGPoint startPoint; 21 @property (nonatomic) CGPoint startPoint;
20 22
@@ -42,6 +44,8 @@ @@ -42,6 +44,8 @@
42 @property (nonatomic) CGFloat AxisX_Margin; 44 @property (nonatomic) CGFloat AxisX_Margin;
43 @property (nonatomic) CGFloat AxisY_Margin; 45 @property (nonatomic) CGFloat AxisY_Margin;
44 46
  47 +@property (nonatomic) BOOL isForUpdate;
  48 +
45 - (void)setDefaultValues; 49 - (void)setDefaultValues;
46 50
47 @end 51 @end
@@ -83,6 +87,7 @@ @@ -83,6 +87,7 @@
83 self.backgroundColor = [UIColor whiteColor]; 87 self.backgroundColor = [UIColor whiteColor];
84 self.clipsToBounds = YES; 88 self.clipsToBounds = YES;
85 _showLabel = YES; 89 _showLabel = YES;
  90 + _isForUpdate = NO;
86 self.userInteractionEnabled = YES; 91 self.userInteractionEnabled = YES;
87 92
88 // Coordinate Axis Default Values 93 // Coordinate Axis Default Values
@@ -94,7 +99,7 @@ @@ -94,7 +99,7 @@
94 _AxisX_Margin = 30 ; 99 _AxisX_Margin = 30 ;
95 _AxisY_Margin = 30 ; 100 _AxisY_Margin = 30 ;
96 101
97 - self.frame = CGRectMake((SCREEN_WIDTH - self.frame.size.width) / 2, 200, self.frame.size.width, self.frame.size.height) ; 102 +// self.frame = CGRectMake((SCREEN_WIDTH - self.frame.size.width) / 2, 200, self.frame.size.width, self.frame.size.height) ;
98 self.backgroundColor = [UIColor clearColor]; 103 self.backgroundColor = [UIColor clearColor];
99 104
100 _startPoint.y = self.frame.size.height - self.AxisY_Margin ; 105 _startPoint.y = self.frame.size.height - self.AxisY_Margin ;
@@ -122,10 +127,14 @@ @@ -122,10 +127,14 @@
122 127
123 NSString *LabelFormat = self.yLabelFormat ? : @"%1.f"; 128 NSString *LabelFormat = self.yLabelFormat ? : @"%1.f";
124 CGFloat tempValue = minVal ; 129 CGFloat tempValue = minVal ;
125 - [_axisX_labels addObject:[NSString stringWithFormat:LabelFormat,minVal]]; 130 + UILabel *label = [[UILabel alloc] init];
  131 + label.text = [NSString stringWithFormat:LabelFormat,minVal] ;
  132 + [_axisX_labels addObject:label];
126 for (int i = 0 ; i < _AxisX_partNumber; i++) { 133 for (int i = 0 ; i < _AxisX_partNumber; i++) {
127 tempValue = tempValue + _AxisX_step; 134 tempValue = tempValue + _AxisX_step;
128 - [_axisX_labels addObject:[NSString stringWithFormat:LabelFormat,tempValue]]; 135 + UILabel *tempLabel = [[UILabel alloc] init];
  136 + tempLabel.text = [NSString stringWithFormat:LabelFormat,tempValue] ;
  137 + [_axisX_labels addObject:tempLabel];
129 } 138 }
130 } 139 }
131 140
@@ -136,13 +145,16 @@ @@ -136,13 +145,16 @@
136 _AxisY_partNumber = numberOfTicks - 1; 145 _AxisY_partNumber = numberOfTicks - 1;
137 _AxisY_step = (float)((maxVal - minVal)/_AxisY_partNumber); 146 _AxisY_step = (float)((maxVal - minVal)/_AxisY_partNumber);
138 147
139 - _axisY_labels = [NSMutableArray array];  
140 NSString *LabelFormat = self.yLabelFormat ? : @"%1.f"; 148 NSString *LabelFormat = self.yLabelFormat ? : @"%1.f";
141 CGFloat tempValue = minVal ; 149 CGFloat tempValue = minVal ;
142 - [_axisY_labels addObject:[NSString stringWithFormat:LabelFormat,minVal]]; 150 + UILabel *label = [[UILabel alloc] init];
  151 + label.text = [NSString stringWithFormat:LabelFormat,minVal] ;
  152 + [_axisY_labels addObject:label];
143 for (int i = 0 ; i < _AxisY_partNumber; i++) { 153 for (int i = 0 ; i < _AxisY_partNumber; i++) {
144 tempValue = tempValue + _AxisY_step; 154 tempValue = tempValue + _AxisY_step;
145 - [_axisY_labels addObject:[NSString stringWithFormat:LabelFormat,tempValue]]; 155 + UILabel *tempLabel = [[UILabel alloc] init];
  156 + tempLabel.text = [NSString stringWithFormat:LabelFormat,tempValue] ;
  157 + [_axisY_labels addObject:tempLabel];
146 } 158 }
147 } 159 }
148 160
@@ -164,11 +176,10 @@ @@ -164,11 +176,10 @@
164 _startPointVectorY = _startPoint ; 176 _startPointVectorY = _startPoint ;
165 } 177 }
166 178
167 -- (void) showXLabelsInPosition : (CGPoint) point AndWithText : (NSString *) title 179 +- (void) showXLabel : (UILabel *) descriptionLabel InPosition : (CGPoint) point
168 { 180 {
169 CGRect frame = CGRectMake(point.x, point.y, 30, 10); 181 CGRect frame = CGRectMake(point.x, point.y, 30, 10);
170 - UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:frame]; 182 + descriptionLabel.frame = frame;
171 - descriptionLabel.text = title;  
172 descriptionLabel.font = _descriptionTextFont; 183 descriptionLabel.font = _descriptionTextFont;
173 descriptionLabel.textColor = _descriptionTextColor; 184 descriptionLabel.textColor = _descriptionTextColor;
174 descriptionLabel.shadowColor = _descriptionTextShadowColor; 185 descriptionLabel.shadowColor = _descriptionTextShadowColor;
@@ -180,40 +191,38 @@ @@ -180,40 +191,38 @@
180 191
181 - (void)setChartData:(NSArray *)data 192 - (void)setChartData:(NSArray *)data
182 { 193 {
183 - if (data != _chartData) { 194 + __block CGFloat yFinilizeValue , xFinilizeValue;
184 - __block CGFloat yFinilizeValue , xFinilizeValue; 195 + __block CGFloat yValue , xValue;
185 - __block CGFloat yValue , xValue; 196 + CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
186 - CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 197 + pathAnimation.duration = _duration;
187 - pathAnimation.duration = _duration; 198 + pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
188 - pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 199 + pathAnimation.fromValue = @(0.0f);
189 - pathAnimation.fromValue = @(0.0f); 200 + pathAnimation.toValue = @(1.0f);
190 - pathAnimation.toValue = @(1.0f); 201 + pathAnimation.fillMode = kCAFillModeForwards;
191 - pathAnimation.fillMode = kCAFillModeForwards; 202 + self.layer.opacity = 1;
192 - self.layer.opacity = 1; 203 +
193 - 204 + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
194 - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 205 + [NSThread sleepForTimeInterval:1];
195 - [NSThread sleepForTimeInterval:1]; 206 + // update UI on the main thread
196 - // update UI on the main thread 207 + dispatch_async(dispatch_get_main_queue(), ^{
197 - dispatch_async(dispatch_get_main_queue(), ^{ 208 + for (PNScatterChartData *chartData in data) {
198 - for (PNScatterChartData *chartData in data) { 209 + for (NSUInteger i = 0; i < chartData.itemCount; i++) {
199 - for (NSUInteger i = 0; i < chartData.itemCount; i++) { 210 + yValue = chartData.getData(i).y;
200 - yValue = chartData.getData(i).y; 211 + xValue = chartData.getData(i).x;
201 - xValue = chartData.getData(i).x; 212 + if (!(xValue >= _AxisX_minValue && xValue <= _AxisX_maxValue) || !(yValue >= _AxisY_minValue && yValue <= _AxisY_maxValue)) {
202 - if (!(xValue >= _AxisX_minValue && xValue <= _AxisX_maxValue) || !(yValue >= _AxisY_minValue && yValue <= _AxisY_maxValue)) { 213 + NSLog(@"input is not in correct range.");
203 - NSLog(@"input is not in correct range."); 214 + exit(0);
204 - exit(0);  
205 - }  
206 - xFinilizeValue = [self mappingIsForAxisX:true WithValue:xValue];  
207 - yFinilizeValue = [self mappingIsForAxisX:false WithValue:yValue];  
208 - CAShapeLayer *shape = [self drawingPointsForChartData:chartData AndWithX:xFinilizeValue AndWithY:yFinilizeValue];  
209 - [self.layer addSublayer:shape];  
210 - self.pathLayer = shape ;  
211 - [self.pathLayer addAnimation:pathAnimation forKey:@"fade"];  
212 } 215 }
  216 + xFinilizeValue = [self mappingIsForAxisX:true WithValue:xValue];
  217 + yFinilizeValue = [self mappingIsForAxisX:false WithValue:yValue];
  218 + CAShapeLayer *shape = [self drawingPointsForChartData:chartData AndWithX:xFinilizeValue AndWithY:yFinilizeValue];
  219 + self.pathLayer = shape ;
  220 + [self.layer addSublayer:self.pathLayer];
  221 + [self.pathLayer addAnimation:pathAnimation forKey:@"fade"];
213 } 222 }
214 - }); 223 + }
215 }); 224 });
216 - } 225 + });
217 } 226 }
218 227
219 - (CGFloat) mappingIsForAxisX : (BOOL) isForAxisX WithValue : (CGFloat) value{ 228 - (CGFloat) mappingIsForAxisX : (BOOL) isForAxisX WithValue : (CGFloat) value{
@@ -231,52 +240,79 @@ @@ -231,52 +240,79 @@
231 return 0; 240 return 0;
232 } 241 }
233 242
  243 +#pragma mark - Update Chart Data
  244 +
  245 +- (void)updateChartData:(NSArray *)data
  246 +{
  247 + _chartData = data;
  248 +
  249 + // will be work in future.
  250 +}
  251 +
234 #pragma drawing methods 252 #pragma drawing methods
235 253
236 - (void)drawRect:(CGRect)rect 254 - (void)drawRect:(CGRect)rect
237 { 255 {
238 [super drawRect:rect]; 256 [super drawRect:rect];
239 - CGContextRef context = UIGraphicsGetCurrentContext();  
240 257
241 - if (_showCoordinateAxis) { 258 + CGContextRef context = UIGraphicsGetCurrentContext();
242 - CGContextSetStrokeColorWithColor(context, [_axisColor CGColor]); 259 + if (_showCoordinateAxis) {
243 - CGContextSetLineWidth(context, _axisWidth); 260 + CGContextSetStrokeColorWithColor(context, [_axisColor CGColor]);
244 - //drawing x vector 261 + CGContextSetLineWidth(context, _axisWidth);
245 - CGContextMoveToPoint(context, _startPoint.x, _startPoint.y); 262 + //drawing x vector
246 - CGContextAddLineToPoint(context, _endPointVecotrX.x, _endPointVecotrX.y); 263 + CGContextMoveToPoint(context, _startPoint.x, _startPoint.y);
247 - //drawing y vector 264 + CGContextAddLineToPoint(context, _endPointVecotrX.x, _endPointVecotrX.y);
248 - CGContextMoveToPoint(context, _startPoint.x, _startPoint.y); 265 + //drawing y vector
249 - CGContextAddLineToPoint(context, _endPointVecotrY.x, _endPointVecotrY.y); 266 + CGContextMoveToPoint(context, _startPoint.x, _startPoint.y);
250 - //drawing x arrow vector 267 + CGContextAddLineToPoint(context, _endPointVecotrY.x, _endPointVecotrY.y);
251 - CGContextMoveToPoint(context, _endPointVecotrX.x, _endPointVecotrX.y); 268 + //drawing x arrow vector
252 - CGContextAddLineToPoint(context, _endPointVecotrX.x - 5, _endPointVecotrX.y + 3); 269 + CGContextMoveToPoint(context, _endPointVecotrX.x, _endPointVecotrX.y);
253 - CGContextMoveToPoint(context, _endPointVecotrX.x, _endPointVecotrX.y); 270 + CGContextAddLineToPoint(context, _endPointVecotrX.x - 5, _endPointVecotrX.y + 3);
254 - CGContextAddLineToPoint(context, _endPointVecotrX.x - 5, _endPointVecotrX.y - 3); 271 + CGContextMoveToPoint(context, _endPointVecotrX.x, _endPointVecotrX.y);
255 - //drawing y arrow vector 272 + CGContextAddLineToPoint(context, _endPointVecotrX.x - 5, _endPointVecotrX.y - 3);
256 - CGContextMoveToPoint(context, _endPointVecotrY.x, _endPointVecotrY.y); 273 + //drawing y arrow vector
257 - CGContextAddLineToPoint(context, _endPointVecotrY.x - 3, _endPointVecotrY.y + 5); 274 + CGContextMoveToPoint(context, _endPointVecotrY.x, _endPointVecotrY.y);
258 - CGContextMoveToPoint(context, _endPointVecotrY.x, _endPointVecotrY.y); 275 + CGContextAddLineToPoint(context, _endPointVecotrY.x - 3, _endPointVecotrY.y + 5);
259 - CGContextAddLineToPoint(context, _endPointVecotrY.x + 3, _endPointVecotrY.y + 5); 276 + CGContextMoveToPoint(context, _endPointVecotrY.x, _endPointVecotrY.y);
260 - } 277 + CGContextAddLineToPoint(context, _endPointVecotrY.x + 3, _endPointVecotrY.y + 5);
  278 + }
261 279
262 if (_showLabel) { 280 if (_showLabel) {
263 NSString *str; 281 NSString *str;
264 //drawing x steps vector and putting axis x labels 282 //drawing x steps vector and putting axis x labels
265 float temp = _startPointVectorX.x + (_vectorX_Steps / 2) ; 283 float temp = _startPointVectorX.x + (_vectorX_Steps / 2) ;
266 - for (int i = 0; i < _AxisX_partNumber; i++) { 284 + for (int i = 0; i < _axisX_labels.count; i++) {
267 - CGContextMoveToPoint(context, temp, _startPointVectorX.y - 2); 285 + UIBezierPath *path = [UIBezierPath bezierPath];
268 - CGContextAddLineToPoint(context, temp, _startPointVectorX.y + 3); 286 + [path moveToPoint:CGPointMake(temp, _startPointVectorX.y - 2)];
269 - str = [_axisX_labels objectAtIndex:i]; 287 + [path addLineToPoint:CGPointMake(temp, _startPointVectorX.y + 3)];
270 - [self showXLabelsInPosition:CGPointMake(temp - 15, _startPointVectorX.y + 10 ) AndWithText:str]; 288 + CAShapeLayer *shapeLayer = [CAShapeLayer layer];
  289 + shapeLayer.path = [path CGPath];
  290 + shapeLayer.strokeColor = [_axisColor CGColor];
  291 + shapeLayer.lineWidth = _axisWidth;
  292 + shapeLayer.fillColor = [_axisColor CGColor];
  293 + [self.horizentalLinepathLayer addObject:shapeLayer];
  294 + [self.layer addSublayer:shapeLayer];
  295 + UILabel *lb = [_axisX_labels objectAtIndex:i] ;
  296 + str = lb.text;
  297 + [self showXLabel:lb InPosition:CGPointMake(temp - 15, _startPointVectorX.y + 10 )];
271 temp = temp + _vectorX_Steps ; 298 temp = temp + _vectorX_Steps ;
272 } 299 }
273 //drawing y steps vector and putting axis x labels 300 //drawing y steps vector and putting axis x labels
274 temp = _startPointVectorY.y - (_vectorY_Steps / 2) ; 301 temp = _startPointVectorY.y - (_vectorY_Steps / 2) ;
275 - for (int i = 0; i < _AxisY_partNumber; i++) { 302 + for (int i = 0; i < _axisY_labels.count; i++) {
276 - CGContextMoveToPoint(context, _startPointVectorY.x - 3, temp); 303 + UIBezierPath *path = [UIBezierPath bezierPath];
277 - CGContextAddLineToPoint(context, _startPointVectorY.x + 2, temp); 304 + [path moveToPoint:CGPointMake(_startPointVectorY.x - 3, temp)];
278 - str = [_axisY_labels objectAtIndex:i]; 305 + [path addLineToPoint:CGPointMake( _startPointVectorY.x + 2, temp)];
279 - [self showXLabelsInPosition:CGPointMake(_startPointVectorY.x - 30, temp - 5) AndWithText:str]; 306 + CAShapeLayer *shapeLayer = [CAShapeLayer layer];
  307 + shapeLayer.path = [path CGPath];
  308 + shapeLayer.strokeColor = [_axisColor CGColor];
  309 + shapeLayer.lineWidth = _axisWidth;
  310 + shapeLayer.fillColor = [_axisColor CGColor];
  311 + [self.verticalLineLayer addObject:shapeLayer];
  312 + [self.layer addSublayer:shapeLayer];
  313 + UILabel *lb = [_axisY_labels objectAtIndex:i];
  314 + str = lb.text;
  315 + [self showXLabel:lb InPosition:CGPointMake(_startPointVectorY.x - 30, temp - 5)];
280 temp = temp - _vectorY_Steps ; 316 temp = temp - _vectorY_Steps ;
281 } 317 }
282 } 318 }
@@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
16 @property (nonatomic) PNBarChart * barChart; 16 @property (nonatomic) PNBarChart * barChart;
17 @property (nonatomic) PNCircleChart * circleChart; 17 @property (nonatomic) PNCircleChart * circleChart;
18 @property (nonatomic) PNPieChart *pieChart; 18 @property (nonatomic) PNPieChart *pieChart;
  19 +@property (nonatomic) PNScatterChart *scatterChart;
19 20
20 @property (weak, nonatomic) IBOutlet UILabel *titleLabel; 21 @property (weak, nonatomic) IBOutlet UILabel *titleLabel;
21 22
@@ -7,6 +7,7 @@ @@ -7,6 +7,7 @@
7 // 7 //
8 8
9 #import "PCChartViewController.h" 9 #import "PCChartViewController.h"
  10 +#define ARC4RANDOM_MAX 0x100000000
10 11
11 @implementation PCChartViewController 12 @implementation PCChartViewController
12 13
@@ -120,6 +121,41 @@ @@ -120,6 +121,41 @@
120 [self.view addSubview:self.pieChart]; 121 [self.view addSubview:self.pieChart];
121 self.changeValueButton.hidden = YES; 122 self.changeValueButton.hidden = YES;
122 } 123 }
  124 + else if ([self.title isEqualToString:@"Scatter Chart"])
  125 + {
  126 + self.titleLabel.text = @"Scatter Chart";
  127 +
  128 + self.scatterChart = [[PNScatterChart alloc] initWithFrame:CGRectMake(SCREEN_WIDTH /6.0 - 30, 135, 280, 200)];
  129 + [self.scatterChart setAxisXWithMinimumValue:20 andMaxValue:100 toTicks:6];
  130 + [self.scatterChart setAxisYWithMinimumValue:30 andMaxValue:50 toTicks:5];
  131 +
  132 + NSArray * data01Array = [self randomSetOfObjects];
  133 + PNScatterChartData *data01 = [PNScatterChartData new];
  134 + data01.strokeColor = PNGreen;
  135 + data01.fillColor = PNFreshGreen;
  136 + data01.size = 2;
  137 + data01.itemCount = [[data01Array objectAtIndex:0] count];
  138 + data01.inflexionPointStyle = PNScatterChartPointStyleCircle;
  139 + __block NSMutableArray *XAr1 = [NSMutableArray arrayWithArray:[data01Array objectAtIndex:0]];
  140 + __block NSMutableArray *YAr1 = [NSMutableArray arrayWithArray:[data01Array objectAtIndex:1]];
  141 + data01.getData = ^(NSUInteger index) {
  142 + CGFloat xValue = [[XAr1 objectAtIndex:index] floatValue];
  143 + CGFloat yValue = [[YAr1 objectAtIndex:index] floatValue];
  144 + return [PNScatterChartDataItem dataItemWithX:xValue AndWithY:yValue];
  145 + };
  146 +
  147 + [self.scatterChart setup];
  148 + self.scatterChart.chartData = @[data01];
  149 +/***
  150 + this is for drawing line to compare
  151 + CGPoint start = CGPointMake(20, 35);
  152 + CGPoint end = CGPointMake(80, 45);
  153 + [self.scatterChart drawLineFromPoint:start ToPoint:end WithLineWith:2 AndWithColor:PNBlack];
  154 +***/
  155 + self.scatterChart.delegate = self;
  156 + self.changeValueButton.hidden = YES;
  157 + [self.view addSubview:self.scatterChart];
  158 + }
123 } 159 }
124 160
125 161
@@ -171,6 +207,10 @@ @@ -171,6 +207,10 @@
171 { 207 {
172 [self.circleChart updateChartByCurrent:@(arc4random() % 100)]; 208 [self.circleChart updateChartByCurrent:@(arc4random() % 100)];
173 } 209 }
  210 + else if ([self.title isEqualToString:@"Scatter Chart"])
  211 + {
  212 + // will be code soon.
  213 + }
174 214
175 } 215 }
176 216
@@ -195,4 +235,19 @@ @@ -195,4 +235,19 @@
195 [bar.layer addAnimation:animation forKey:@"Float"]; 235 [bar.layer addAnimation:animation forKey:@"Float"];
196 } 236 }
197 237
  238 +/* this function is used only for creating random points */
  239 +- (NSArray *) randomSetOfObjects{
  240 + NSMutableArray *array = [NSMutableArray array];
  241 + NSString *LabelFormat = @"%1.f";
  242 + NSMutableArray *XAr = [NSMutableArray array];
  243 + NSMutableArray *YAr = [NSMutableArray array];
  244 + for (int i = 0; i < 25 ; i++) {
  245 + [XAr addObject:[NSString stringWithFormat:LabelFormat,(((double)arc4random() / ARC4RANDOM_MAX) * (self.scatterChart.AxisX_maxValue - self.scatterChart.AxisX_minValue) + self.scatterChart.AxisX_minValue)]];
  246 + [YAr addObject:[NSString stringWithFormat:LabelFormat,(((double)arc4random() / ARC4RANDOM_MAX) * (self.scatterChart.AxisY_maxValue - self.scatterChart.AxisY_minValue) + self.scatterChart.AxisY_minValue)]];
  247 + }
  248 + [array addObject:XAr];
  249 + [array addObject:YAr];
  250 + return (NSArray*) array;
  251 +}
  252 +
198 @end 253 @end
@@ -7,7 +7,6 @@ @@ -7,7 +7,6 @@
7 // 7 //
8 8
9 #import "PCChartsTableViewController.h" 9 #import "PCChartsTableViewController.h"
10 -#define ARC4RANDOM_MAX 0x100000000  
11 10
12 @implementation PCChartsTableViewController 11 @implementation PCChartsTableViewController
13 12
@@ -44,76 +43,9 @@ @@ -44,76 +43,9 @@
44 } else if ([segue.identifier isEqualToString:@"scatterChart"]) 43 } else if ([segue.identifier isEqualToString:@"scatterChart"])
45 { 44 {
46 //Add scatter chart 45 //Add scatter chart
47 - UILabel * scatterChartLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, SCREEN_WIDTH, 30)];  
48 - scatterChartLabel.text = @"Scatter Chart";  
49 - scatterChartLabel.textColor = PNFreshGreen;  
50 - scatterChartLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:23.0];  
51 - scatterChartLabel.textAlignment = NSTextAlignmentCenter;  
52 46
53 - PNScatterChart *scatterChart = [[PNScatterChart alloc] initWithFrame:CGRectMake(0, 0, 280, 200)];  
54 - [scatterChart setAxisXWithMinimumValue:20 andMaxValue:100 toTicks:6];  
55 - [scatterChart setAxisYWithMinimumValue:30 andMaxValue:50 toTicks:5];  
56 -  
57 - NSArray * data01Array = [self randomSetOfObjectsForScatterChart:scatterChart];  
58 - PNScatterChartData *data01 = [PNScatterChartData new];  
59 - data01.strokeColor = PNGreen;  
60 - data01.fillColor = PNFreshGreen;  
61 - data01.size = 2;  
62 - data01.itemCount = [[data01Array objectAtIndex:0] count];  
63 - data01.inflexionPointStyle = PNScatterChartPointStyleCircle;  
64 - __block NSMutableArray *XAr1 = [NSMutableArray arrayWithArray:[data01Array objectAtIndex:0]];  
65 - __block NSMutableArray *YAr1 = [NSMutableArray arrayWithArray:[data01Array objectAtIndex:1]];  
66 - data01.getData = ^(NSUInteger index) {  
67 - CGFloat xValue = [[XAr1 objectAtIndex:index] floatValue];  
68 - CGFloat yValue = [[YAr1 objectAtIndex:index] floatValue];  
69 - return [PNScatterChartDataItem dataItemWithX:xValue AndWithY:yValue];  
70 - };  
71 -  
72 - NSArray * data02Array = [self randomSetOfObjectsForScatterChart:scatterChart];  
73 - PNScatterChartData *data02 = [PNScatterChartData new];  
74 - data02.strokeColor = PNBlue;  
75 - data02.fillColor = PNBlue;  
76 - data02.size = 2;  
77 - data02.itemCount = [[data02Array objectAtIndex:0] count];  
78 - data02.inflexionPointStyle = PNScatterChartPointStyleCircle;  
79 - __block NSMutableArray *XAr2 = [NSMutableArray arrayWithArray:[data02Array objectAtIndex:0]];  
80 - __block NSMutableArray *YAr2 = [NSMutableArray arrayWithArray:[data02Array objectAtIndex:1]];  
81 - data02.getData = ^(NSUInteger index) {  
82 - CGFloat xValue = [[XAr2 objectAtIndex:index] floatValue];  
83 - CGFloat yValue = [[YAr2 objectAtIndex:index] floatValue];  
84 - return [PNScatterChartDataItem dataItemWithX:xValue AndWithY:yValue];  
85 - };  
86 -  
87 - [scatterChart setup];  
88 - scatterChart.chartData = @[data01 , data02];  
89 -  
90 - // this is for drawing line to compare  
91 - CGPoint start = CGPointMake(20, 35);  
92 - CGPoint end = CGPointMake(80, 45);  
93 - [scatterChart drawLineFromPoint:start ToPoint:end WithLineWith:2 AndWithColor:PNBlack];  
94 -  
95 - scatterChart.delegate = self;  
96 - [viewController.view addSubview:scatterChartLabel];  
97 - [viewController.view addSubview:scatterChart];  
98 viewController.title = @"Scatter Chart"; 47 viewController.title = @"Scatter Chart";
99 } 48 }
100 } 49 }
101 50
102 -/* this function is used only for creating random points */  
103 -- (NSArray *) randomSetOfObjectsForScatterChart:(PNScatterChart *)chart{  
104 - NSMutableArray *array = [NSMutableArray array];  
105 - NSString *LabelFormat = @"%1.f";  
106 - NSMutableArray *XAr = [NSMutableArray array];  
107 - NSMutableArray *YAr = [NSMutableArray array];  
108 - for (int i = 0; i < 25 ; i++) {  
109 - [XAr addObject:[NSString stringWithFormat:LabelFormat,(((double)arc4random() / ARC4RANDOM_MAX) * (chart.AxisX_maxValue - chart.AxisX_minValue) + chart.AxisX_minValue)]];  
110 - [YAr addObject:[NSString stringWithFormat:LabelFormat,(((double)arc4random() / ARC4RANDOM_MAX) * (chart.AxisY_maxValue - chart.AxisY_minValue) + chart.AxisY_minValue)]];  
111 - }  
112 - [array addObject:XAr];  
113 - [array addObject:YAr];  
114 - return (NSArray*) array;  
115 -}  
116 -  
117 -  
118 -  
119 @end 51 @end