DemoBaseViewController.m 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. //
  2. // DemoBaseViewController.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 "DemoBaseViewController.h"
  12. @interface DemoBaseViewController () <UITableViewDelegate, UITableViewDataSource>
  13. @property (nonatomic, strong) UITableView *optionsTableView;
  14. @end
  15. @implementation DemoBaseViewController
  16. - (id)initWithCoder:(NSCoder *)aDecoder
  17. {
  18. self = [super initWithCoder:aDecoder];
  19. if (self)
  20. {
  21. [self initialize];
  22. }
  23. return self;
  24. }
  25. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  26. {
  27. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  28. if (self)
  29. {
  30. [self initialize];
  31. }
  32. return self;
  33. }
  34. - (void)initialize
  35. {
  36. self.edgesForExtendedLayout = UIRectEdgeNone;
  37. parties = @[
  38. @"Party A", @"Party B", @"Party C", @"Party D", @"Party E", @"Party F",
  39. @"Party G", @"Party H", @"Party I", @"Party J", @"Party K", @"Party L",
  40. @"Party M", @"Party N", @"Party O", @"Party P", @"Party Q", @"Party R",
  41. @"Party S", @"Party T", @"Party U", @"Party V", @"Party W", @"Party X",
  42. @"Party Y", @"Party Z"
  43. ];
  44. }
  45. - (void)viewDidLoad
  46. {
  47. [super viewDidLoad];
  48. // Do any additional setup after loading the view from its nib.
  49. }
  50. - (void)didReceiveMemoryWarning
  51. {
  52. [super didReceiveMemoryWarning];
  53. // Dispose of any resources that can be recreated.
  54. }
  55. - (void)optionTapped:(NSString *)key
  56. {
  57. }
  58. #pragma mark - Common option actions
  59. - (void)handleOption:(NSString *)key forChartView:(ChartViewBase *)chartView
  60. {
  61. if ([key isEqualToString:@"toggleValues"])
  62. {
  63. for (id<ChartDataSetProtocol> set in chartView.data.dataSets)
  64. {
  65. set.drawValuesEnabled = !set.isDrawValuesEnabled;
  66. }
  67. [chartView setNeedsDisplay];
  68. }
  69. if ([key isEqualToString:@"toggleIcons"])
  70. {
  71. for (id<ChartDataSetProtocol> set in chartView.data.dataSets)
  72. {
  73. set.drawIconsEnabled = !set.isDrawIconsEnabled;
  74. }
  75. [chartView setNeedsDisplay];
  76. }
  77. if ([key isEqualToString:@"toggleHighlight"])
  78. {
  79. chartView.data.isHighlightEnabled = !chartView.data.isHighlightEnabled;
  80. [chartView setNeedsDisplay];
  81. }
  82. if ([key isEqualToString:@"animateX"])
  83. {
  84. [chartView animateWithXAxisDuration:3.0];
  85. }
  86. if ([key isEqualToString:@"animateY"])
  87. {
  88. [chartView animateWithYAxisDuration:3.0];
  89. }
  90. if ([key isEqualToString:@"animateXY"])
  91. {
  92. [chartView animateWithXAxisDuration:3.0 yAxisDuration:3.0];
  93. }
  94. if ([key isEqualToString:@"saveToGallery"])
  95. {
  96. UIImageWriteToSavedPhotosAlbum([chartView getChartImageWithTransparent:NO], nil, nil, nil);
  97. }
  98. if ([key isEqualToString:@"togglePinchZoom"])
  99. {
  100. BarLineChartViewBase *barLineChart = (BarLineChartViewBase *)chartView;
  101. barLineChart.pinchZoomEnabled = !barLineChart.isPinchZoomEnabled;
  102. [chartView setNeedsDisplay];
  103. }
  104. if ([key isEqualToString:@"toggleAutoScaleMinMax"])
  105. {
  106. BarLineChartViewBase *barLineChart = (BarLineChartViewBase *)chartView;
  107. barLineChart.autoScaleMinMaxEnabled = !barLineChart.isAutoScaleMinMaxEnabled;
  108. [chartView notifyDataSetChanged];
  109. }
  110. if ([key isEqualToString:@"toggleData"])
  111. {
  112. _shouldHideData = !_shouldHideData;
  113. [self updateChartData];
  114. }
  115. if ([key isEqualToString:@"toggleBarBorders"])
  116. {
  117. for (id<BarChartDataSetProtocol, NSObject> set in chartView.data.dataSets)
  118. {
  119. if ([set conformsToProtocol:@protocol(BarChartDataSetProtocol)])
  120. {
  121. set.barBorderWidth = set.barBorderWidth == 1.0 ? 0.0 : 1.0;
  122. }
  123. }
  124. [chartView setNeedsDisplay];
  125. }
  126. }
  127. #pragma mark - Actions
  128. - (IBAction)optionsButtonTapped:(id)sender
  129. {
  130. if (_optionsTableView)
  131. {
  132. [_optionsTableView removeFromSuperview];
  133. self.optionsTableView = nil;
  134. return;
  135. }
  136. self.optionsTableView = [[UITableView alloc] init];
  137. _optionsTableView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.9f];
  138. _optionsTableView.delegate = self;
  139. _optionsTableView.dataSource = self;
  140. [_optionsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
  141. _optionsTableView.translatesAutoresizingMaskIntoConstraints = NO;
  142. NSMutableArray *constraints = [[NSMutableArray alloc] init];
  143. [constraints addObject:[NSLayoutConstraint constraintWithItem:_optionsTableView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.f constant:40.f]];
  144. [constraints addObject:[NSLayoutConstraint constraintWithItem:_optionsTableView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:sender attribute:NSLayoutAttributeTrailing multiplier:1.f constant:0]];
  145. [constraints addObject:[NSLayoutConstraint constraintWithItem:_optionsTableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:sender attribute:NSLayoutAttributeBottom multiplier:1.f constant:5.f]];
  146. [self.view addSubview:_optionsTableView];
  147. [self.view addConstraints:constraints];
  148. [_optionsTableView addConstraints:@[
  149. [NSLayoutConstraint constraintWithItem:_optionsTableView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.f constant:220.f]
  150. ]];
  151. }
  152. #pragma mark - UITableViewDelegate, UITableViewDataSource
  153. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  154. {
  155. if (tableView == _optionsTableView)
  156. {
  157. return 1;
  158. }
  159. return 0;
  160. }
  161. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  162. {
  163. if (tableView == _optionsTableView)
  164. {
  165. return self.options.count;
  166. }
  167. return 0;
  168. }
  169. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  170. {
  171. if (tableView == _optionsTableView)
  172. {
  173. return 40.0;
  174. }
  175. return 44.0;
  176. }
  177. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  178. {
  179. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  180. cell.backgroundView = nil;
  181. cell.backgroundColor = UIColor.clearColor;
  182. cell.textLabel.textColor = UIColor.whiteColor;
  183. cell.textLabel.text = self.options[indexPath.row][@"label"];
  184. return cell;
  185. }
  186. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  187. {
  188. if (tableView == _optionsTableView)
  189. {
  190. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  191. if (_optionsTableView)
  192. {
  193. [_optionsTableView removeFromSuperview];
  194. self.optionsTableView = nil;
  195. }
  196. [self optionTapped:self.options[indexPath.row][@"key"]];
  197. }
  198. }
  199. #pragma mark - Stubs for chart view
  200. - (void)updateChartData
  201. {
  202. // Override this
  203. }
  204. - (void)setupPieChartView:(PieChartView *)chartView
  205. {
  206. chartView.usePercentValuesEnabled = YES;
  207. chartView.drawSlicesUnderHoleEnabled = NO;
  208. chartView.holeRadiusPercent = 0.58;
  209. chartView.transparentCircleRadiusPercent = 0.61;
  210. chartView.chartDescription.enabled = NO;
  211. [chartView setExtraOffsetsWithLeft:5.f top:10.f right:5.f bottom:5.f];
  212. chartView.drawCenterTextEnabled = YES;
  213. NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
  214. paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
  215. paragraphStyle.alignment = NSTextAlignmentCenter;
  216. NSMutableAttributedString *centerText = [[NSMutableAttributedString alloc] initWithString:@"Charts\nby Daniel Cohen Gindi"];
  217. [centerText setAttributes:@{
  218. NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:13.f],
  219. NSParagraphStyleAttributeName: paragraphStyle
  220. } range:NSMakeRange(0, centerText.length)];
  221. [centerText addAttributes:@{
  222. NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f],
  223. NSForegroundColorAttributeName: UIColor.grayColor
  224. } range:NSMakeRange(10, centerText.length - 10)];
  225. [centerText addAttributes:@{
  226. NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-LightItalic" size:11.f],
  227. NSForegroundColorAttributeName: [UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]
  228. } range:NSMakeRange(centerText.length - 19, 19)];
  229. chartView.centerAttributedText = centerText;
  230. chartView.drawHoleEnabled = YES;
  231. chartView.rotationAngle = 0.0;
  232. chartView.rotationEnabled = YES;
  233. chartView.highlightPerTapEnabled = YES;
  234. ChartLegend *l = chartView.legend;
  235. l.horizontalAlignment = ChartLegendHorizontalAlignmentRight;
  236. l.verticalAlignment = ChartLegendVerticalAlignmentTop;
  237. l.orientation = ChartLegendOrientationVertical;
  238. l.drawInside = NO;
  239. l.xEntrySpace = 7.0;
  240. l.yEntrySpace = 0.0;
  241. l.yOffset = 0.0;
  242. }
  243. - (void)setupRadarChartView:(RadarChartView *)chartView
  244. {
  245. chartView.chartDescription.enabled = NO;
  246. }
  247. - (void)setupBarLineChartView:(BarLineChartViewBase *)chartView
  248. {
  249. chartView.chartDescription.enabled = NO;
  250. chartView.drawGridBackgroundEnabled = NO;
  251. chartView.dragEnabled = YES;
  252. [chartView setScaleEnabled:YES];
  253. chartView.pinchZoomEnabled = NO;
  254. // ChartYAxis *leftAxis = chartView.leftAxis;
  255. ChartXAxis *xAxis = chartView.xAxis;
  256. xAxis.labelPosition = XAxisLabelPositionBottom;
  257. chartView.rightAxis.enabled = NO;
  258. }
  259. @end