LineChart2ViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // LineChart2ViewController.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 "LineChart2ViewController.h"
  12. #import "ChartsDemo_iOS-Swift.h"
  13. @interface LineChart2ViewController () <ChartViewDelegate>
  14. @property (nonatomic, strong) IBOutlet LineChartView *chartView;
  15. @property (nonatomic, strong) IBOutlet UISlider *sliderX;
  16. @property (nonatomic, strong) IBOutlet UISlider *sliderY;
  17. @property (nonatomic, strong) IBOutlet UITextField *sliderTextX;
  18. @property (nonatomic, strong) IBOutlet UITextField *sliderTextY;
  19. @end
  20. @implementation LineChart2ViewController
  21. - (void)viewDidLoad
  22. {
  23. [super viewDidLoad];
  24. self.title = @"Line Chart 2";
  25. self.options = @[
  26. @{@"key": @"toggleValues", @"label": @"Toggle Values"},
  27. @{@"key": @"toggleFilled", @"label": @"Toggle Filled"},
  28. @{@"key": @"toggleCircles", @"label": @"Toggle Circles"},
  29. @{@"key": @"toggleCubic", @"label": @"Toggle Cubic"},
  30. @{@"key": @"toggleHorizontalCubic", @"label": @"Toggle Horizontal Cubic"},
  31. @{@"key": @"toggleStepped", @"label": @"Toggle Stepped"},
  32. @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
  33. @{@"key": @"animateX", @"label": @"Animate X"},
  34. @{@"key": @"animateY", @"label": @"Animate Y"},
  35. @{@"key": @"animateXY", @"label": @"Animate XY"},
  36. @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
  37. @{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
  38. @{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
  39. @{@"key": @"toggleData", @"label": @"Toggle Data"},
  40. ];
  41. _chartView.delegate = self;
  42. _chartView.chartDescription.enabled = NO;
  43. _chartView.dragEnabled = YES;
  44. [_chartView setScaleEnabled:YES];
  45. _chartView.drawGridBackgroundEnabled = NO;
  46. _chartView.pinchZoomEnabled = YES;
  47. _chartView.backgroundColor = [UIColor colorWithWhite:204/255.f alpha:1.f];
  48. ChartLegend *l = _chartView.legend;
  49. l.form = ChartLegendFormLine;
  50. l.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
  51. l.textColor = UIColor.whiteColor;
  52. l.horizontalAlignment = ChartLegendHorizontalAlignmentLeft;
  53. l.verticalAlignment = ChartLegendVerticalAlignmentBottom;
  54. l.orientation = ChartLegendOrientationHorizontal;
  55. l.drawInside = NO;
  56. ChartXAxis *xAxis = _chartView.xAxis;
  57. xAxis.labelFont = [UIFont systemFontOfSize:11.f];
  58. xAxis.labelTextColor = UIColor.whiteColor;
  59. xAxis.drawGridLinesEnabled = NO;
  60. xAxis.drawAxisLineEnabled = NO;
  61. ChartYAxis *leftAxis = _chartView.leftAxis;
  62. leftAxis.labelTextColor = [UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f];
  63. leftAxis.axisMaximum = 200.0;
  64. leftAxis.axisMinimum = 0.0;
  65. leftAxis.drawGridLinesEnabled = YES;
  66. leftAxis.drawZeroLineEnabled = NO;
  67. leftAxis.granularityEnabled = YES;
  68. ChartYAxis *rightAxis = _chartView.rightAxis;
  69. rightAxis.labelTextColor = UIColor.redColor;
  70. rightAxis.axisMaximum = 900.0;
  71. rightAxis.axisMinimum = -200.0;
  72. rightAxis.drawGridLinesEnabled = NO;
  73. rightAxis.granularityEnabled = NO;
  74. _sliderX.value = 20.0;
  75. _sliderY.value = 30.0;
  76. [self slidersValueChanged:nil];
  77. [_chartView animateWithXAxisDuration:2.5];
  78. }
  79. - (void)didReceiveMemoryWarning
  80. {
  81. [super didReceiveMemoryWarning];
  82. // Dispose of any resources that can be recreated.
  83. }
  84. - (void)updateChartData
  85. {
  86. if (self.shouldHideData)
  87. {
  88. _chartView.data = nil;
  89. return;
  90. }
  91. [self setDataCount:_sliderX.value + 1 range:_sliderY.value];
  92. }
  93. - (void)setDataCount:(int)count range:(double)range
  94. {
  95. NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
  96. NSMutableArray *yVals2 = [[NSMutableArray alloc] init];
  97. NSMutableArray *yVals3 = [[NSMutableArray alloc] init];
  98. for (int i = 0; i < count; i++)
  99. {
  100. double mult = range / 2.0;
  101. double val = (double) (arc4random_uniform(mult)) + 50;
  102. [yVals1 addObject:[[ChartDataEntry alloc] initWithX:i y:val]];
  103. }
  104. for (int i = 0; i < count - 1; i++)
  105. {
  106. double mult = range;
  107. double val = (double) (arc4random_uniform(mult)) + 450;
  108. [yVals2 addObject:[[ChartDataEntry alloc] initWithX:i y:val]];
  109. }
  110. for (int i = 0; i < count; i++)
  111. {
  112. double mult = range;
  113. double val = (double) (arc4random_uniform(mult)) + 500;
  114. [yVals3 addObject:[[ChartDataEntry alloc] initWithX:i y:val]];
  115. }
  116. LineChartDataSet *set1 = nil, *set2 = nil, *set3 = nil;
  117. if (_chartView.data.dataSetCount > 0)
  118. {
  119. set1 = (LineChartDataSet *)_chartView.data.dataSets[0];
  120. set2 = (LineChartDataSet *)_chartView.data.dataSets[1];
  121. set3 = (LineChartDataSet *)_chartView.data.dataSets[2];
  122. [set1 replaceEntries:yVals1];
  123. [set2 replaceEntries:yVals2];
  124. [set3 replaceEntries:yVals3];
  125. [_chartView.data notifyDataChanged];
  126. [_chartView notifyDataSetChanged];
  127. }
  128. else
  129. {
  130. set1 = [[LineChartDataSet alloc] initWithEntries:yVals1 label:@"DataSet 1"];
  131. set1.axisDependency = AxisDependencyLeft;
  132. [set1 setColor:[UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]];
  133. [set1 setCircleColor:UIColor.whiteColor];
  134. set1.lineWidth = 2.0;
  135. set1.circleRadius = 3.0;
  136. set1.fillAlpha = 65/255.0;
  137. set1.fillColor = [UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f];
  138. set1.highlightColor = [UIColor colorWithRed:244/255.f green:117/255.f blue:117/255.f alpha:1.f];
  139. set1.drawCircleHoleEnabled = NO;
  140. set2 = [[LineChartDataSet alloc] initWithEntries:yVals2 label:@"DataSet 2"];
  141. set2.axisDependency = AxisDependencyRight;
  142. [set2 setColor:UIColor.redColor];
  143. [set2 setCircleColor:UIColor.whiteColor];
  144. set2.lineWidth = 2.0;
  145. set2.circleRadius = 3.0;
  146. set2.fillAlpha = 65/255.0;
  147. set2.fillColor = UIColor.redColor;
  148. set2.highlightColor = [UIColor colorWithRed:244/255.f green:117/255.f blue:117/255.f alpha:1.f];
  149. set2.drawCircleHoleEnabled = NO;
  150. set3 = [[LineChartDataSet alloc] initWithEntries:yVals3 label:@"DataSet 3"];
  151. set3.axisDependency = AxisDependencyRight;
  152. [set3 setColor:UIColor.yellowColor];
  153. [set3 setCircleColor:UIColor.whiteColor];
  154. set3.lineWidth = 2.0;
  155. set3.circleRadius = 3.0;
  156. set3.fillAlpha = 65/255.0;
  157. set3.fillColor = [UIColor.yellowColor colorWithAlphaComponent:200/255.f];
  158. set3.highlightColor = [UIColor colorWithRed:244/255.f green:117/255.f blue:117/255.f alpha:1.f];
  159. set3.drawCircleHoleEnabled = NO;
  160. NSMutableArray *dataSets = [[NSMutableArray alloc] init];
  161. [dataSets addObject:set1];
  162. [dataSets addObject:set2];
  163. [dataSets addObject:set3];
  164. LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
  165. [data setValueTextColor:UIColor.whiteColor];
  166. [data setValueFont:[UIFont systemFontOfSize:9.f]];
  167. _chartView.data = data;
  168. }
  169. }
  170. - (void)optionTapped:(NSString *)key
  171. {
  172. if ([key isEqualToString:@"toggleFilled"])
  173. {
  174. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  175. {
  176. set.drawFilledEnabled = !set.isDrawFilledEnabled;
  177. }
  178. [_chartView setNeedsDisplay];
  179. return;
  180. }
  181. if ([key isEqualToString:@"toggleCircles"])
  182. {
  183. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  184. {
  185. set.drawCirclesEnabled = !set.isDrawCirclesEnabled;
  186. }
  187. [_chartView setNeedsDisplay];
  188. return;
  189. }
  190. if ([key isEqualToString:@"toggleCubic"])
  191. {
  192. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  193. {
  194. set.mode = set.mode == LineChartModeCubicBezier ? LineChartModeLinear : LineChartModeCubicBezier;
  195. }
  196. [_chartView setNeedsDisplay];
  197. return;
  198. }
  199. if ([key isEqualToString:@"toggleStepped"])
  200. {
  201. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  202. {
  203. switch (set.mode) {
  204. case LineChartModeLinear:
  205. case LineChartModeCubicBezier:
  206. case LineChartModeHorizontalBezier:
  207. set.mode = LineChartModeStepped;
  208. break;
  209. case LineChartModeStepped: set.mode = LineChartModeLinear;
  210. }
  211. }
  212. [_chartView setNeedsDisplay];
  213. }
  214. if ([key isEqualToString:@"toggleHorizontalCubic"])
  215. {
  216. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  217. {
  218. set.mode = set.mode == LineChartModeCubicBezier ? LineChartModeHorizontalBezier : LineChartModeCubicBezier;
  219. }
  220. [_chartView setNeedsDisplay];
  221. return;
  222. }
  223. [super handleOption:key forChartView:_chartView];
  224. }
  225. #pragma mark - Actions
  226. - (IBAction)slidersValueChanged:(id)sender
  227. {
  228. _sliderTextX.text = [@((int)_sliderX.value) stringValue];
  229. _sliderTextY.text = [@((int)_sliderY.value) stringValue];
  230. [self updateChartData];
  231. }
  232. #pragma mark - ChartViewDelegate
  233. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
  234. {
  235. NSLog(@"chartValueSelected");
  236. [_chartView centerViewToAnimatedWithXValue:entry.x yValue:entry.y axis:[_chartView.data dataSetAtIndex:highlight.dataSetIndex].axisDependency duration:1.0];
  237. //[_chartView moveViewToAnimatedWithXValue:entry.x yValue:entry.y axis:[_chartView.data getDataSetByIndex:dataSetIndex].axisDependency duration:1.0];
  238. //[_chartView zoomAndCenterViewAnimatedWithScaleX:1.8 scaleY:1.8 xValue:entry.x yValue:entry.y axis:[_chartView.data getDataSetByIndex:dataSetIndex].axisDependency duration:1.0];
  239. }
  240. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  241. {
  242. NSLog(@"chartValueNothingSelected");
  243. }
  244. @end