CubicLineChartViewController.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // CubicLineChartViewController.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 "CubicLineChartViewController.h"
  12. #import "ChartsDemo_iOS-Swift.h"
  13. @interface CubicLineSampleFillFormatter : NSObject <ChartFillFormatter>
  14. {
  15. }
  16. @end
  17. @implementation CubicLineSampleFillFormatter
  18. - (CGFloat)getFillLinePositionWithDataSet:(LineChartDataSet *)dataSet dataProvider:(id<LineChartDataProvider>)dataProvider
  19. {
  20. return -10.f;
  21. }
  22. @end
  23. @interface CubicLineChartViewController () <ChartViewDelegate>
  24. @property (nonatomic, strong) IBOutlet LineChartView *chartView;
  25. @property (nonatomic, strong) IBOutlet UISlider *sliderX;
  26. @property (nonatomic, strong) IBOutlet UISlider *sliderY;
  27. @property (nonatomic, strong) IBOutlet UITextField *sliderTextX;
  28. @property (nonatomic, strong) IBOutlet UITextField *sliderTextY;
  29. @end
  30. @implementation CubicLineChartViewController
  31. - (void)viewDidLoad
  32. {
  33. [super viewDidLoad];
  34. self.title = @"Cubic Line Chart";
  35. self.options = @[
  36. @{@"key": @"toggleValues", @"label": @"Toggle Values"},
  37. @{@"key": @"toggleFilled", @"label": @"Toggle Filled"},
  38. @{@"key": @"toggleCircles", @"label": @"Toggle Circles"},
  39. @{@"key": @"toggleCubic", @"label": @"Toggle Cubic"},
  40. @{@"key": @"toggleHorizontalCubic", @"label": @"Toggle Horizontal Cubic"},
  41. @{@"key": @"toggleStepped", @"label": @"Toggle Stepped"},
  42. @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
  43. @{@"key": @"animateX", @"label": @"Animate X"},
  44. @{@"key": @"animateY", @"label": @"Animate Y"},
  45. @{@"key": @"animateXY", @"label": @"Animate XY"},
  46. @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
  47. @{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
  48. @{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
  49. @{@"key": @"toggleData", @"label": @"Toggle Data"},
  50. ];
  51. _chartView.delegate = self;
  52. [_chartView setViewPortOffsetsWithLeft:0.f top:20.f right:0.f bottom:0.f];
  53. _chartView.backgroundColor = [UIColor colorWithRed:104/255.f green:241/255.f blue:175/255.f alpha:1.f];
  54. _chartView.chartDescription.enabled = NO;
  55. _chartView.dragEnabled = YES;
  56. [_chartView setScaleEnabled:YES];
  57. _chartView.pinchZoomEnabled = NO;
  58. _chartView.drawGridBackgroundEnabled = NO;
  59. _chartView.maxHighlightDistance = 300.0;
  60. _chartView.xAxis.enabled = NO;
  61. ChartYAxis *yAxis = _chartView.leftAxis;
  62. yAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.f];
  63. [yAxis setLabelCount:6 force:NO];
  64. yAxis.labelTextColor = UIColor.whiteColor;
  65. yAxis.labelPosition = YAxisLabelPositionInsideChart;
  66. yAxis.drawGridLinesEnabled = NO;
  67. yAxis.axisLineColor = UIColor.whiteColor;
  68. _chartView.rightAxis.enabled = NO;
  69. _chartView.legend.enabled = NO;
  70. _sliderX.value = 45.0;
  71. _sliderY.value = 100.0;
  72. [self slidersValueChanged:nil];
  73. [_chartView animateWithXAxisDuration:2.0 yAxisDuration:2.0];
  74. }
  75. - (void)didReceiveMemoryWarning
  76. {
  77. [super didReceiveMemoryWarning];
  78. // Dispose of any resources that can be recreated.
  79. }
  80. - (void)updateChartData
  81. {
  82. if (self.shouldHideData)
  83. {
  84. _chartView.data = nil;
  85. return;
  86. }
  87. [self setDataCount:_sliderX.value + 1 range:_sliderY.value];
  88. }
  89. - (void)setDataCount:(int)count range:(double)range
  90. {
  91. NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
  92. for (int i = 0; i < count; i++)
  93. {
  94. double mult = (range + 1);
  95. double val = (double) (arc4random_uniform(mult)) + 20;
  96. [yVals1 addObject:[[ChartDataEntry alloc] initWithX:i y:val]];
  97. }
  98. LineChartDataSet *set1 = nil;
  99. if (_chartView.data.dataSetCount > 0)
  100. {
  101. set1 = (LineChartDataSet *)_chartView.data.dataSets[0];
  102. [set1 replaceEntries:yVals1];
  103. [_chartView.data notifyDataChanged];
  104. [_chartView notifyDataSetChanged];
  105. }
  106. else
  107. {
  108. set1 = [[LineChartDataSet alloc] initWithEntries:yVals1 label:@"DataSet 1"];
  109. set1.mode = LineChartModeCubicBezier;
  110. set1.cubicIntensity = 0.2;
  111. set1.drawCirclesEnabled = NO;
  112. set1.lineWidth = 1.8;
  113. set1.circleRadius = 4.0;
  114. [set1 setCircleColor:UIColor.whiteColor];
  115. set1.highlightColor = [UIColor colorWithRed:244/255.f green:117/255.f blue:117/255.f alpha:1.f];
  116. [set1 setColor:UIColor.whiteColor];
  117. set1.fillColor = UIColor.whiteColor;
  118. set1.fillAlpha = 1.f;
  119. set1.drawHorizontalHighlightIndicatorEnabled = NO;
  120. set1.fillFormatter = [[CubicLineSampleFillFormatter alloc] init];
  121. LineChartData *data = [[LineChartData alloc] initWithDataSet:set1];
  122. [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:9.f]];
  123. [data setDrawValues:NO];
  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. set.mode = set.mode == LineChartModeStepped ? LineChartModeLinear : LineChartModeStepped;
  161. }
  162. [_chartView setNeedsDisplay];
  163. }
  164. if ([key isEqualToString:@"toggleHorizontalCubic"])
  165. {
  166. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  167. {
  168. set.mode = set.mode == LineChartModeCubicBezier ? LineChartModeHorizontalBezier : LineChartModeCubicBezier;
  169. }
  170. [_chartView setNeedsDisplay];
  171. return;
  172. }
  173. [super handleOption:key forChartView:_chartView];
  174. }
  175. #pragma mark - Actions
  176. - (IBAction)slidersValueChanged:(id)sender
  177. {
  178. _sliderTextX.text = [@((int)_sliderX.value) stringValue];
  179. _sliderTextY.text = [@((int)_sliderY.value) stringValue];
  180. [self updateChartData];
  181. }
  182. #pragma mark - ChartViewDelegate
  183. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
  184. {
  185. NSLog(@"chartValueSelected");
  186. }
  187. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  188. {
  189. NSLog(@"chartValueNothingSelected");
  190. }
  191. @end