Skip to content

Commit

Permalink
DRYed duplicate demo code
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Feb 28, 2016
1 parent 13bc03d commit c677463
Show file tree
Hide file tree
Showing 28 changed files with 178 additions and 1,291 deletions.
6 changes: 6 additions & 0 deletions ChartsDemo/Classes/DemoBaseViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
@property (nonatomic, strong) IBOutlet UIButton *optionsButton;
@property (nonatomic, strong) IBOutlet NSArray *options;

@property (nonatomic, assign) BOOL shouldHideData;

- (void)handleOption:(NSString *)key forChartView:(ChartViewBase *)chartView;

- (void)updateChartData;

- (void)setupPieChartView:(PieChartView *)chartView;
- (void)setupRadarChartView:(RadarChartView *)chartView;
- (void)setupBarLineChartView:(BarLineChartViewBase *)chartView;
Expand Down
76 changes: 76 additions & 0 deletions ChartsDemo/Classes/DemoBaseViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,77 @@ - (void)optionTapped:(NSString *)key

}

#pragma mark - Common option actions

- (void)handleOption:(NSString *)key forChartView:(ChartViewBase *)chartView
{
if ([key isEqualToString:@"toggleValues"])
{
for (id<IChartDataSet> set in chartView.data.dataSets)
{
set.drawValuesEnabled = !set.isDrawValuesEnabled;
}

[chartView setNeedsDisplay];
}

if ([key isEqualToString:@"toggleHighlight"])
{
chartView.data.highlightEnabled = !chartView.data.isHighlightEnabled;
[chartView setNeedsDisplay];
}

if ([key isEqualToString:@"animateX"])
{
[chartView animateWithXAxisDuration:3.0];
}

if ([key isEqualToString:@"animateY"])
{
[chartView animateWithYAxisDuration:3.0];
}

if ([key isEqualToString:@"animateXY"])
{
[chartView animateWithXAxisDuration:3.0 yAxisDuration:3.0];
}

if ([key isEqualToString:@"saveToGallery"])
{
[chartView saveToCameraRoll];
}

if ([key isEqualToString:@"togglePinchZoom"])
{
BarLineChartViewBase *barLineChart = (BarLineChartViewBase *)chartView;
barLineChart.pinchZoomEnabled = !barLineChart.isPinchZoomEnabled;

[chartView setNeedsDisplay];
}

if ([key isEqualToString:@"toggleAutoScaleMinMax"])
{
BarLineChartViewBase *barLineChart = (BarLineChartViewBase *)chartView;
barLineChart.autoScaleMinMaxEnabled = !barLineChart.isAutoScaleMinMaxEnabled;

[chartView notifyDataSetChanged];
}

if ([key isEqualToString:@"toggleHighlightArrow"])
{
BarChartView *barChart = (BarChartView *)chartView;
barChart.drawHighlightArrowEnabled = !barChart.isDrawHighlightArrowEnabled;

[chartView setNeedsDisplay];
}

if ([key isEqualToString:@"toggleData"])
{
_shouldHideData = !_shouldHideData;
[self updateChartData];
}
}

#pragma mark - Actions

- (IBAction)optionsButtonTapped:(id)sender
Expand Down Expand Up @@ -182,6 +253,11 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath

#pragma mark - Stubs for chart view

- (void)updateChartData
{
// Override this
}

