HalfPieChartViewController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // HalfPieChartViewController.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 "HalfPieChartViewController.h"
  12. #import "ChartsDemo_iOS-Swift.h"
  13. @interface HalfPieChartViewController () <ChartViewDelegate>
  14. @property (nonatomic, strong) IBOutlet PieChartView *chartView;
  15. @end
  16. @implementation HalfPieChartViewController
  17. - (void)viewDidLoad
  18. {
  19. [super viewDidLoad];
  20. self.title = @"Half Pie Chart";
  21. self.options = @[
  22. @{@"key": @"toggleValues", @"label": @"Toggle Y-Values"},
  23. @{@"key": @"toggleXValues", @"label": @"Toggle X-Values"},
  24. @{@"key": @"togglePercent", @"label": @"Toggle Percent"},
  25. @{@"key": @"toggleHole", @"label": @"Toggle Hole"},
  26. @{@"key": @"animateX", @"label": @"Animate X"},
  27. @{@"key": @"animateY", @"label": @"Animate Y"},
  28. @{@"key": @"animateXY", @"label": @"Animate XY"},
  29. @{@"key": @"spin", @"label": @"Spin"},
  30. @{@"key": @"drawCenter", @"label": @"Draw CenterText"},
  31. @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
  32. @{@"key": @"toggleData", @"label": @"Toggle Data"},
  33. ];
  34. [self setupPieChartView:_chartView];
  35. _chartView.delegate = self;
  36. _chartView.holeColor = UIColor.whiteColor;
  37. _chartView.transparentCircleColor = [UIColor.whiteColor colorWithAlphaComponent:0.43];
  38. _chartView.holeRadiusPercent = 0.58;
  39. _chartView.rotationEnabled = NO;
  40. _chartView.highlightPerTapEnabled = YES;
  41. _chartView.maxAngle = 180.0; // Half chart
  42. _chartView.rotationAngle = 180.0; // Rotate to make the half on the upper side
  43. _chartView.centerTextOffset = CGPointMake(0.0, -20.0);
  44. ChartLegend *l = _chartView.legend;
  45. l.horizontalAlignment = ChartLegendHorizontalAlignmentCenter;
  46. l.verticalAlignment = ChartLegendVerticalAlignmentTop;
  47. l.orientation = ChartLegendOrientationHorizontal;
  48. l.drawInside = NO;
  49. l.xEntrySpace = 7.0;
  50. l.yEntrySpace = 0.0;
  51. l.yOffset = 0.0;
  52. // entry label styling
  53. _chartView.entryLabelColor = UIColor.whiteColor;
  54. _chartView.entryLabelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.f];
  55. [self updateChartData];
  56. [_chartView animateWithXAxisDuration:1.4 easingOption:ChartEasingOptionEaseOutBack];
  57. }
  58. - (void)didReceiveMemoryWarning
  59. {
  60. [super didReceiveMemoryWarning];
  61. // Dispose of any resources that can be recreated.
  62. }
  63. - (void)updateChartData
  64. {
  65. if (self.shouldHideData)
  66. {
  67. _chartView.data = nil;
  68. return;
  69. }
  70. [self setDataCount:4 range:100];
  71. }
  72. - (void)setDataCount:(int)count range:(double)range
  73. {
  74. double mult = range;
  75. NSMutableArray *values = [[NSMutableArray alloc] init];
  76. // IMPORTANT: In a PieChart, no values (Entry) should have the same xIndex (even if from different DataSets), since no values can be drawn above each other.
  77. for (int i = 0; i < count; i++)
  78. {
  79. [values addObject:[[PieChartDataEntry alloc] initWithValue:(arc4random_uniform(mult) + mult / 5) label:parties[i % parties.count]]];
  80. }
  81. PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithEntries:values label:@"Election Results"];
  82. dataSet.sliceSpace = 3.0;
  83. dataSet.selectionShift = 5.0;
  84. dataSet.colors = ChartColorTemplates.material;
  85. PieChartData *data = [[PieChartData alloc] initWithDataSet:dataSet];
  86. NSNumberFormatter *pFormatter = [[NSNumberFormatter alloc] init];
  87. pFormatter.numberStyle = NSNumberFormatterPercentStyle;
  88. pFormatter.maximumFractionDigits = 1;
  89. pFormatter.multiplier = @1.f;
  90. pFormatter.percentSymbol = @" %";
  91. [data setValueFormatter:[[ChartDefaultValueFormatter alloc] initWithFormatter:pFormatter]];
  92. [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:11.f]];
  93. [data setValueTextColor:UIColor.whiteColor];
  94. _chartView.data = data;
  95. [_chartView setNeedsDisplay];
  96. }
  97. - (void)optionTapped:(NSString *)key
  98. {
  99. if ([key isEqualToString:@"toggleXValues"])
  100. {
  101. _chartView.drawEntryLabelsEnabled = !_chartView.drawEntryLabelsEnabled;
  102. [_chartView setNeedsDisplay];
  103. return;
  104. }
  105. if ([key isEqualToString:@"togglePercent"])
  106. {
  107. _chartView.usePercentValuesEnabled = !_chartView.isUsePercentValuesEnabled;
  108. [_chartView setNeedsDisplay];
  109. return;
  110. }
  111. if ([key isEqualToString:@"toggleHole"])
  112. {
  113. _chartView.drawHoleEnabled = !_chartView.isDrawHoleEnabled;
  114. [_chartView setNeedsDisplay];
  115. return;
  116. }
  117. if ([key isEqualToString:@"drawCenter"])
  118. {
  119. _chartView.drawCenterTextEnabled = !_chartView.isDrawCenterTextEnabled;
  120. [_chartView setNeedsDisplay];
  121. return;
  122. }
  123. if ([key isEqualToString:@"animateX"])
  124. {
  125. [_chartView animateWithXAxisDuration:1.4];
  126. return;
  127. }
  128. if ([key isEqualToString:@"animateY"])
  129. {
  130. [_chartView animateWithYAxisDuration:1.4];
  131. return;
  132. }
  133. if ([key isEqualToString:@"animateXY"])
  134. {
  135. [_chartView animateWithXAxisDuration:1.4 yAxisDuration:1.4];
  136. return;
  137. }
  138. if ([key isEqualToString:@"spin"])
  139. {
  140. [_chartView spinWithDuration:2.0 fromAngle:_chartView.rotationAngle toAngle:_chartView.rotationAngle + 360.f easingOption:ChartEasingOptionEaseInCubic];
  141. return;
  142. }
  143. [super handleOption:key forChartView:_chartView];
  144. }
  145. #pragma mark - Action
  146. #pragma mark - ChartViewDelegate
  147. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
  148. {
  149. NSLog(@"chartValueSelected");
  150. }
  151. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  152. {
  153. NSLog(@"chartValueNothingSelected");
  154. }
  155. @end