From 3f123b55ff5fc90aefc6452d8c22e5b5b0558657 Mon Sep 17 00:00:00 2001 From: Mike Hollis Date: Mon, 2 Dec 2013 20:43:32 -0500 Subject: [PATCH 1/4] Removed dependency on JSONKit for use of NSJSONSerialize --- iOSPlot/PlotCreator.xcodeproj/project.pbxproj | 14 -------------- iOSPlot/iPhone/LineChartViewController.m | 4 ++-- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/iOSPlot/PlotCreator.xcodeproj/project.pbxproj b/iOSPlot/PlotCreator.xcodeproj/project.pbxproj index 156456b..1829c3d 100755 --- a/iOSPlot/PlotCreator.xcodeproj/project.pbxproj +++ b/iOSPlot/PlotCreator.xcodeproj/project.pbxproj @@ -26,7 +26,6 @@ F897D6EE13630F560025FE6E /* sample_piechart_data.plist in Resources */ = {isa = PBXBuildFile; fileRef = F897D6ED13630F560025FE6E /* sample_piechart_data.plist */; }; F897D6F113630FD40025FE6E /* PCPieChart.m in Sources */ = {isa = PBXBuildFile; fileRef = F897D6F013630FD40025FE6E /* PCPieChart.m */; }; F897D700136311E60025FE6E /* PieChartViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F897D6FF136311E60025FE6E /* PieChartViewController.m */; }; - F8B8196215D573FB0005945A /* JSONKit.m in Sources */ = {isa = PBXBuildFile; fileRef = F8B8196115D573FB0005945A /* JSONKit.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; F8C1B41C136F8056002DDF6C /* sample_linechart_data.json in Resources */ = {isa = PBXBuildFile; fileRef = F8C1B41B136F8056002DDF6C /* sample_linechart_data.json */; }; /* End PBXBuildFile section */ @@ -63,8 +62,6 @@ F897D6F013630FD40025FE6E /* PCPieChart.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PCPieChart.m; path = Shared/PCPieChart.m; sourceTree = ""; }; F897D6FE136311E60025FE6E /* PieChartViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PieChartViewController.h; path = iPhone/PieChartViewController.h; sourceTree = ""; }; F897D6FF136311E60025FE6E /* PieChartViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PieChartViewController.m; path = iPhone/PieChartViewController.m; sourceTree = ""; }; - F8B8196015D573FB0005945A /* JSONKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JSONKit.h; path = JSONKit/JSONKit.h; sourceTree = ""; }; - F8B8196115D573FB0005945A /* JSONKit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = JSONKit.m; path = JSONKit/JSONKit.m; sourceTree = ""; }; F8C1B41B136F8056002DDF6C /* sample_linechart_data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = sample_linechart_data.json; path = "Sample Data/sample_linechart_data.json"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -114,7 +111,6 @@ isa = PBXGroup; children = ( 3794799B13706BB200C0E457 /* iOSPlot */, - F8C1B407136F7FB7002DDF6C /* JSONKit */, F897D6FE136311E60025FE6E /* PieChartViewController.h */, F897D6FF136311E60025FE6E /* PieChartViewController.m */, F83260C01363D91E0045F9DC /* PieChartViewController2.h */, @@ -187,15 +183,6 @@ name = "Sample Data"; sourceTree = ""; }; - F8C1B407136F7FB7002DDF6C /* JSONKit */ = { - isa = PBXGroup; - children = ( - F8B8196015D573FB0005945A /* JSONKit.h */, - F8B8196115D573FB0005945A /* JSONKit.m */, - ); - name = JSONKit; - sourceTree = ""; - }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -275,7 +262,6 @@ F88CC4D313684FD30096327F /* LineChartViewController.m in Sources */, F87DE41A1370D68300347F69 /* PCHalfPieChart.m in Sources */, F87DE41D1370D82D00347F69 /* HalfPieChartViewController.m in Sources */, - F8B8196215D573FB0005945A /* JSONKit.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/iOSPlot/iPhone/LineChartViewController.m b/iOSPlot/iPhone/LineChartViewController.m index 6a63509..3224b65 100644 --- a/iOSPlot/iPhone/LineChartViewController.m +++ b/iOSPlot/iPhone/LineChartViewController.m @@ -32,7 +32,6 @@ */ #import "LineChartViewController.h" -#import "JSONKit.h" @implementation LineChartViewController @@ -53,7 +52,8 @@ - (id)init NSString *sampleFile = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"sample_linechart_data.json"]; NSString *jsonString = [NSString stringWithContentsOfFile:sampleFile encoding:NSUTF8StringEncoding error:nil]; - NSDictionary *sampleInfo = [jsonString objectFromJSONString]; + NSDictionary *sampleInfo = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] + options:NSJSONReadingMutableContainers error:nil]; NSMutableArray *components = [NSMutableArray array]; for (int i=0; i<[[sampleInfo objectForKey:@"data"] count]; i++) From 22eeb2a7ac232c5aa52c1407979532a19c530946 Mon Sep 17 00:00:00 2001 From: Mike Hollis Date: Mon, 2 Dec 2013 20:52:31 -0500 Subject: [PATCH 2/4] Added error handle for bad JSON in test code --- iOSPlot/iPhone/LineChartViewController.m | 78 +++++++++++++----------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/iOSPlot/iPhone/LineChartViewController.m b/iOSPlot/iPhone/LineChartViewController.m index 3224b65..0747ff4 100644 --- a/iOSPlot/iPhone/LineChartViewController.m +++ b/iOSPlot/iPhone/LineChartViewController.m @@ -40,6 +40,7 @@ - (id)init self = [super init]; if (self) { + NSError *jsonError; [self.view setBackgroundColor:[UIColor colorWithWhite:1 alpha:1]]; [self setTitle:@"Line Chart"]; @@ -53,42 +54,49 @@ - (id)init NSString *jsonString = [NSString stringWithContentsOfFile:sampleFile encoding:NSUTF8StringEncoding error:nil]; NSDictionary *sampleInfo = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] - options:NSJSONReadingMutableContainers error:nil]; + options:NSJSONReadingMutableContainers error:&jsonError]; - NSMutableArray *components = [NSMutableArray array]; - for (int i=0; i<[[sampleInfo objectForKey:@"data"] count]; i++) - { - NSDictionary *point = [[sampleInfo objectForKey:@"data"] objectAtIndex:i]; - PCLineChartViewComponent *component = [[PCLineChartViewComponent alloc] init]; - [component setTitle:[point objectForKey:@"title"]]; - [component setPoints:[point objectForKey:@"data"]]; - [component setShouldLabelValues:NO]; - - if (i==0) - { - [component setColour:PCColorYellow]; - } - else if (i==1) - { - [component setColour:PCColorGreen]; - } - else if (i==2) - { - [component setColour:PCColorOrange]; - } - else if (i==3) - { - [component setColour:PCColorRed]; - } - else if (i==4) - { - [component setColour:PCColorBlue]; - } - - [components addObject:component]; - } - [_lineChartView setComponents:components]; - [_lineChartView setXLabels:[sampleInfo objectForKey:@"x_labels"]]; + if(!jsonError){ + NSMutableArray *components = [NSMutableArray array]; + for (int i=0; i<[[sampleInfo objectForKey:@"data"] count]; i++) + { + NSDictionary *point = [[sampleInfo objectForKey:@"data"] objectAtIndex:i]; + PCLineChartViewComponent *component = [[PCLineChartViewComponent alloc] init]; + [component setTitle:[point objectForKey:@"title"]]; + [component setPoints:[point objectForKey:@"data"]]; + [component setShouldLabelValues:NO]; + + if (i==0) + { + [component setColour:PCColorYellow]; + } + else if (i==1) + { + [component setColour:PCColorGreen]; + } + else if (i==2) + { + [component setColour:PCColorOrange]; + } + else if (i==3) + { + [component setColour:PCColorRed]; + } + else if (i==4) + { + [component setColour:PCColorBlue]; + } + + [components addObject:component]; + } + [_lineChartView setComponents:components]; + [_lineChartView setXLabels:[sampleInfo objectForKey:@"x_labels"]]; + }else{ + UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" + message:[jsonError localizedDescription] delegate:self + cancelButtonTitle:@"OK" otherButtonTitles: nil]; + [alertView show]; + } } return self; } From 855ec8ac205d87568041961f9062f1a5f940480f Mon Sep 17 00:00:00 2001 From: Mike Hollis Date: Mon, 2 Dec 2013 20:55:10 -0500 Subject: [PATCH 3/4] fixed initaliztion of variable --- iOSPlot/iPhone/LineChartViewController.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iOSPlot/iPhone/LineChartViewController.m b/iOSPlot/iPhone/LineChartViewController.m index 0747ff4..8a34dfe 100644 --- a/iOSPlot/iPhone/LineChartViewController.m +++ b/iOSPlot/iPhone/LineChartViewController.m @@ -40,7 +40,7 @@ - (id)init self = [super init]; if (self) { - NSError *jsonError; + NSError *jsonError = nil; [self.view setBackgroundColor:[UIColor colorWithWhite:1 alpha:1]]; [self setTitle:@"Line Chart"]; From bad164b95b7c98c8be89e6f43b904bc3b51e1285 Mon Sep 17 00:00:00 2001 From: Mike Hollis Date: Mon, 2 Dec 2013 22:02:59 -0500 Subject: [PATCH 4/4] Fixed issue #32 where the label wouldn't display if null data value is at the end of the list --- iOSPlot/Shared/PCLineChartView.m | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/iOSPlot/Shared/PCLineChartView.m b/iOSPlot/Shared/PCLineChartView.m index b8d57aa..c690e48 100644 --- a/iOSPlot/Shared/PCLineChartView.m +++ b/iOSPlot/Shared/PCLineChartView.m @@ -153,6 +153,8 @@ - (void)drawRect:(CGRect)rect { component.colour = PCColorBlue; } + int x = 0; + int y = 0; for (int x_axis_index=0; x_axis_index<[component.points count]; x_axis_index++) { id object = [component.points objectAtIndex:x_axis_index]; @@ -162,8 +164,8 @@ - (void)drawRect:(CGRect)rect { CGContextSetStrokeColorWithColor(ctx, [component.colour CGColor]); CGContextSetLineWidth(ctx, circle_stroke_width); - int x = margin + div_width*x_axis_index; - int y = top_margin + (scale_max-value)/self.interval*div_height; + x = margin + div_width*x_axis_index; + y = top_margin + (scale_max-value)/self.interval*div_height; CGRect circleRect = CGRectMake(x-circle_diameter/2, y-circle_diameter/2, circle_diameter,circle_diameter); CGContextStrokeEllipseInRect(ctx, circleRect); @@ -182,22 +184,22 @@ - (void)drawRect:(CGRect)rect { CGContextAddLineToPoint(ctx, x1, y1); CGContextStrokePath(ctx); } - - - if (x_axis_index==[component.points count]-1) { - NSMutableDictionary *info = [NSMutableDictionary dictionary]; - if (component.title) { - [info setObject:component.title forKey:@"title"]; - } - [info setObject:[NSNumber numberWithFloat:x+circle_diameter/2+15] forKey:@"x"]; - [info setObject:[NSNumber numberWithFloat:y-10] forKey:@"y"]; - [info setObject:component.colour forKey:@"colour"]; - [legends addObject:info]; - } - + last_x = x; last_y = y; } + + if (x_axis_index==[component.points count]-1) { + NSMutableDictionary *info = [NSMutableDictionary dictionary]; + if (component.title) { + [info setObject:component.title forKey:@"title"]; + } + [info setObject:[NSNumber numberWithFloat:x+circle_diameter/2+15] forKey:@"x"]; + [info setObject:[NSNumber numberWithFloat:y-10] forKey:@"y"]; + [info setObject:component.colour forKey:@"colour"]; + [legends addObject:info]; + } + } }