- (void)setupPieChartView:(PieChartView *)chartView
{
chartView.usePercentValuesEnabled = YES;
Expand Down
59 changes: 2 additions & 57 deletions ChartsDemo/Classes/Demos/AnotherBarChartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ @interface AnotherBarChartViewController () <ChartViewDelegate>
@property (nonatomic, strong) IBOutlet UISlider *sliderY;
@property (nonatomic, strong) IBOutlet UITextField *sliderTextX;
@property (nonatomic, strong) IBOutlet UITextField *sliderTextY;
@property (nonatomic, assign) BOOL shouldHideData;

@end

Expand Down Expand Up @@ -78,7 +77,7 @@ - (void)didReceiveMemoryWarning

- (void)updateChartData
{
if (_shouldHideData)
if (self.shouldHideData)
{
_chartView.data = nil;
return;
Expand Down Expand Up @@ -118,61 +117,7 @@ - (void)setDataCount:(int)count range:(double)range

- (void)optionTapped:(NSString *)key
{
if ([key isEqualToString:@"toggleValues"])
{
for (id<IChartDataSet> set in _chartView.data.dataSets)
{
set.drawValuesEnabled = !set.isDrawValuesEnabled;
}

[_chartView setNeedsDisplay];
}

if ([key isEqualToString:@"toggleHighlight"])
{
_chartView.data.highlightEnabled = !_chartView.data.isHighlightEnabled;
[_chartView setNeedsDisplay];
}

if ([key isEqualToString:@"toggleHighlightArrow"])
{
_chartView.drawHighlightArrowEnabled = !_chartView.isDrawHighlightArrowEnabled;

[_chartView setNeedsDisplay];
}

if ([key isEqualToString:@"animateX"])
{
[_chartView animateWithXAxisDuration:3.0];
}

if ([key isEqualToString:@"animateY"])
{
[_chartView animateWithYAxisDuration:3.0];
}

if ([key isEqualToString:@"animateXY"])
{
[_chartView animateWithXAxisDuration:3.0 yAxisDuration:3.0];
}

if ([key isEqualToString:@"saveToGallery"])
{
[_chartView saveToCameraRoll];
}

if ([key isEqualToString:@"togglePinchZoom"])
{
_chartView.pinchZoomEnabled = !_chartView.isPinchZoomEnabled;

[_chartView setNeedsDisplay];
}

if ([key isEqualToString:@"toggleData"])
{
_shouldHideData = !_shouldHideData;
[self updateChartData];
}
[super handleOption:key forChartView:_chartView];
}

#pragma mark - Actions
Expand Down
66 changes: 2 additions & 64 deletions ChartsDemo/Classes/Demos/BarChartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ @interface BarChartViewController () <ChartViewDelegate>
@property (nonatomic, strong) IBOutlet UISlider *sliderY;
@property (nonatomic, strong) IBOutlet UITextField *sliderTextX;
@property (nonatomic, strong) IBOutlet UITextField *sliderTextY;
@property (nonatomic, assign) BOOL shouldHideData;

@end

Expand Down Expand Up @@ -100,7 +99,7 @@ - (void)didReceiveMemoryWarning

- (void)updateChartData
{
if (_shouldHideData)
if (self.shouldHideData)
{
_chartView.data = nil;
return;
Expand Down Expand Up @@ -141,68 +140,7 @@ - (void)setDataCount:(int)count range:(double)range

- (void)optionTapped:(NSString *)key
{
if ([key isEqualToString:@"toggleValues"])
{
for (id<IChartDataSet> set in _chartView.data.dataSets)
{
set.drawValuesEnabled = !set.isDrawValuesEnabled;
}

[_chartView setNeedsDisplay];
}

if ([key isEqualToString:@"toggleHighlight"])
{
_chartView.data.highlightEnabled = !_chartView.data.isHighlightEnabled;
[_chartView setNeedsDisplay];
}

if ([key isEqualToString:@"toggleHighlightArrow"])
{
_chartView.drawHighlightArrowEnabled = !_chartView.isDrawHighlightArrowEnabled;

[_chartView setNeedsDisplay];
}


if ([key isEqualToString:@"animateX"])
{
[_chartView animateWithXAxisDuration:3.0];
}

if ([key isEqualToString:@"animateY"])
{
[_chartView animateWithYAxisDuration:3.0];
}

if ([key isEqualToString:@"animateXY"])
{
[_chartView animateWithXAxisDuration:3.0 yAxisDuration:3.0];
}

if ([key isEqualToString:@"saveToGallery"])
{
[_chartView saveToCameraRoll];
}

if ([key isEqualToString:@"togglePinchZoom"])
{
_chartView.pinchZoomEnabled = !_chartView.isPinchZoomEnabled;

[_chartView setNeedsDisplay];
}

if ([key isEqualToString:@"toggleAutoScaleMinMax"])
{
_chartView.autoScaleMinMaxEnabled = !_chartView.isAutoScaleMinMaxEnabled;
[_chartView notifyDataSetChanged];
}

if ([key isEqualToString:@"toggleData"])
{
_shouldHideData = !_shouldHideData;
[self updateChartData];
}
[super handleOption:key forChartView:_chartView];
}

#pragma mark - Actions
Expand Down
58 changes: 2 additions & 56 deletions ChartsDemo/Classes/Demos/BubbleChartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ @interface BubbleChartViewController () <ChartViewDelegate>
@property (nonatomic, strong) IBOutlet UISlider *sliderY;
@property (nonatomic, strong) IBOutlet UITextField *sliderTextX;
@property (nonatomic, strong) IBOutlet UITextField *sliderTextY;
@property (nonatomic, assign) BOOL shouldHideData;

@end

Expand Down Expand Up @@ -83,7 +82,7 @@ - (void)didReceiveMemoryWarning

- (void)updateChartData
{
if (_shouldHideData)
if (self.shouldHideData)
{
_chartView.data = nil;
return;
Expand Down Expand Up @@ -145,60 +144,7 @@ - (void)setDataCount:(int)count range:(double)range

- (void)optionTapped:(NSString *)key
{
if ([key isEqualToString:@"toggleValues"])
{
for (id<IChartDataSet> set in _chartView.data.dataSets)
{
set.drawValuesEnabled = !set.isDrawValuesEnabled;
}

[_chartView setNeedsDisplay];
}

if ([key isEqualToString:@"toggleHighlight"])
{
_chartView.data.highlightEnabled = !_chartView.data.isHighlightEnabled;
[_chartView setNeedsDisplay];
}

if ([key isEqualToString:@"animateX"])
{
[_chartView animateWithXAxisDuration:3.0];
}

if ([key isEqualToString:@"animateY"])
{
[_chartView animateWithYAxisDuration:3.0];
}

if ([key isEqualToString:@"animateXY"])
{
[_chartView animateWithXAxisDuration:3.0 yAxisDuration:3.0];
}

if ([key isEqualToString:@"saveToGallery"])
{
[_chartView saveToCameraRoll];
}

if ([key isEqualToString:@"togglePinchZoom"])
{
_chartView.pinchZoomEnabled = !_chartView.isPinchZoomEnabled;

[_chartView setNeedsDisplay];
}

if ([key isEqualToString:@"toggleAutoScaleMinMax"])
{
_chartView.autoScaleMinMaxEnabled = !_chartView.isAutoScaleMinMaxEnabled;
[_chartView notifyDataSetChanged];
}

if ([key isEqualToString:@"toggleData"])
{
_shouldHideData = !_shouldHideData;
[self updateChartData];
}
[super handleOption:key forChartView:_chartView];
}

#pragma mark - Actions
Expand Down
Loading

0 comments on commit c677463

Please sign in to comment.