// // MultipleLinesChartViewController.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 "MultipleLinesChartViewController.h" #import "ChartsDemo_iOS-Swift.h" @interface MultipleLinesChartViewController () @property (nonatomic, strong) IBOutlet LineChartView *chartView; @property (nonatomic, strong) IBOutlet UISlider *sliderX; @property (nonatomic, strong) IBOutlet UISlider *sliderY; @property (nonatomic, strong) IBOutlet UITextField *sliderTextX; @property (nonatomic, strong) IBOutlet UITextField *sliderTextY; @end @implementation MultipleLinesChartViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"Multiple Lines Chart"; self.options = @[ @{@"key": @"toggleValues", @"label": @"Toggle Values"}, @{@"key": @"toggleFilled", @"label": @"Toggle Filled"}, @{@"key": @"toggleCircles", @"label": @"Toggle Circles"}, @{@"key": @"toggleCubic", @"label": @"Toggle Cubic"}, @{@"key": @"toggleStepped", @"label": @"Toggle Stepped"}, @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"}, @{@"key": @"animateX", @"label": @"Animate X"}, @{@"key": @"animateY", @"label": @"Animate Y"}, @{@"key": @"animateXY", @"label": @"Animate XY"}, @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"}, @{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"}, @{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"}, @{@"key": @"toggleData", @"label": @"Toggle Data"}, ]; _chartView.delegate = self; _chartView.chartDescription.enabled = NO; _chartView.leftAxis.enabled = NO; _chartView.rightAxis.drawAxisLineEnabled = NO; _chartView.rightAxis.drawGridLinesEnabled = NO; _chartView.xAxis.drawAxisLineEnabled = NO; _chartView.xAxis.drawGridLinesEnabled = NO; _chartView.drawGridBackgroundEnabled = NO; _chartView.drawBordersEnabled = NO; _chartView.dragEnabled = YES; [_chartView setScaleEnabled:YES]; _chartView.pinchZoomEnabled = NO; ChartLegend *l = _chartView.legend; l.horizontalAlignment = ChartLegendHorizontalAlignmentRight; l.verticalAlignment = ChartLegendVerticalAlignmentTop; l.orientation = ChartLegendOrientationVertical; l.drawInside = NO; _sliderX.value = 20.0; _sliderY.value = 100.0; [self slidersValueChanged:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)updateChartData { if (self.shouldHideData) { _chartView.data = nil; return; } [self setDataCount:_sliderX.value range:_sliderY.value]; } - (void)setDataCount:(int)count range:(double)range { NSArray *colors = @[ChartColorTemplates.vordiplom[0], ChartColorTemplates.vordiplom[1], ChartColorTemplates.vordiplom[2]]; NSMutableArray *dataSets = [[NSMutableArray alloc] init]; for (int z = 0; z < 3; z++) { NSMutableArray *values = [[NSMutableArray alloc] init]; for (int i = 0; i < count; i++) { double val = (double) (arc4random_uniform(range) + 3); [values addObject:[[ChartDataEntry alloc] initWithX:i y:val]]; } LineChartDataSet *d = [[LineChartDataSet alloc] initWithEntries:values label:[NSString stringWithFormat:@"DataSet %d", z + 1]]; d.lineWidth = 2.5; d.circleRadius = 4.0; d.circleHoleRadius = 2.0; UIColor *color = colors[z % colors.count]; [d setColor:color]; [d setCircleColor:color]; [dataSets addObject:d]; } ((LineChartDataSet *)dataSets[0]).lineDashLengths = @[@5.f, @5.f]; ((LineChartDataSet *)dataSets[0]).colors = ChartColorTemplates.vordiplom; ((LineChartDataSet *)dataSets[0]).circleColors = ChartColorTemplates.vordiplom; LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets]; [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:7.f]]; _chartView.data = data; } - (void)optionTapped:(NSString *)key { if ([key isEqualToString:@"toggleFilled"]) { for (id set in _chartView.data.dataSets) { set.drawFilledEnabled = !set.isDrawFilledEnabled; } [_chartView setNeedsDisplay]; return; } if ([key isEqualToString:@"toggleCircles"]) { for (id set in _chartView.data.dataSets) { set.drawCirclesEnabled = !set.isDrawCirclesEnabled; } [_chartView setNeedsDisplay]; return; } if ([key isEqualToString:@"toggleCubic"]) { for (id set in _chartView.data.dataSets) { set.mode = set.mode == LineChartModeCubicBezier ? LineChartModeLinear : LineChartModeCubicBezier; } [_chartView setNeedsDisplay]; return; } if ([key isEqualToString:@"toggleStepped"]) { for (id set in _chartView.data.dataSets) { switch (set.mode) { case LineChartModeLinear: case LineChartModeCubicBezier: case LineChartModeHorizontalBezier: set.mode = LineChartModeStepped; break; case LineChartModeStepped: set.mode = LineChartModeLinear; } } [_chartView setNeedsDisplay]; } [super handleOption:key forChartView:_chartView]; } #pragma mark - Actions - (IBAction)slidersValueChanged:(id)sender { _sliderTextX.text = [@((int)_sliderX.value) stringValue]; _sliderTextY.text = [@((int)_sliderY.value) stringValue]; [self updateChartData]; } #pragma mark - ChartViewDelegate - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight { NSLog(@"chartValueSelected"); } - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView { NSLog(@"chartValueNothingSelected"); } @end