MultipleLinesChartViewController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // MultipleLinesChartViewController.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 "MultipleLinesChartViewController.h"
  12. #import "ChartsDemo_iOS-Swift.h"
  13. @interface MultipleLinesChartViewController () <ChartViewDelegate>
  14. @property (nonatomic, strong) IBOutlet LineChartView *chartView;
  15. @property (nonatomic, strong) IBOutlet UISlider *sliderX;
  16. @property (nonatomic, strong) IBOutlet UISlider *sliderY;
  17. @property (nonatomic, strong) IBOutlet UITextField *sliderTextX;
  18. @property (nonatomic, strong) IBOutlet UITextField *sliderTextY;
  19. @end
  20. @implementation MultipleLinesChartViewController
  21. - (void)viewDidLoad
  22. {
  23. [super viewDidLoad];
  24. self.title = @"Multiple Lines Chart";
  25. self.options = @[
  26. @{@"key": @"toggleValues", @"label": @"Toggle Values"},
  27. @{@"key": @"toggleFilled", @"label": @"Toggle Filled"},
  28. @{@"key": @"toggleCircles", @"label": @"Toggle Circles"},
  29. @{@"key": @"toggleCubic", @"label": @"Toggle Cubic"},
  30. @{@"key": @"toggleStepped", @"label": @"Toggle Stepped"},
  31. @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
  32. @{@"key": @"animateX", @"label": @"Animate X"},
  33. @{@"key": @"animateY", @"label": @"Animate Y"},
  34. @{@"key": @"animateXY", @"label": @"Animate XY"},
  35. @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
  36. @{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
  37. @{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
  38. @{@"key": @"toggleData", @"label": @"Toggle Data"},
  39. ];
  40. _chartView.delegate = self;
  41. _chartView.chartDescription.enabled = NO;
  42. _chartView.leftAxis.enabled = NO;
  43. _chartView.rightAxis.drawAxisLineEnabled = NO;
  44. _chartView.rightAxis.drawGridLinesEnabled = NO;
  45. _chartView.xAxis.drawAxisLineEnabled = NO;
  46. _chartView.xAxis.drawGridLinesEnabled = NO;
  47. _chartView.drawGridBackgroundEnabled = NO;
  48. _chartView.drawBordersEnabled = NO;
  49. _chartView.dragEnabled = YES;
  50. [_chartView setScaleEnabled:YES];
  51. _chartView.pinchZoomEnabled = NO;
  52. ChartLegend *l = _chartView.legend;
  53. l.horizontalAlignment = ChartLegendHorizontalAlignmentRight;
  54. l.verticalAlignment = ChartLegendVerticalAlignmentTop;
  55. l.orientation = ChartLegendOrientationVertical;
  56. l.drawInside = NO;
  57. _sliderX.value = 20.0;
  58. _sliderY.value = 100.0;
  59. [self slidersValueChanged:nil];
  60. }
  61. - (void)didReceiveMemoryWarning
  62. {
  63. [super didReceiveMemoryWarning];
  64. // Dispose of any resources that can be recreated.
  65. }
  66. - (void)updateChartData
  67. {
  68. if (self.shouldHideData)
  69. {
  70. _chartView.data = nil;
  71. return;
  72. }
  73. [self setDataCount:_sliderX.value range:_sliderY.value];
  74. }
  75. - (void)setDataCount:(int)count range:(double)range
  76. {
  77. NSArray *colors = @[ChartColorTemplates.vordiplom[0], ChartColorTemplates.vordiplom[1], ChartColorTemplates.vordiplom[2]];
  78. NSMutableArray *dataSets = [[NSMutableArray alloc] init];
  79. for (int z = 0; z < 3; z++)
  80. {
  81. NSMutableArray *values = [[NSMutableArray alloc] init];
  82. for (int i = 0; i < count; i++)
  83. {
  84. double val = (double) (arc4random_uniform(range) + 3);
  85. [values addObject:[[ChartDataEntry alloc] initWithX:i y:val]];
  86. }
  87. LineChartDataSet *d = [[LineChartDataSet alloc] initWithEntries:values label:[NSString stringWithFormat:@"DataSet %d", z + 1]];
  88. d.lineWidth = 2.5;
  89. d.circleRadius = 4.0;
  90. d.circleHoleRadius = 2.0;
  91. UIColor *color = colors[z % colors.count];
  92. [d setColor:color];
  93. [d setCircleColor:color];
  94. [dataSets addObject:d];
  95. }
  96. ((LineChartDataSet *)dataSets[0]).lineDashLengths = @[@5.f, @5.f];
  97. ((LineChartDataSet *)dataSets[0]).colors = ChartColorTemplates.vordiplom;
  98. ((LineChartDataSet *)dataSets[0]).circleColors = ChartColorTemplates.vordiplom;
  99. LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
  100. [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:7.f]];
  101. _chartView.data = data;
  102. }
  103. - (void)optionTapped:(NSString *)key
  104. {
  105. if ([key isEqualToString:@"toggleFilled"])
  106. {
  107. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  108. {
  109. set.drawFilledEnabled = !set.isDrawFilledEnabled;
  110. }
  111. [_chartView setNeedsDisplay];
  112. return;
  113. }
  114. if ([key isEqualToString:@"toggleCircles"])
  115. {
  116. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  117. {
  118. set.drawCirclesEnabled = !set.isDrawCirclesEnabled;
  119. }
  120. [_chartView setNeedsDisplay];
  121. return;
  122. }
  123. if ([key isEqualToString:@"toggleCubic"])
  124. {
  125. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  126. {
  127. set.mode = set.mode == LineChartModeCubicBezier ? LineChartModeLinear : LineChartModeCubicBezier;
  128. }
  129. [_chartView setNeedsDisplay];
  130. return;
  131. }
  132. if ([key isEqualToString:@"toggleStepped"])
  133. {
  134. for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
  135. {
  136. switch (set.mode) {
  137. case LineChartModeLinear:
  138. case LineChartModeCubicBezier:
  139. case LineChartModeHorizontalBezier:
  140. set.mode = LineChartModeStepped;
  141. break;
  142. case LineChartModeStepped: set.mode = LineChartModeLinear;
  143. }
  144. }
  145. [_chartView setNeedsDisplay];
  146. }
  147. [super handleOption:key forChartView:_chartView];
  148. }
  149. #pragma mark - Actions
  150. - (IBAction)slidersValueChanged:(id)sender
  151. {
  152. _sliderTextX.text = [@((int)_sliderX.value) stringValue];
  153. _sliderTextY.text = [@((int)_sliderY.value) stringValue];
  154. [self updateChartData];
  155. }
  156. #pragma mark - ChartViewDelegate
  157. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
  158. {
  159. NSLog(@"chartValueSelected");
  160. }
  161. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  162. {
  163. NSLog(@"chartValueNothingSelected");
  164. }
  165. @end