// // LineChartTimeViewController.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 "LineChartTimeViewController.h" #import "ChartsDemo_iOS-Swift.h" #import "DateValueFormatter.h" @interface LineChartTimeViewController () @property (nonatomic, strong) IBOutlet LineChartView *chartView; @property (nonatomic, strong) IBOutlet UISlider *sliderX; @property (nonatomic, strong) IBOutlet UITextField *sliderTextX; @end @implementation LineChartTimeViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"Time 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.chartDescription.enabled = NO; _chartView.dragEnabled = YES; [_chartView setScaleEnabled:YES]; _chartView.pinchZoomEnabled = NO; _chartView.drawGridBackgroundEnabled = NO; _chartView.highlightPerDragEnabled = YES; _chartView.backgroundColor = UIColor.whiteColor; _chartView.legend.enabled = NO; ChartXAxis *xAxis = _chartView.xAxis; xAxis.labelPosition = XAxisLabelPositionTopInside; xAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]; xAxis.labelTextColor = [UIColor colorWithRed:255/255.0 green:192/255.0 blue:56/255.0 alpha:1.0]; xAxis.drawAxisLineEnabled = NO; xAxis.drawGridLinesEnabled = YES; xAxis.centerAxisLabelsEnabled = YES; xAxis.granularity = 3600.0; xAxis.valueFormatter = [[DateValueFormatter alloc] init]; ChartYAxis *leftAxis = _chartView.leftAxis; leftAxis.labelPosition = YAxisLabelPositionInsideChart; leftAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.f]; leftAxis.labelTextColor = [UIColor colorWithRed:51/255.0 green:181/255.0 blue:229/255.0 alpha:1.0]; leftAxis.drawGridLinesEnabled = YES; leftAxis.granularityEnabled = YES; leftAxis.axisMinimum = 0.0; leftAxis.axisMaximum = 170.0; leftAxis.yOffset = -9.0; leftAxis.labelTextColor = [UIColor colorWithRed:255/255.0 green:192/255.0 blue:56/255.0 alpha:1.0]; _chartView.rightAxis.enabled = NO; _chartView.legend.form = ChartLegendFormLine; _sliderX.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:30.0]; } - (void)setDataCount:(int)count range:(double)range { NSTimeInterval now = [[NSDate date] timeIntervalSince1970]; NSTimeInterval hourSeconds = 3600.0; NSMutableArray *values = [[NSMutableArray alloc] init]; NSTimeInterval from = now - (count / 2.0) * hourSeconds; NSTimeInterval to = now + (count / 2.0) * hourSeconds; for (NSTimeInterval x = from; x < to; x += hourSeconds) { double y = arc4random_uniform(range) + 50; [values addObject:[[ChartDataEntry alloc] initWithX:x y:y]]; } LineChartDataSet *set1 = nil; if (_chartView.data.dataSetCount > 0) { set1 = (LineChartDataSet *)_chartView.data.dataSets[0]; [set1 replaceEntries: values]; [_chartView.data notifyDataChanged]; [_chartView notifyDataSetChanged]; } else { set1 = [[LineChartDataSet alloc] initWithEntries:values label:@"DataSet 1"]; set1.axisDependency = AxisDependencyLeft; set1.valueTextColor = [UIColor colorWithRed:51/255.0 green:181/255.0 blue:229/255.0 alpha:1.0]; set1.lineWidth = 1.5; set1.drawCirclesEnabled = NO; set1.drawValuesEnabled = NO; set1.fillAlpha = 0.26; set1.fillColor = [UIColor colorWithRed:51/255.0 green:181/255.0 blue:229/255.0 alpha:1.0]; set1.highlightColor = [UIColor colorWithRed:224/255.0 green:117/255.0 blue:117/255.0 alpha:1.0]; set1.drawCircleHoleEnabled = NO; NSMutableArray *dataSets = [[NSMutableArray alloc] init]; [dataSets addObject:set1]; LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets]; [data setValueTextColor:UIColor.whiteColor]; [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:9.0]]; _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]; } 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]; [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