123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- //
- // 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 () <ChartViewDelegate>
- @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<LineChartDataSetProtocol> set in _chartView.data.dataSets)
- {
- set.drawFilledEnabled = !set.isDrawFilledEnabled;
- }
-
- [_chartView setNeedsDisplay];
- return;
- }
-
- if ([key isEqualToString:@"toggleCircles"])
- {
- for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
- {
- set.drawCirclesEnabled = !set.isDrawCirclesEnabled;
- }
-
- [_chartView setNeedsDisplay];
- return;
- }
-
- if ([key isEqualToString:@"toggleCubic"])
- {
- for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
- {
- set.mode = set.mode == LineChartModeCubicBezier ? LineChartModeLinear : LineChartModeCubicBezier;
- }
-
- [_chartView setNeedsDisplay];
- return;
- }
- if ([key isEqualToString:@"toggleStepped"])
- {
- for (id<LineChartDataSetProtocol> 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<LineChartDataSetProtocol> 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
|