Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polar chart addition #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions TWRCharts/TWRChartView.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ typedef void(^TWRAnimationCompletionBlock)(BOOL finished);
*/
- (void)loadCircularChart:(TWRCircularChart *)circularChart withCompletionHandler:(TWRAnimationCompletionBlock)block;

- (void)loadChartFromString:(NSString *)chartString;

@end
14 changes: 14 additions & 0 deletions TWRCharts/TWRChartView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

This file was deleted.

3 changes: 2 additions & 1 deletion TWRChartsDemo/ChartJS/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<segmentedControl opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="mPm-5C-unx">
<segmentedControl opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="3" translatesAutoresizingMaskIntoConstraints="NO" id="mPm-5C-unx">
<rect key="frame" x="20" y="520" width="280" height="29"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<segments>
<segment title="Line"/>
<segment title="Bar"/>
<segment title="Pie"/>
<segment title="Polar"/>
</segments>
</segmentedControl>
</subviews>
Expand Down
6 changes: 6 additions & 0 deletions TWRChartsDemo/ChartJS/TWRChartView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions TWRChartsDemo/ChartJS/TWRChartView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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];
Expand All @@ -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];
Expand Down
32 changes: 27 additions & 5 deletions TWRChartsDemo/ChartJS/TWRViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

#import "TWRViewController.h"
#import "TWRChart.h"
#import "TWRChartView.h"

typedef NS_ENUM(NSInteger, ChartsType) {
Line,
Bar,
Pie,
Polar
};

@interface TWRViewController ()

Expand Down Expand Up @@ -116,28 +124,42 @@ - (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;

case Polar:{
[self loadPolarChart];
}
break;
default:
break;
}
}

- (void)loadPolarChart
{
//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];
}

@end