1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
| #import "DetailsViewController.h"
@interface DetailsViewController ()
@end
@implementation DetailsViewController
@synthesize value; @synthesize key; @synthesize dataForPlot1; @synthesize graph; @synthesize points;
@synthesize yArr; @synthesize xArr;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.points = [[NSMutableArray alloc] init]; NSUInteger i; self.xArr = [[NSMutableArray alloc] init]; self.yArr = [[NSMutableArray alloc] init]; for(i=0;i<60;i++){ id x = [NSNumber numberWithFloat: 1+i*0.5]; id y = [NSNumber numberWithFloat: 1.2 * rand()/(float)RAND_MAX+1.2]; [self.xArr addObject:x]; [self.yArr addObject:y]; [self.points addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x,@"x",y,@"y", nil]]; NSNumber *tmpX = [NSNumber numberWithFloat:[x intValue]]; NSNumber *tmpY = [NSNumber numberWithFloat:[y intValue]]; maxX = MAX([tmpX intValue], maxX); minX = MIN([tmpX intValue], minX); maxY = MAX([tmpY intValue], maxY); minY = MIN([tmpY intValue], minY); } NSLog(@"maxX = %d",maxX); NSLog(@"minX = %d",minX); NSLog(@"maxY = %d",maxY); NSLog(@"minY = %d",minY);
} return self; }
- (void)viewDidLoad{ [super viewDidLoad]; self.title = self.value; [self lineChart];
}
-(void) gridLine{ }
-(void) lineChart{ graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero]; CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme]; [graph applyTheme:theme]; CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:self.view.bounds]; graph.paddingLeft = 0; graph.paddingTop = 0; graph.paddingRight = 0; graph.paddingBottom = 0; graph.plotAreaFrame.paddingLeft = 40.0 ; graph.plotAreaFrame.paddingTop = 10.0 ; graph.plotAreaFrame.paddingRight = 10.0 ; graph.plotAreaFrame.paddingBottom = 10.0 ; hostingView.hostedGraph = graph; CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace; plotSpace.allowsUserInteraction = NO; plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(minX) length:CPTDecimalFromFloat(maxX)]; plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(minY) length:CPTDecimalFromFloat(maxY+2)]; CPTScatterPlot *boundLinePlot = [[CPTScatterPlot alloc] init]; CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; lineStyle.miterLimit = 1.0f; lineStyle.lineWidth = 3.0f; lineStyle.lineColor = [CPTColor blueColor]; boundLinePlot.dataLineStyle = lineStyle; boundLinePlot.identifier = @"lineChart"; boundLinePlot.dataSource = self; [graph addPlot:boundLinePlot]; CPTXYAxisSet *axisSet = (CPTXYAxisSet *) graph.axisSet; CPTXYAxis *x = axisSet.xAxis; x.majorIntervalLength = CPTDecimalFromString(@"10"); x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0"); x.minorTicksPerInterval = 10;
CPTXYAxis *y = axisSet.yAxis; y.majorIntervalLength = CPTDecimalFromString(@"1"); y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"1"); y.minorTicksPerInterval = 1; [self.view addSubview:hostingView];
}
-(void) fillFigure{ graph = [[CPTXYGraph alloc] initWithFrame:self.view.bounds]; CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme]; [graph applyTheme:theme]; CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:self.view.bounds]; hostingView.hostedGraph = graph; [self.view addSubview:hostingView]; graph.paddingLeft = 0; graph.paddingTop = 0; graph.paddingRight = 0; graph.paddingBottom = 0; graph.plotAreaFrame.paddingLeft = 40.0 ; graph.plotAreaFrame.paddingTop = 40.0 ; graph.plotAreaFrame.paddingRight = 15.0 ; graph.plotAreaFrame.paddingBottom = 80.0 ; CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; plotSpace.allowsUserInteraction = YES; plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(200.0)]; plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(200.0)]; CPTXYAxisSet *axisSet = (CPTXYAxisSet *) graph.axisSet ; CPTXYAxis *x = axisSet.xAxis ; x. minorTickLineStyle = nil ; x. majorIntervalLength = CPTDecimalFromString (@"50"); x. orthogonalCoordinateDecimal = CPTDecimalFromString ( @"0" ); CPTXYAxis *y = axisSet.yAxis ; y. minorTickLineStyle = nil ; y. majorIntervalLength = CPTDecimalFromString ( @"50" ); y. orthogonalCoordinateDecimal = CPTDecimalFromString (@"0"); dataSourceLinePlot = [[CPTScatterPlot alloc] init]; dataSourceLinePlot.identifier = @"Green Plot"; CPTMutableLineStyle *lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy]; lineStyle.lineWidth = 1.f; lineStyle.lineColor = [CPTColor greenColor]; dataSourceLinePlot.dataLineStyle = lineStyle; dataSourceLinePlot.opacity = 0.0f; dataSourceLinePlot.dataSource = self; [graph addPlot:dataSourceLinePlot]; CPTGradient *areaGradient = [ CPTGradient gradientWithBeginningColor :[CPTColor greenColor] endingColor :[CPTColor colorWithComponentRed:0.65 green:0.65 blue:0.16 alpha:0.2]]; areaGradient.angle = -90.0f ; CPTFill *areaGradientFill = [ CPTFill fillWithGradient :areaGradient]; dataSourceLinePlot.areaFill = areaGradientFill; dataSourceLinePlot.areaBaseValue = CPTDecimalFromString ( @"0.0" ); dataSourceLinePlot.interpolation = CPTScatterPlotInterpolationLinear ; dataForPlot1 = [[NSMutableArray alloc] init]; [self plotData]; }
-(void) plotData{ if ([dataSourceLinePlot.identifier isEqual:@"Green Plot"]) { NSString *xp1 = [NSString stringWithFormat:@"%d",1]; NSString *yp1 = [NSString stringWithFormat:@"%d",10]; NSMutableDictionary *point1 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:xp1, @"x", yp1, @"y", nil]; [dataForPlot1 insertObject:point1 atIndex:0]; NSString *xp2 = [NSString stringWithFormat:@"%d",10]; NSString *yp2 = [NSString stringWithFormat:@"%d",25]; NSMutableDictionary *point2 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:xp2, @"x", yp2, @"y", nil]; [dataForPlot1 insertObject:point2 atIndex:1]; NSString *xp3 = [NSString stringWithFormat:@"%d",30]; NSString *yp3 = [NSString stringWithFormat:@"%d",15]; NSMutableDictionary *point3 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:xp3, @"x", yp3, @"y", nil]; [dataForPlot1 insertObject:point3 atIndex:2]; NSString *xp4 = [NSString stringWithFormat:@"%d",50]; NSString *yp4 = [NSString stringWithFormat:@"%d",80]; NSMutableDictionary *point4 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:xp4, @"x", yp4, @"y", nil]; [dataForPlot1 insertObject:point4 atIndex:3]; NSString *xp5 = [NSString stringWithFormat:@"%d",70]; NSString *yp5 = [NSString stringWithFormat:@"%d",60]; NSMutableDictionary *point5 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:xp5, @"x", yp5, @"y", nil]; [dataForPlot1 insertObject:point5 atIndex:4]; NSString *xp6 = [NSString stringWithFormat:@"%d",90]; NSString *yp6 = [NSString stringWithFormat:@"%d",100]; NSMutableDictionary *point6 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:xp6, @"x", yp6, @"y", nil]; [dataForPlot1 insertObject:point6 atIndex:5]; NSString *xp7 = [NSString stringWithFormat:@"%d",110]; NSString *yp7 = [NSString stringWithFormat:@"%d",70]; NSMutableDictionary *point7 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:xp7, @"x", yp7, @"y", nil]; [dataForPlot1 insertObject:point7 atIndex:6]; NSString *xp8 = [NSString stringWithFormat:@"%d",130]; NSString *yp8 = [NSString stringWithFormat:@"%d",80]; NSMutableDictionary *point8 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:xp8, @"x", yp8, @"y", nil]; [dataForPlot1 insertObject:point8 atIndex:7]; NSString *xp9 = [NSString stringWithFormat:@"%d",200]; NSString *yp9 = [NSString stringWithFormat:@"%d",135]; NSMutableDictionary *point9 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:xp9, @"x", yp9, @"y", nil]; [dataForPlot1 insertObject:point9 atIndex:8]; } }
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot{ return [self.points count]; }
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{ NSString *key = (fieldEnum == CPTScatterPlotFieldX ? @"x" : @"y"); NSNumber *num = [[self.points objectAtIndex:index] valueForKey:key]; return num; }
- (void) dataOpt{ if ([dataSourceLinePlot.identifier isEqual:@"Green Plot"]) { NSString *xp = [NSString stringWithFormat:@"%d",j]; NSString *yp = [NSString stringWithFormat:@"%d",(rand()%100)]; NSMutableDictionary *point1 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:xp, @"x", yp, @"y", nil]; [dataForPlot1 insertObject:point1 atIndex:0]; } [graph reloadData]; j = j + 20; r = r + 20; }
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }
@end
|