RadarChartViewController.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // RadarChartViewController.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 "RadarChartViewController.h"
  12. #import "ChartsDemo_iOS-Swift.h"
  13. @interface RadarChartViewController () <ChartViewDelegate, ChartAxisValueFormatter>
  14. @property (nonatomic, strong) IBOutlet RadarChartView *chartView;
  15. @property (nonatomic, strong) NSArray<NSString *> *activities;
  16. @property (nonatomic, strong) UIColor *originalBarBgColor;
  17. @property (nonatomic, strong) UIColor *originalBarTintColor;
  18. @property (nonatomic) UIBarStyle originalBarStyle;
  19. @end
  20. @implementation RadarChartViewController
  21. - (void)viewDidLoad
  22. {
  23. [super viewDidLoad];
  24. self.activities = @[ @"Burger", @"Steak", @"Salad", @"Pasta", @"Pizza" ];
  25. self.title = @"Radar Chart";
  26. self.options = @[
  27. @{@"key": @"toggleValues", @"label": @"Toggle Values"},
  28. @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
  29. @{@"key": @"toggleHighlightCircle", @"label": @"Toggle highlight circle"},
  30. @{@"key": @"toggleXLabels", @"label": @"Toggle X-Values"},
  31. @{@"key": @"toggleYLabels", @"label": @"Toggle Y-Values"},
  32. @{@"key": @"toggleRotate", @"label": @"Toggle Rotate"},
  33. @{@"key": @"toggleFill", @"label": @"Toggle Fill"},
  34. @{@"key": @"animateX", @"label": @"Animate X"},
  35. @{@"key": @"animateY", @"label": @"Animate Y"},
  36. @{@"key": @"animateXY", @"label": @"Animate XY"},
  37. @{@"key": @"spin", @"label": @"Spin"},
  38. @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
  39. @{@"key": @"toggleData", @"label": @"Toggle Data"},
  40. ];
  41. _chartView.delegate = self;
  42. _chartView.chartDescription.enabled = NO;
  43. _chartView.webLineWidth = 1.0;
  44. _chartView.innerWebLineWidth = 1.0;
  45. _chartView.webColor = UIColor.lightGrayColor;
  46. _chartView.innerWebColor = UIColor.lightGrayColor;
  47. _chartView.webAlpha = 1.0;
  48. RadarMarkerView *marker = (RadarMarkerView *)[RadarMarkerView viewFromXibIn:[NSBundle mainBundle]];
  49. marker.chartView = _chartView;
  50. _chartView.marker = marker;
  51. ChartXAxis *xAxis = _chartView.xAxis;
  52. xAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:9.f];
  53. xAxis.xOffset = 0.0;
  54. xAxis.yOffset = 0.0;
  55. xAxis.valueFormatter = self;
  56. xAxis.labelTextColor = UIColor.whiteColor;
  57. ChartYAxis *yAxis = _chartView.yAxis;
  58. yAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:9.f];
  59. yAxis.labelCount = 5;
  60. yAxis.axisMinimum = 0.0;
  61. yAxis.axisMaximum = 80.0;
  62. yAxis.drawLabelsEnabled = NO;
  63. ChartLegend *l = _chartView.legend;
  64. l.horizontalAlignment = ChartLegendHorizontalAlignmentCenter;
  65. l.verticalAlignment = ChartLegendVerticalAlignmentTop;
  66. l.orientation = ChartLegendOrientationHorizontal;
  67. l.drawInside = NO;
  68. l.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
  69. l.xEntrySpace = 7.0;
  70. l.yEntrySpace = 5.0;
  71. l.textColor = UIColor.whiteColor;
  72. [self updateChartData];
  73. [_chartView animateWithXAxisDuration:1.4 yAxisDuration:1.4 easingOption:ChartEasingOptionEaseOutBack];
  74. }
  75. - (void)viewWillAppear:(BOOL)animated
  76. {
  77. [super viewWillAppear:animated];
  78. [UIView animateWithDuration:0.15 animations:^{
  79. UINavigationBar *navigationBar = self.navigationController.navigationBar;
  80. self.originalBarBgColor = navigationBar.barTintColor;
  81. self.originalBarTintColor = navigationBar.tintColor;
  82. self.originalBarStyle = navigationBar.barStyle;
  83. navigationBar.barTintColor = self.view.backgroundColor;
  84. navigationBar.tintColor = UIColor.whiteColor;
  85. navigationBar.barStyle = UIBarStyleBlack;
  86. }];
  87. }
  88. - (void)viewWillDisappear:(BOOL)animated
  89. {
  90. [super viewWillDisappear:animated];
  91. [UIView animateWithDuration:0.15 animations:^{
  92. UINavigationBar *navigationBar = self.navigationController.navigationBar;
  93. navigationBar.barTintColor = self.originalBarBgColor;
  94. navigationBar.tintColor = self.originalBarTintColor;
  95. navigationBar.barStyle = self.originalBarStyle;
  96. }];
  97. }
  98. - (void)didReceiveMemoryWarning
  99. {
  100. [super didReceiveMemoryWarning];
  101. // Dispose of any resources that can be recreated.
  102. }
  103. - (void)updateChartData
  104. {
  105. if (self.shouldHideData)
  106. {
  107. _chartView.data = nil;
  108. return;
  109. }
  110. [self setChartData];
  111. }
  112. - (void)setChartData
  113. {
  114. double mult = 80;
  115. double min = 20;
  116. int cnt = 5;
  117. NSMutableArray *entries1 = [[NSMutableArray alloc] init];
  118. NSMutableArray *entries2 = [[NSMutableArray alloc] init];
  119. // NOTE: The order of the entries when being added to the entries array determines their position around the center of the chart.
  120. for (int i = 0; i < cnt; i++)
  121. {
  122. [entries1 addObject:[[RadarChartDataEntry alloc] initWithValue:(arc4random_uniform(mult) + min)]];
  123. [entries2 addObject:[[RadarChartDataEntry alloc] initWithValue:(arc4random_uniform(mult) + min)]];
  124. }
  125. RadarChartDataSet *set1 = [[RadarChartDataSet alloc] initWithEntries:entries1 label:@"Last Week"];
  126. [set1 setColor:[UIColor colorWithRed:103/255.0 green:110/255.0 blue:129/255.0 alpha:1.0]];
  127. set1.fillColor = [UIColor colorWithRed:103/255.0 green:110/255.0 blue:129/255.0 alpha:1.0];
  128. set1.drawFilledEnabled = YES;
  129. set1.fillAlpha = 0.7;
  130. set1.lineWidth = 2.0;
  131. set1.drawHighlightCircleEnabled = YES;
  132. [set1 setDrawHighlightIndicators:NO];
  133. RadarChartDataSet *set2 = [[RadarChartDataSet alloc] initWithEntries:entries2 label:@"This Week"];
  134. [set2 setColor:[UIColor colorWithRed:121/255.0 green:162/255.0 blue:175/255.0 alpha:1.0]];
  135. set2.fillColor = [UIColor colorWithRed:121/255.0 green:162/255.0 blue:175/255.0 alpha:1.0];
  136. set2.drawFilledEnabled = YES;
  137. set2.fillAlpha = 0.7;
  138. set2.lineWidth = 2.0;
  139. set2.drawHighlightCircleEnabled = YES;
  140. [set2 setDrawHighlightIndicators:NO];
  141. RadarChartData *data = [[RadarChartData alloc] initWithDataSets:@[set1, set2]];
  142. [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:8.f]];
  143. [data setDrawValues:NO];
  144. data.valueTextColor = UIColor.whiteColor;
  145. _chartView.data = data;
  146. }
  147. - (void)optionTapped:(NSString *)key
  148. {
  149. if ([key isEqualToString:@"toggleXLabels"])
  150. {
  151. _chartView.xAxis.drawLabelsEnabled = !_chartView.xAxis.isDrawLabelsEnabled;
  152. [_chartView.data notifyDataChanged];
  153. [_chartView notifyDataSetChanged];
  154. [_chartView setNeedsDisplay];
  155. return;
  156. }
  157. if ([key isEqualToString:@"toggleYLabels"])
  158. {
  159. _chartView.yAxis.drawLabelsEnabled = !_chartView.yAxis.isDrawLabelsEnabled;
  160. [_chartView setNeedsDisplay];
  161. return;
  162. }
  163. if ([key isEqualToString:@"toggleRotate"])
  164. {
  165. _chartView.rotationEnabled = !_chartView.isRotationEnabled;
  166. return;
  167. }
  168. if ([key isEqualToString:@"toggleFill"])
  169. {
  170. for (RadarChartDataSet *set in _chartView.data.dataSets)
  171. {
  172. set.drawFilledEnabled = !set.isDrawFilledEnabled;
  173. }
  174. [_chartView setNeedsDisplay];
  175. return;
  176. }
  177. if ([key isEqualToString:@"toggleHighlightCircle"])
  178. {
  179. for (RadarChartDataSet *set in _chartView.data.dataSets)
  180. {
  181. set.drawHighlightCircleEnabled = !set.drawHighlightCircleEnabled;
  182. }
  183. [_chartView setNeedsDisplay];
  184. return;
  185. }
  186. if ([key isEqualToString:@"animateX"])
  187. {
  188. [_chartView animateWithXAxisDuration:1.4];
  189. return;
  190. }
  191. if ([key isEqualToString:@"animateY"])
  192. {
  193. [_chartView animateWithYAxisDuration:1.4];
  194. return;
  195. }
  196. if ([key isEqualToString:@"animateXY"])
  197. {
  198. [_chartView animateWithXAxisDuration:1.4 yAxisDuration:1.4];
  199. return;
  200. }
  201. if ([key isEqualToString:@"spin"])
  202. {
  203. [_chartView spinWithDuration:2.0 fromAngle:_chartView.rotationAngle toAngle:_chartView.rotationAngle + 360.f easingOption:ChartEasingOptionEaseInCubic];
  204. return;
  205. }
  206. [super handleOption:key forChartView:_chartView];
  207. }
  208. #pragma mark - ChartViewDelegate
  209. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
  210. {
  211. NSLog(@"chartValueSelected");
  212. }
  213. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  214. {
  215. NSLog(@"chartValueNothingSelected");
  216. }
  217. #pragma mark - IAxisValueFormatter
  218. - (NSString *)stringForValue:(double)value
  219. axis:(ChartAxisBase *)axis
  220. {
  221. return self.activities[(int) value % self.activities.count];
  222. }
  223. @end