PiePolylineChartViewController.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // PiePolylineChartViewController.h
  3. // ChartsDemo
  4. //
  5. // Created by Jack Wang on 3/21/16.
  6. // Copyright © 2016 Jack Wang
  7. //
  8. #import "PiePolylineChartViewController.h"
  9. #import "ChartsDemo_iOS-Swift.h"
  10. @interface PiePolylineChartViewController () <ChartViewDelegate>
  11. @property (nonatomic, strong) IBOutlet PieChartView *chartView;
  12. @property (nonatomic, strong) IBOutlet UISlider *sliderX;
  13. @property (nonatomic, strong) IBOutlet UISlider *sliderY;
  14. @property (nonatomic, strong) IBOutlet UITextField *sliderTextX;
  15. @property (nonatomic, strong) IBOutlet UITextField *sliderTextY;
  16. @end
  17. @implementation PiePolylineChartViewController
  18. - (void)viewDidLoad
  19. {
  20. [super viewDidLoad];
  21. self.title = @"Pie Poly Line Chart";
  22. self.options = @[
  23. @{@"key": @"toggleValues", @"label": @"Toggle Y-Values"},
  24. @{@"key": @"toggleXValues", @"label": @"Toggle X-Values"},
  25. @{@"key": @"togglePercent", @"label": @"Toggle Percent"},
  26. @{@"key": @"toggleHole", @"label": @"Toggle Hole"},
  27. @{@"key": @"toggleLabelsMinimumAngle", @"label": @"Toggle Labels Minimum Angle"},
  28. @{@"key": @"animateX", @"label": @"Animate X"},
  29. @{@"key": @"animateY", @"label": @"Animate Y"},
  30. @{@"key": @"animateXY", @"label": @"Animate XY"},
  31. @{@"key": @"spin", @"label": @"Spin"},
  32. @{@"key": @"drawCenter", @"label": @"Draw CenterText"},
  33. @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
  34. @{@"key": @"toggleData", @"label": @"Toggle Data"},
  35. ];
  36. [self setupPieChartView:_chartView];
  37. _chartView.legend.enabled = NO;
  38. _chartView.delegate = self;
  39. [_chartView setExtraOffsetsWithLeft:20.f top:0.f right:20.f bottom:0.f];
  40. _sliderX.value = 4.0;
  41. _sliderY.value = 100.0;
  42. [self slidersValueChanged:nil];
  43. [_chartView animateWithYAxisDuration:1.4 easingOption:ChartEasingOptionEaseOutBack];
  44. }
  45. - (void)didReceiveMemoryWarning
  46. {
  47. [super didReceiveMemoryWarning];
  48. // Dispose of any resources that can be recreated.
  49. }
  50. - (void)updateChartData
  51. {
  52. if (self.shouldHideData)
  53. {
  54. _chartView.data = nil;
  55. return;
  56. }
  57. [self setDataCount:_sliderX.value range:_sliderY.value];
  58. }
  59. - (void)setDataCount:(int)count range:(double)range
  60. {
  61. double mult = range;
  62. NSMutableArray *entries = [[NSMutableArray alloc] init];
  63. for (int i = 0; i < count; i++)
  64. {
  65. [entries addObject:[[PieChartDataEntry alloc] initWithValue:(arc4random_uniform(mult) + mult / 5) label:parties[i % parties.count]]];
  66. }
  67. PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithEntries:entries label:@"Election Results"];
  68. dataSet.sliceSpace = 2.0;
  69. // add a lot of colors
  70. NSMutableArray *colors = [[NSMutableArray alloc] init];
  71. [colors addObjectsFromArray:ChartColorTemplates.vordiplom];
  72. [colors addObjectsFromArray:ChartColorTemplates.joyful];
  73. [colors addObjectsFromArray:ChartColorTemplates.colorful];
  74. [colors addObjectsFromArray:ChartColorTemplates.liberty];
  75. [colors addObjectsFromArray:ChartColorTemplates.pastel];
  76. [colors addObject:[UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]];
  77. dataSet.colors = colors;
  78. dataSet.valueLinePart1OffsetPercentage = 0.8;
  79. dataSet.valueLinePart1Length = 0.2;
  80. dataSet.valueLinePart2Length = 0.4;
  81. //dataSet.xValuePosition = PieChartValuePositionOutsideSlice;
  82. dataSet.yValuePosition = PieChartValuePositionOutsideSlice;
  83. PieChartData *data = [[PieChartData alloc] initWithDataSet:dataSet];
  84. NSNumberFormatter *pFormatter = [[NSNumberFormatter alloc] init];
  85. pFormatter.numberStyle = NSNumberFormatterPercentStyle;
  86. pFormatter.maximumFractionDigits = 1;
  87. pFormatter.multiplier = @1.f;
  88. pFormatter.percentSymbol = @" %";
  89. [data setValueFormatter:[[ChartDefaultValueFormatter alloc] initWithFormatter:pFormatter]];
  90. [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:11.f]];
  91. [data setValueTextColor:UIColor.blackColor];
  92. _chartView.data = data;
  93. [_chartView highlightValues:nil];
  94. }
  95. - (void)optionTapped:(NSString *)key
  96. {
  97. if ([key isEqualToString:@"toggleXValues"])
  98. {
  99. _chartView.drawEntryLabelsEnabled = !_chartView.isDrawEntryLabelsEnabled;
  100. [_chartView setNeedsDisplay];
  101. return;
  102. }
  103. if ([key isEqualToString:@"togglePercent"])
  104. {
  105. _chartView.usePercentValuesEnabled = !_chartView.isUsePercentValuesEnabled;
  106. [_chartView setNeedsDisplay];
  107. return;
  108. }
  109. if ([key isEqualToString:@"toggleHole"])
  110. {
  111. _chartView.drawHoleEnabled = !_chartView.isDrawHoleEnabled;
  112. [_chartView setNeedsDisplay];
  113. return;
  114. }
  115. if ([key isEqualToString:@"toggleLabelsMinimumAngle"])
  116. {
  117. CGFloat newMinimum = _chartView.sliceTextDrawingThreshold == 20.0 ? 0.0 : 20.0;
  118. _chartView.sliceTextDrawingThreshold = newMinimum;
  119. }
  120. if ([key isEqualToString:@"drawCenter"])
  121. {
  122. _chartView.drawCenterTextEnabled = !_chartView.isDrawCenterTextEnabled;
  123. [_chartView setNeedsDisplay];
  124. return;
  125. }
  126. if ([key isEqualToString:@"animateX"])
  127. {
  128. [_chartView animateWithXAxisDuration:1.4];
  129. return;
  130. }
  131. if ([key isEqualToString:@"animateY"])
  132. {
  133. [_chartView animateWithYAxisDuration:1.4];
  134. return;
  135. }
  136. if ([key isEqualToString:@"animateXY"])
  137. {
  138. [_chartView animateWithXAxisDuration:1.4 yAxisDuration:1.4];
  139. return;
  140. }
  141. if ([key isEqualToString:@"spin"])
  142. {
  143. [_chartView spinWithDuration:2.0 fromAngle:_chartView.rotationAngle toAngle:_chartView.rotationAngle + 360.f];
  144. return;
  145. }
  146. [super handleOption:key forChartView:_chartView];
  147. }
  148. #pragma mark - Actions
  149. - (IBAction)slidersValueChanged:(id)sender
  150. {
  151. _sliderTextX.text = [@((int)_sliderX.value) stringValue];
  152. _sliderTextY.text = [@((int)_sliderY.value) stringValue];
  153. [self updateChartData];
  154. }
  155. #pragma mark - ChartViewDelegate
  156. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
  157. {
  158. NSLog(@"chartValueSelected");
  159. }
  160. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  161. {
  162. NSLog(@"chartValueNothingSelected");
  163. }
  164. @end