// // CubicLineChartViewController.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 "CubicLineChartViewController.h" #import "ChartsDemo_iOS-Swift.h" @interface CubicLineSampleFillFormatter : NSObject { } @end @implementation CubicLineSampleFillFormatter - (CGFloat)getFillLinePositionWithDataSet:(LineChartDataSet *)dataSet dataProvider:(id)dataProvider { return -10.f; } @end @interface CubicLineChartViewController () @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 CubicLineChartViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"Cubic Line Chart"; self.options = @[ @{@"key": @"toggleValues", @"label": @"Toggle Values"}, @{@"key": @"toggleFilled", @"label": @"Toggle Filled"}, @{@"key": @"toggleCircles", @"label": @"Toggle Circles"}, @{@"key": @"toggleCubic", @"label": @"Toggle Cubic"}, @{@"key": @"toggleHorizontalCubic", @"label": @"Toggle Horizontal 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 setViewPortOffsetsWithLeft:0.f top:20.f right:0.f bottom:0.f]; _chartView.backgroundColor = [UIColor colorWithRed:104/255.f green:241/255.f blue:175/255.f alpha:1.f]; _chartView.chartDescription.enabled = NO; _chartView.dragEnabled = YES; [_chartView setScaleEnabled:YES]; _chartView.pinchZoomEnabled = NO; _chartView.drawGridBackgroundEnabled = NO; _chartView.maxHighlightDistance = 300.0; _chartView.xAxis.enabled = NO; ChartYAxis *yAxis = _chartView.leftAxis; yAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.f]; [yAxis setLabelCount:6 force:NO]; yAxis.labelTextColor = UIColor.whiteColor; yAxis.labelPosition = YAxisLabelPositionInsideChart; yAxis.drawGridLinesEnabled = NO; yAxis.axisLineColor = UIColor.whiteColor; _chartView.rightAxis.enabled = NO; _chartView.legend.enabled = NO; _sliderX.value = 45.0; _sliderY.value = 100.0; [self slidersValueChanged:nil]; [_chartView animateWithXAxisDuration:2.0 yAxisDuration:2.0]; } - (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 + 1 range:_sliderY.value]; } - (void)setDataCount:(int)count range:(double)range { NSMutableArray *yVals1 = [[NSMutableArray alloc] init]; for (int i = 0; i < count; i++) { double mult = (range + 1); double val = (double) (arc4random_uniform(mult)) + 20; [yVals1 addObject:[[ChartDataEntry alloc] initWithX:i y:val]]; } LineChartDataSet *set1 = nil; if (_chartView.data.dataSetCount > 0) { set1 = (LineChartDataSet *)_chartView.data.dataSets[0]; [set1 replaceEntries:yVals1]; [_chartView.data notifyDataChanged]; [_chartView notifyDataSetChanged]; } else { set1 = [[LineChartDataSet alloc] initWithEntries:yVals1 label:@"DataSet 1"]; set1.mode = LineChartModeCubicBezier; set1.cubicIntensity = 0.2; set1.drawCirclesEnabled = NO; set1.lineWidth = 1.8; set1.circleRadius = 4.0; [set1 setCircleColor:UIColor.whiteColor]; set1.highlightColor = [UIColor colorWithRed:244/255.f green:117/255.f blue:117/255.f alpha:1.f]; [set1 setColor:UIColor.whiteColor]; set1.fillColor = UIColor.whiteColor; set1.fillAlpha = 1.f; set1.drawHorizontalHighlightIndicatorEnabled = NO; set1.fillFormatter = [[CubicLineSampleFillFormatter alloc] init]; LineChartData *data = [[LineChartData alloc] initWithDataSet:set1]; [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:9.f]]; [data setDrawValues:NO]; _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) { set.mode = set.mode == LineChartModeStepped ? LineChartModeLinear : LineChartModeStepped; } [_chartView setNeedsDisplay]; } if ([key isEqualToString:@"toggleHorizontalCubic"]) { for (id set in _chartView.data.dataSets) { set.mode = set.mode == LineChartModeCubicBezier ? LineChartModeHorizontalBezier : LineChartModeCubicBezier; } [_chartView setNeedsDisplay]; return; } [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