LineChart1ViewController.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // LineChart1ViewController.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 "LineChart1ViewController.h"
  12. #import "ChartsDemo_iOS-Swift.h"
  13. @interface LineChart1ViewController () <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 LineChart1ViewController
  21. - (void)viewDidLoad
  22. {
  23. [super viewDidLoad];
  24. self.title = @"Line Chart 1";
  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": @"toggleIcons", @"label": @"Toggle Icons"},
  32. @{@"key": @"toggleStepped", @"label": @"Toggle Stepped"},
  33. @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
  34. @{@"key": @"animateX", @"label": @"Animate X"},
  35. @{@"key": @"animateY", @"label": @"Animate Y"},
  36. @{@"key": @"animateXY", @"label": @"Animate XY"},
  37. @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
  38. @{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
  39. @{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
  40. @{@"key": @"toggleData", @"label": @"Toggle Data"},
  41. ];
  42. _chartView.delegate = self;
  43. _chartView.chartDescription.enabled = NO;
  44. _chartView.dragEnabled = YES;
  45. [_chartView setScaleEnabled:YES];
  46. _chartView.pinchZoomEnabled = YES;
  47. _chartView.drawGridBackgroundEnabled = NO;
  48. // x-axis limit line
  49. ChartLimitLine *llXAxis = [[ChartLimitLine alloc] initWithLimit:10.0 label:@"Index 10"];
  50. llXAxis.lineWidth = 4.0;
  51. llXAxis.lineDashLengths = @[@(10.f), @(10.f), @(0.f)];
  52. llXAxis.labelPosition = ChartLimitLabelPositionRightBottom;
  53. llXAxis.valueFont = [UIFont systemFontOfSize:10.f];
  54. //[_chartView.xAxis addLimitLine:llXAxis];
  55. _chartView.xAxis.gridLineDashLengths = @[@10.0, @10.0];
  56. _chartView.xAxis.gridLineDashPhase = 0.f;
  57. ChartLimitLine *ll1 = [[ChartLimitLine alloc] initWithLimit:150.0 label:@"Upper Limit"];
  58. ll1.lineWidth = 4.0;
  59. ll1.lineDashLengths = @[@5.f, @5.f];
  60. ll1.labelPosition = ChartLimitLabelPositionRightTop;
  61. ll1.valueFont = [UIFont systemFontOfSize:10.0];
  62. ChartLimitLine *ll2 = [[ChartLimitLine alloc] initWithLimit:-30.0 label:@"Lower Limit"];
  63. ll2.lineWidth = 4.0;
  64. ll2.lineDashLengths = @[@5.f, @5.f];
  65. ll2.labelPosition = ChartLimitLabelPositionRightBottom;
  66. ll2.valueFont = [UIFont systemFontOfSize:10.0];
  67. ChartYAxis *leftAxis = _chartView.leftAxis;
  68. [leftAxis removeAllLimitLines];
  69. [leftAxis addLimitLine:ll1];
  70. [leftAxis addLimitLine:ll2];
  71. leftAxis.axisMaximum = 200.0;
  72. leftAxis.axisMinimum = -50.0;
  73. leftAxis.gridLineDashLengths = @[@5.f, @5.f];
  74. leftAxis.drawZeroLineEnabled = NO;
  75. leftAxis.drawLimitLinesBehindDataEnabled = YES;
  76. _chartView.rightAxis.enabled = NO;
  77. //[_chartView.viewPortHandler setMaximumScaleY: 2.f];
  78. //[_chartView.viewPortHandler setMaximumScaleX: 2.f];
  79. BalloonMarker *marker = [[BalloonMarker alloc]
  80. initWithColor: [UIColor colorWithWhite:180/255. alpha:1.0]
  81. font: [UIFont systemFontOfSize:12.0]
  82. textColor: UIColor.whiteColor
  83. insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0)];
  84. marker.chartView = _chartView;
  85. marker.minimumSize = CGSizeMake(80.f, 40.f);
  86. _chartView.marker = marker;
  87. _chartView.legend.form = ChartLegendFormLine;
  88. _sliderX.value = 45.0;
  89. _sliderY.value = 100.0;
  90. [self slidersValueChanged:nil];
  91. [_chartView animateWithXAxisDuration:2.5];
  92. }
  93. - (void)didReceiveMemoryWarning
  94. {
  95. [super didReceiveMemoryWarning];
  96. // Dispose of any resources that can be recreated.
  97. }
  98. - (void)updateChartData
  99. {
  100. if (self.shouldHideData)
  101. {
  102. _chartView.data = nil;
  103. return;
  104. }
  105. [self setDataCount:_sliderX.value range:_sliderY.value];
  106. }
  107. - (void)setDataCount:(int)count range:(double)range
  108. {
  109. NSMutableArray *values = [[NSMutableArray alloc] init];
  110. for (int i = 0; i < count; i++)
  111. {
  112. double val = arc4random_uniform(range) + 3;
  113. [values addObject:[[ChartDataEntry alloc] initWithX:i y:val icon: [UIImage imageNamed:@"icon"]]];
  114. }
  115. LineChartDataSet *set1 = nil;
  116. if (_chartView.data.dataSetCount > 0)
  117. {
  118. set1 = (LineChartDataSet *)_chartView.data.dataSets[0];
  119. [set1 replaceEntries: values];
  120. [_chartView.data notifyDataChanged];
  121. [_chartView notifyDataSetChanged];
  122. }
  123. else
  124. {
  125. set1 = [[LineChartDataSet alloc] initWithEntries:values label:@"DataSet 1"];
  126. set1.drawIconsEnabled = NO;
  127. set1.lineDashLengths = @[@5.f, @2.5f];
  128. set1.highlightLineDashLengths = @[@5.f, @2.5f];
  129. [set1 setColor:UIColor.blackColor];
  130. [set1 setCircleColor:UIColor.blackColor];
  131. set1.lineWidth = 1.0;
  132. set1.circleRadius = 3.0;
  133. set1.drawCircleHoleEnabled = NO;
  134. set1.valueFont = [UIFont systemFontOfSize:9.f];
  135. set1.formLineDashLengths = @[@5.f, @2.5f];
  136. set1.formLineWidth = 1.0;
  137. set1.formSize = 15.0;
  138. NSArray *gradientColors = @[
  139. (id)[ChartColorTemplates colorFromString:@"#00ff0000"].CGColor,
  140. (id)[ChartColorTemplates colorFromString:@"#ffff0000"].CGColor
  141. ];
  142. CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
  143. set1.fillAlpha = 1.f;
  144. set1.fill = [[ChartLinearGradientFill alloc] initWithGradient:gradient angle:90.0f];
  145. set1.drawFilledEnabled = YES;
  146. CGGradientRelease(gradient);
  147. NSMutableArray *dataSets = [[NSMutableArray alloc] init];
  148. [dataSets addObject:set1];
  149. LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
  150. _chartView.data = data;
  151. }
  152. }
  153. - (void)optionTapped:(NSString *)key
  154. {
  155. if ([key isEqualToString:@"toggleFilled"])
  156. {
  157. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  158. {
  159. set.drawFilledEnabled = !set.isDrawFilledEnabled;
  160. }
  161. [_chartView setNeedsDisplay];
  162. return;
  163. }
  164. if ([key isEqualToString:@"toggleCircles"])
  165. {
  166. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  167. {
  168. set.drawCirclesEnabled = !set.isDrawCirclesEnabled;
  169. }
  170. [_chartView setNeedsDisplay];
  171. return;
  172. }
  173. if ([key isEqualToString:@"toggleCubic"])
  174. {
  175. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  176. {
  177. set.mode = set.mode == LineChartModeCubicBezier ? LineChartModeLinear : LineChartModeCubicBezier;
  178. }
  179. [_chartView setNeedsDisplay];
  180. return;
  181. }
  182. if ([key isEqualToString:@"toggleStepped"])
  183. {
  184. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  185. {
  186. switch (set.mode) {
  187. case LineChartModeLinear:
  188. case LineChartModeCubicBezier:
  189. case LineChartModeHorizontalBezier:
  190. set.mode = LineChartModeStepped;
  191. break;
  192. case LineChartModeStepped: set.mode = LineChartModeLinear;
  193. }
  194. }
  195. [_chartView setNeedsDisplay];
  196. }
  197. if ([key isEqualToString:@"toggleHorizontalCubic"])
  198. {
  199. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  200. {
  201. set.mode = set.mode == LineChartModeHorizontalBezier ? LineChartModeCubicBezier : LineChartModeHorizontalBezier;
  202. }
  203. [_chartView setNeedsDisplay];
  204. return;
  205. }
  206. [super handleOption:key forChartView:_chartView];
  207. }
  208. #pragma mark - Actions
  209. - (IBAction)slidersValueChanged:(id)sender
  210. {
  211. _sliderTextX.text = [@((int)_sliderX.value) stringValue];
  212. _sliderTextY.text = [@((int)_sliderY.value) stringValue];
  213. [self updateChartData];
  214. }
  215. #pragma mark - ChartViewDelegate
  216. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
  217. {
  218. NSLog(@"chartValueSelected");
  219. }
  220. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  221. {
  222. NSLog(@"chartValueNothingSelected");
  223. }
  224. @end