123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- //
- // DemoBaseViewController.m
- // ChartsDemo
- //
- // Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
- // A port of MPAndroidChart for iOS
- // Licensed under Apache License 2.0
- //
- // https://github.com/danielgindi/Charts
- //
- #import "DemoBaseViewController.h"
- @interface DemoBaseViewController () <UITableViewDelegate, UITableViewDataSource>
- @property (nonatomic, strong) UITableView *optionsTableView;
- @end
- @implementation DemoBaseViewController
- - (id)initWithCoder:(NSCoder *)aDecoder
- {
- self = [super initWithCoder:aDecoder];
- if (self)
- {
- [self initialize];
- }
- return self;
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self)
- {
- [self initialize];
- }
- return self;
- }
- - (void)initialize
- {
- self.edgesForExtendedLayout = UIRectEdgeNone;
-
- parties = @[
- @"Party A", @"Party B", @"Party C", @"Party D", @"Party E", @"Party F",
- @"Party G", @"Party H", @"Party I", @"Party J", @"Party K", @"Party L",
- @"Party M", @"Party N", @"Party O", @"Party P", @"Party Q", @"Party R",
- @"Party S", @"Party T", @"Party U", @"Party V", @"Party W", @"Party X",
- @"Party Y", @"Party Z"
- ];
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)optionTapped:(NSString *)key
- {
-
- }
- #pragma mark - Common option actions
- - (void)handleOption:(NSString *)key forChartView:(ChartViewBase *)chartView
- {
- if ([key isEqualToString:@"toggleValues"])
- {
- for (id<ChartDataSetProtocol> set in chartView.data.dataSets)
- {
- set.drawValuesEnabled = !set.isDrawValuesEnabled;
- }
-
- [chartView setNeedsDisplay];
- }
-
- if ([key isEqualToString:@"toggleIcons"])
- {
- for (id<ChartDataSetProtocol> set in chartView.data.dataSets)
- {
- set.drawIconsEnabled = !set.isDrawIconsEnabled;
- }
-
- [chartView setNeedsDisplay];
- }
-
- if ([key isEqualToString:@"toggleHighlight"])
- {
- chartView.data.isHighlightEnabled = !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"])
- {
- UIImageWriteToSavedPhotosAlbum([chartView getChartImageWithTransparent:NO], nil, nil, nil);
- }
-
- 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:@"toggleData"])
- {
- _shouldHideData = !_shouldHideData;
- [self updateChartData];
- }
-
- if ([key isEqualToString:@"toggleBarBorders"])
- {
- for (id<BarChartDataSetProtocol, NSObject> set in chartView.data.dataSets)
- {
- if ([set conformsToProtocol:@protocol(BarChartDataSetProtocol)])
- {
- set.barBorderWidth = set.barBorderWidth == 1.0 ? 0.0 : 1.0;
- }
- }
-
- [chartView setNeedsDisplay];
- }
- }
- #pragma mark - Actions
- - (IBAction)optionsButtonTapped:(id)sender
- {
- if (_optionsTableView)
- {
- [_optionsTableView removeFromSuperview];
- self.optionsTableView = nil;
- return;
- }
-
- self.optionsTableView = [[UITableView alloc] init];
- _optionsTableView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.9f];
- _optionsTableView.delegate = self;
- _optionsTableView.dataSource = self;
- [_optionsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
- _optionsTableView.translatesAutoresizingMaskIntoConstraints = NO;
-
- NSMutableArray *constraints = [[NSMutableArray alloc] init];
-
- [constraints addObject:[NSLayoutConstraint constraintWithItem:_optionsTableView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.f constant:40.f]];
-
- [constraints addObject:[NSLayoutConstraint constraintWithItem:_optionsTableView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:sender attribute:NSLayoutAttributeTrailing multiplier:1.f constant:0]];
-
- [constraints addObject:[NSLayoutConstraint constraintWithItem:_optionsTableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:sender attribute:NSLayoutAttributeBottom multiplier:1.f constant:5.f]];
-
- [self.view addSubview:_optionsTableView];
-
- [self.view addConstraints:constraints];
-
- [_optionsTableView addConstraints:@[
- [NSLayoutConstraint constraintWithItem:_optionsTableView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.f constant:220.f]
- ]];
- }
- #pragma mark - UITableViewDelegate, UITableViewDataSource
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- if (tableView == _optionsTableView)
- {
- return 1;
- }
-
- return 0;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- if (tableView == _optionsTableView)
- {
- return self.options.count;
- }
-
- return 0;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (tableView == _optionsTableView)
- {
- return 40.0;
- }
-
- return 44.0;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
- cell.backgroundView = nil;
- cell.backgroundColor = UIColor.clearColor;
- cell.textLabel.textColor = UIColor.whiteColor;
- cell.textLabel.text = self.options[indexPath.row][@"label"];
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (tableView == _optionsTableView)
- {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
-
- if (_optionsTableView)
- {
- [_optionsTableView removeFromSuperview];
- self.optionsTableView = nil;
- }
-
- [self optionTapped:self.options[indexPath.row][@"key"]];
- }
- }
- #pragma mark - Stubs for chart view
- - (void)updateChartData
- {
- // Override this
- }
- - (void)setupPieChartView:(PieChartView *)chartView
- {
- chartView.usePercentValuesEnabled = YES;
- chartView.drawSlicesUnderHoleEnabled = NO;
- chartView.holeRadiusPercent = 0.58;
- chartView.transparentCircleRadiusPercent = 0.61;
- chartView.chartDescription.enabled = NO;
- [chartView setExtraOffsetsWithLeft:5.f top:10.f right:5.f bottom:5.f];
-
- chartView.drawCenterTextEnabled = YES;
-
- NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
- paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
- paragraphStyle.alignment = NSTextAlignmentCenter;
-
- NSMutableAttributedString *centerText = [[NSMutableAttributedString alloc] initWithString:@"Charts\nby Daniel Cohen Gindi"];
- [centerText setAttributes:@{
- NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:13.f],
- NSParagraphStyleAttributeName: paragraphStyle
- } range:NSMakeRange(0, centerText.length)];
- [centerText addAttributes:@{
- NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f],
- NSForegroundColorAttributeName: UIColor.grayColor
- } range:NSMakeRange(10, centerText.length - 10)];
- [centerText addAttributes:@{
- NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-LightItalic" size:11.f],
- NSForegroundColorAttributeName: [UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]
- } range:NSMakeRange(centerText.length - 19, 19)];
- chartView.centerAttributedText = centerText;
-
- chartView.drawHoleEnabled = YES;
- chartView.rotationAngle = 0.0;
- chartView.rotationEnabled = YES;
- chartView.highlightPerTapEnabled = YES;
-
- ChartLegend *l = chartView.legend;
- l.horizontalAlignment = ChartLegendHorizontalAlignmentRight;
- l.verticalAlignment = ChartLegendVerticalAlignmentTop;
- l.orientation = ChartLegendOrientationVertical;
- l.drawInside = NO;
- l.xEntrySpace = 7.0;
- l.yEntrySpace = 0.0;
- l.yOffset = 0.0;
- }
- - (void)setupRadarChartView:(RadarChartView *)chartView
- {
- chartView.chartDescription.enabled = NO;
- }
- - (void)setupBarLineChartView:(BarLineChartViewBase *)chartView
- {
- chartView.chartDescription.enabled = NO;
-
- chartView.drawGridBackgroundEnabled = NO;
-
- chartView.dragEnabled = YES;
- [chartView setScaleEnabled:YES];
- chartView.pinchZoomEnabled = NO;
-
- // ChartYAxis *leftAxis = chartView.leftAxis;
-
- ChartXAxis *xAxis = chartView.xAxis;
- xAxis.labelPosition = XAxisLabelPositionBottom;
-
- chartView.rightAxis.enabled = NO;
- }
- @end
|