LineChartTimeViewController.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // LineChartTimeViewController.m
  3. // ChartsDemo
  4. //
  5. // Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
  6. // A port of MPAndroidChart for iOS
  7. // Licensed under Apache License 2.0
  8. //
  9. // https://github.com/danielgindi/Charts
  10. //
  11. #import "LineChartTimeViewController.h"
  12. #import "ChartsDemo_iOS-Swift.h"
  13. #import "DateValueFormatter.h"
  14. @interface LineChartTimeViewController () <ChartViewDelegate>
  15. @property (nonatomic, strong) IBOutlet LineChartView *chartView;
  16. @property (nonatomic, strong) IBOutlet UISlider *sliderX;
  17. @property (nonatomic, strong) IBOutlet UITextField *sliderTextX;
  18. @end
  19. @implementation LineChartTimeViewController
  20. - (void)viewDidLoad
  21. {
  22. [super viewDidLoad];
  23. self.title = @"Time Line Chart";
  24. self.options = @[
  25. @{@"key": @"toggleValues", @"label": @"Toggle Values"},
  26. @{@"key": @"toggleFilled", @"label": @"Toggle Filled"},
  27. @{@"key": @"toggleCircles", @"label": @"Toggle Circles"},
  28. @{@"key": @"toggleCubic", @"label": @"Toggle Cubic"},
  29. @{@"key": @"toggleHorizontalCubic", @"label": @"Toggle Horizontal Cubic"},
  30. @{@"key": @"toggleStepped", @"label": @"Toggle Stepped"},
  31. @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
  32. @{@"key": @"animateX", @"label": @"Animate X"},
  33. @{@"key": @"animateY", @"label": @"Animate Y"},
  34. @{@"key": @"animateXY", @"label": @"Animate XY"},
  35. @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
  36. @{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
  37. @{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
  38. @{@"key": @"toggleData", @"label": @"Toggle Data"},
  39. ];
  40. _chartView.delegate = self;
  41. _chartView.chartDescription.enabled = NO;
  42. _chartView.dragEnabled = YES;
  43. [_chartView setScaleEnabled:YES];
  44. _chartView.pinchZoomEnabled = NO;
  45. _chartView.drawGridBackgroundEnabled = NO;
  46. _chartView.highlightPerDragEnabled = YES;
  47. _chartView.backgroundColor = UIColor.whiteColor;
  48. _chartView.legend.enabled = NO;
  49. ChartXAxis *xAxis = _chartView.xAxis;
  50. xAxis.labelPosition = XAxisLabelPositionTopInside;
  51. xAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
  52. xAxis.labelTextColor = [UIColor colorWithRed:255/255.0 green:192/255.0 blue:56/255.0 alpha:1.0];
  53. xAxis.drawAxisLineEnabled = NO;
  54. xAxis.drawGridLinesEnabled = YES;
  55. xAxis.centerAxisLabelsEnabled = YES;
  56. xAxis.granularity = 3600.0;
  57. xAxis.valueFormatter = [[DateValueFormatter alloc] init];
  58. ChartYAxis *leftAxis = _chartView.leftAxis;
  59. leftAxis.labelPosition = YAxisLabelPositionInsideChart;
  60. leftAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.f];
  61. leftAxis.labelTextColor = [UIColor colorWithRed:51/255.0 green:181/255.0 blue:229/255.0 alpha:1.0];
  62. leftAxis.drawGridLinesEnabled = YES;
  63. leftAxis.granularityEnabled = YES;
  64. leftAxis.axisMinimum = 0.0;
  65. leftAxis.axisMaximum = 170.0;
  66. leftAxis.yOffset = -9.0;
  67. leftAxis.labelTextColor = [UIColor colorWithRed:255/255.0 green:192/255.0 blue:56/255.0 alpha:1.0];
  68. _chartView.rightAxis.enabled = NO;
  69. _chartView.legend.form = ChartLegendFormLine;
  70. _sliderX.value = 100.0;
  71. [self slidersValueChanged:nil];
  72. }
  73. - (void)didReceiveMemoryWarning
  74. {
  75. [super didReceiveMemoryWarning];
  76. // Dispose of any resources that can be recreated.
  77. }
  78. - (void)updateChartData
  79. {
  80. if (self.shouldHideData)
  81. {
  82. _chartView.data = nil;
  83. return;
  84. }
  85. [self setDataCount:_sliderX.value range:30.0];
  86. }
  87. - (void)setDataCount:(int)count range:(double)range
  88. {
  89. NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
  90. NSTimeInterval hourSeconds = 3600.0;
  91. NSMutableArray *values = [[NSMutableArray alloc] init];
  92. NSTimeInterval from = now - (count / 2.0) * hourSeconds;
  93. NSTimeInterval to = now + (count / 2.0) * hourSeconds;
  94. for (NSTimeInterval x = from; x < to; x += hourSeconds)
  95. {
  96. double y = arc4random_uniform(range) + 50;
  97. [values addObject:[[ChartDataEntry alloc] initWithX:x y:y]];
  98. }
  99. LineChartDataSet *set1 = nil;
  100. if (_chartView.data.dataSetCount > 0)
  101. {
  102. set1 = (LineChartDataSet *)_chartView.data.dataSets[0];
  103. [set1 replaceEntries: values];
  104. [_chartView.data notifyDataChanged];
  105. [_chartView notifyDataSetChanged];
  106. }
  107. else
  108. {
  109. set1 = [[LineChartDataSet alloc] initWithEntries:values label:@"DataSet 1"];
  110. set1.axisDependency = AxisDependencyLeft;
  111. set1.valueTextColor = [UIColor colorWithRed:51/255.0 green:181/255.0 blue:229/255.0 alpha:1.0];
  112. set1.lineWidth = 1.5;
  113. set1.drawCirclesEnabled = NO;
  114. set1.drawValuesEnabled = NO;
  115. set1.fillAlpha = 0.26;
  116. set1.fillColor = [UIColor colorWithRed:51/255.0 green:181/255.0 blue:229/255.0 alpha:1.0];
  117. set1.highlightColor = [UIColor colorWithRed:224/255.0 green:117/255.0 blue:117/255.0 alpha:1.0];
  118. set1.drawCircleHoleEnabled = NO;
  119. NSMutableArray *dataSets = [[NSMutableArray alloc] init];
  120. [dataSets addObject:set1];
  121. LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
  122. [data setValueTextColor:UIColor.whiteColor];
  123. [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:9.0]];
  124. _chartView.data = data;
  125. }
  126. }
  127. - (void)optionTapped:(NSString *)key
  128. {
  129. if ([key isEqualToString:@"toggleFilled"])
  130. {
  131. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  132. {
  133. set.drawFilledEnabled = !set.isDrawFilledEnabled;
  134. }
  135. [_chartView setNeedsDisplay];
  136. return;
  137. }
  138. if ([key isEqualToString:@"toggleCircles"])
  139. {
  140. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  141. {
  142. set.drawCirclesEnabled = !set.isDrawCirclesEnabled;
  143. }
  144. [_chartView setNeedsDisplay];
  145. return;
  146. }
  147. if ([key isEqualToString:@"toggleCubic"])
  148. {
  149. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  150. {
  151. set.mode = set.mode == LineChartModeCubicBezier ? LineChartModeLinear : LineChartModeCubicBezier;
  152. }
  153. [_chartView setNeedsDisplay];
  154. return;
  155. }
  156. if ([key isEqualToString:@"toggleStepped"])
  157. {
  158. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  159. {
  160. switch (set.mode) {
  161. case LineChartModeLinear:
  162. case LineChartModeCubicBezier:
  163. case LineChartModeHorizontalBezier:
  164. set.mode = LineChartModeStepped;
  165. break;
  166. case LineChartModeStepped: set.mode = LineChartModeLinear;
  167. }
  168. }
  169. [_chartView setNeedsDisplay];
  170. }
  171. if ([key isEqualToString:@"toggleHorizontalCubic"])
  172. {
  173. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  174. {
  175. set.mode = set.mode == LineChartModeCubicBezier ? LineChartModeHorizontalBezier : LineChartModeCubicBezier;
  176. }
  177. [_chartView setNeedsDisplay];
  178. return;
  179. }
  180. [super handleOption:key forChartView:_chartView];
  181. }
  182. #pragma mark - Actions
  183. - (IBAction)slidersValueChanged:(id)sender
  184. {
  185. _sliderTextX.text = [@((int)_sliderX.value) stringValue];
  186. [self updateChartData];
  187. }
  188. #pragma mark - ChartViewDelegate
  189. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
  190. {
  191. NSLog(@"chartValueSelected");
  192. }
  193. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  194. {
  195. NSLog(@"chartValueNothingSelected");
  196. }
  197. @end