RadarChartViewController.m 7.7 KB

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