From fb6cc81656e2c177ed161ddf3eb522ea7b969d66 Mon Sep 17 00:00:00 2001 From: Petr Korolev Date: Fri, 9 May 2014 13:25:13 +0300 Subject: [PATCH 1/6] Fix bug, when start Demo - Line chart is selected, but PolarChart showing. Add custom method for laoading chart from string. Use this method and add Polar chart view to demo. --- TWRCharts/TWRChartView.h | 2 ++ TWRCharts/TWRChartView.m | 14 ++++++++ .../ChartJS/Base.lproj/Main.storyboard | 3 +- TWRChartsDemo/ChartJS/TWRChartView.h | 6 ++++ TWRChartsDemo/ChartJS/TWRViewController.m | 32 ++++++++++++++++--- 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/TWRCharts/TWRChartView.h b/TWRCharts/TWRChartView.h index 4cf8a22..0f51a2a 100644 --- a/TWRCharts/TWRChartView.h +++ b/TWRCharts/TWRChartView.h @@ -62,4 +62,6 @@ typedef void(^TWRAnimationCompletionBlock)(BOOL finished); */ - (void)loadCircularChart:(TWRCircularChart *)circularChart withCompletionHandler:(TWRAnimationCompletionBlock)block; +- (void)loadChartFromString:(NSString *)chartString; + @end diff --git a/TWRCharts/TWRChartView.m b/TWRCharts/TWRChartView.m index c23006f..d6fc568 100644 --- a/TWRCharts/TWRChartView.m +++ b/TWRCharts/TWRChartView.m @@ -111,6 +111,20 @@ - (void)loadCircularChart:(TWRCircularChart *)circularChart withCompletionHandle [self loadCircularChart:circularChart]; } +- (void)loadChartFromString:(NSString *)chartString +{ + if ([chartString isKindOfClass:[NSString class]]) { + _jsFileString = chartString; + [self stringByEvaluatingJavaScriptFromString:_jsFileString]; + [self loadIndex]; + } else { + NSException *exception = [NSException exceptionWithName:@"TWRChartInvalicChartElement" + reason:@"The element object provided to the chart view is not a valid circular chart." + userInfo:nil]; + [exception raise]; + } +} + #pragma mark - Private API - (void)loadIndex { diff --git a/TWRChartsDemo/ChartJS/Base.lproj/Main.storyboard b/TWRChartsDemo/ChartJS/Base.lproj/Main.storyboard index dbd753a..e7a0a2a 100644 --- a/TWRChartsDemo/ChartJS/Base.lproj/Main.storyboard +++ b/TWRChartsDemo/ChartJS/Base.lproj/Main.storyboard @@ -16,13 +16,14 @@ - + + diff --git a/TWRChartsDemo/ChartJS/TWRChartView.h b/TWRChartsDemo/ChartJS/TWRChartView.h index 4cf8a22..6182aa9 100644 --- a/TWRChartsDemo/ChartJS/TWRChartView.h +++ b/TWRChartsDemo/ChartJS/TWRChartView.h @@ -62,4 +62,10 @@ typedef void(^TWRAnimationCompletionBlock)(BOOL finished); */ - (void)loadCircularChart:(TWRCircularChart *)circularChart withCompletionHandler:(TWRAnimationCompletionBlock)block; +/** + * Loading a custom chart from string + * + * @param chartString the string with custom chart data + */ +- (void)loadChartFromString:(NSString *)chartString; @end diff --git a/TWRChartsDemo/ChartJS/TWRViewController.m b/TWRChartsDemo/ChartJS/TWRViewController.m index 73e5788..5a064d5 100644 --- a/TWRChartsDemo/ChartJS/TWRViewController.m +++ b/TWRChartsDemo/ChartJS/TWRViewController.m @@ -8,6 +8,15 @@ #import "TWRViewController.h" #import "TWRChart.h" +#import "TWRChartView.h" + +typedef NS_ENUM(NSInteger, ChartsType) { + Line, + Bar, + Pie, + Polar, + chartsTypeCount +}; @interface TWRViewController () @@ -116,28 +125,43 @@ - (void)loadPieChart { #pragma mark - UISegmentedController switch methods - (void)switchChart:(UISegmentedControl *)sender { - switch (sender.selectedSegmentIndex) { + ChartsType type = (ChartsType) sender.selectedSegmentIndex; + switch (type) { //Line - case 0: { + case Line: { [self loadLineChart]; } break; //Bar - case 1: { + case Bar: { [self loadBarChart]; } break; //Pie - case 2: { + case Pie: { [self loadPieChart]; } break; default: break; + case Polar:{ + [self loadPolarChart]; + } + break; + case chartsTypeCount:break; } } +- (void)loadPolarChart +{ + // Load chart by using a ChartJS javascript file + NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"js"]; + NSData *data = [NSData dataWithContentsOfFile:jsFilePath]; + NSString *jsString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + [_chartView loadChartFromString:jsString]; +} + @end From 6d002c26daf898a301d3f663a9216cd1000bd91c Mon Sep 17 00:00:00 2001 From: Petr Korolev Date: Fri, 9 May 2014 13:28:57 +0300 Subject: [PATCH 2/6] add comments --- TWRChartsDemo/ChartJS/TWRViewController.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TWRChartsDemo/ChartJS/TWRViewController.m b/TWRChartsDemo/ChartJS/TWRViewController.m index 5a064d5..42e061f 100644 --- a/TWRChartsDemo/ChartJS/TWRViewController.m +++ b/TWRChartsDemo/ChartJS/TWRViewController.m @@ -157,10 +157,11 @@ - (void)switchChart:(UISegmentedControl *)sender { - (void)loadPolarChart { - // Load chart by using a ChartJS javascript file + //todo implement polar chart parser in the future (now Polar char just loading from file index.js NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"js"]; NSData *data = [NSData dataWithContentsOfFile:jsFilePath]; NSString *jsString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + [_chartView loadChartFromString:jsString]; } From 27eebacd7216565ca288f2eb61705a5e70eec00d Mon Sep 17 00:00:00 2001 From: Petr Korolev Date: Fri, 9 May 2014 14:20:39 +0300 Subject: [PATCH 3/6] remove file from gitignore list --- .../xcshareddata/ChartJS.xccheckout | 41 ------------------- 1 file changed, 41 deletions(-) delete mode 100644 TWRChartsDemo/ChartJS.xcodeproj/project.xcworkspace/xcshareddata/ChartJS.xccheckout diff --git a/TWRChartsDemo/ChartJS.xcodeproj/project.xcworkspace/xcshareddata/ChartJS.xccheckout b/TWRChartsDemo/ChartJS.xcodeproj/project.xcworkspace/xcshareddata/ChartJS.xccheckout deleted file mode 100644 index 7856b57..0000000 --- a/TWRChartsDemo/ChartJS.xcodeproj/project.xcworkspace/xcshareddata/ChartJS.xccheckout +++ /dev/null @@ -1,41 +0,0 @@ - - - - - IDESourceControlProjectFavoriteDictionaryKey - - IDESourceControlProjectIdentifier - 00E03A49-C07C-4535-B4BA-A21B8C0ABA6D - IDESourceControlProjectName - ChartJS - IDESourceControlProjectOriginsDictionary - - D9970D84-C374-4B68-ADD9-3F22B8947B4D - https://github.com/chasseurmic/TWRCharts.git - - IDESourceControlProjectPath - TWRChartsDemo/ChartJS.xcodeproj/project.xcworkspace - IDESourceControlProjectRelativeInstallPathDictionary - - D9970D84-C374-4B68-ADD9-3F22B8947B4D - ../../.. - - IDESourceControlProjectURL - https://github.com/chasseurmic/TWRCharts.git - IDESourceControlProjectVersion - 110 - IDESourceControlProjectWCCIdentifier - D9970D84-C374-4B68-ADD9-3F22B8947B4D - IDESourceControlProjectWCConfigurations - - - IDESourceControlRepositoryExtensionIdentifierKey - public.vcs.git - IDESourceControlWCCIdentifierKey - D9970D84-C374-4B68-ADD9-3F22B8947B4D - IDESourceControlWCCName - TWRCharts - - - - From 3fa470759b015f0485014e2a531335158715d0b9 Mon Sep 17 00:00:00 2001 From: Petr Korolev Date: Fri, 9 May 2014 14:21:28 +0300 Subject: [PATCH 4/6] prettifying code --- TWRChartsDemo/ChartJS/TWRViewController.m | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/TWRChartsDemo/ChartJS/TWRViewController.m b/TWRChartsDemo/ChartJS/TWRViewController.m index 42e061f..174f2d9 100644 --- a/TWRChartsDemo/ChartJS/TWRViewController.m +++ b/TWRChartsDemo/ChartJS/TWRViewController.m @@ -14,8 +14,7 @@ typedef NS_ENUM(NSInteger, ChartsType) { Line, Bar, Pie, - Polar, - chartsTypeCount + Polar }; @interface TWRViewController () @@ -144,14 +143,12 @@ - (void)switchChart:(UISegmentedControl *)sender { [self loadPieChart]; } break; - - default: - break; case Polar:{ [self loadPolarChart]; } break; - case chartsTypeCount:break; + default: + break; } } From 447308948b5e5481a9582aecd88dbec6cb9e394a Mon Sep 17 00:00:00 2001 From: Petr Date: Fri, 9 May 2014 14:49:16 +0300 Subject: [PATCH 5/6] Update README.md update how to load personal js files. Since example from readme not working correctly. --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c746c40..3970ddd 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,10 @@ Drop in your Xcode project a .js file and make sure it's been added to the resou Then just get a handle on the file and set its path to the TWRChartView that's being added to the controller's view. ```objc -NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"js"]; -[_chartView setChartJsFilePath:jsFilePath]; +NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"js"]; +NSData *data = [NSData dataWithContentsOfFile:jsFilePath]; +NSString *jsString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; +[_chartView loadChartFromString:jsString]; ``` You can use any of the chart types currently supported by [ChartJS](http://www.chartjs.org). Here's an example of how you would load a Polar Chart. From 86676096683baf725cbc5f3af1eb235920fb7665 Mon Sep 17 00:00:00 2001 From: Petr Korolev Date: Thu, 3 Jul 2014 11:03:55 +0300 Subject: [PATCH 6/6] fix typo --- TWRChartsDemo/ChartJS/TWRChartView.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TWRChartsDemo/ChartJS/TWRChartView.m b/TWRChartsDemo/ChartJS/TWRChartView.m index 7243e84..a568aaa 100644 --- a/TWRChartsDemo/ChartJS/TWRChartView.m +++ b/TWRChartsDemo/ChartJS/TWRChartView.m @@ -62,7 +62,7 @@ - (void)loadBarChart:(TWRBarChart *)barChart { [self stringByEvaluatingJavaScriptFromString:_jsFileString]; [self loadIndex]; } else { - NSException *exception = [NSException exceptionWithName:@"TWRChartInvalicChartElement" + NSException *exception = [NSException exceptionWithName:@"TWRChartInvalidChartElement" reason:@"The element object provided to the chart view is not a valid bar chart." userInfo:nil]; [exception raise]; @@ -80,7 +80,7 @@ - (void)loadLineChart:(TWRLineChart *)lineChart { [self stringByEvaluatingJavaScriptFromString:_jsFileString]; [self loadIndex]; } else { - NSException *exception = [NSException exceptionWithName:@"TWRChartInvalicChartElement" + NSException *exception = [NSException exceptionWithName:@"TWRChartInvalidChartElement" reason:@"The element object provided to the chart view is not a valid line chart." userInfo:nil]; [exception raise]; @@ -98,7 +98,7 @@ - (void)loadCircularChart:(TWRCircularChart *)circularChart { [self stringByEvaluatingJavaScriptFromString:_jsFileString]; [self loadIndex]; } else { - NSException *exception = [NSException exceptionWithName:@"TWRChartInvalicChartElement" + NSException *exception = [NSException exceptionWithName:@"TWRChartInvalidChartElement" reason:@"The element object provided to the chart view is not a valid circular chart." userInfo:nil]; [exception raise];