ChartsDemo-iOS/Objective-C/DemoBaseViewController.m (245 lines of code) (raw):

// // 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