adding Scatter Chart code to viewController
** scatter chart programmatically ** adding header to header files
Showing
2 changed files
with
72 additions
and
0 deletions
| @@ -7,6 +7,7 @@ | @@ -7,6 +7,7 @@ | ||
| 7 | // | 7 | // |
| 8 | 8 | ||
| 9 | #import "PCChartsTableViewController.h" | 9 | #import "PCChartsTableViewController.h" |
| 10 | +#define ARC4RANDOM_MAX 0x100000000 | ||
| 10 | 11 | ||
| 11 | @implementation PCChartsTableViewController | 12 | @implementation PCChartsTableViewController |
| 12 | 13 | ||
| @@ -40,7 +41,77 @@ | @@ -40,7 +41,77 @@ | ||
| 40 | //Add pie chart | 41 | //Add pie chart |
| 41 | 42 | ||
| 42 | viewController.title = @"Pie Chart"; | 43 | viewController.title = @"Pie Chart"; |
| 44 | + } else if ([segue.identifier isEqualToString:@"scatterChart"]) | ||
| 45 | + { | ||
| 46 | + //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 | + | ||
| 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"; | ||
| 99 | + } | ||
| 100 | +} | ||
| 101 | + | ||
| 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)]]; | ||
| 43 | } | 111 | } |
| 112 | + [array addObject:XAr]; | ||
| 113 | + [array addObject:YAr]; | ||
| 114 | + return (NSArray*) array; | ||
| 44 | } | 115 | } |
| 45 | 116 | ||
| 46 | 117 |
-
Please register or login to post a comment