BarChartViewController.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // BarChartViewController.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 "BarChartViewController.h"
  12. #import "ChartsDemo_iOS-Swift.h"
  13. #import "DayAxisValueFormatter.h"
  14. @interface BarChartViewController () <ChartViewDelegate>
  15. @property (nonatomic, strong) IBOutlet BarChartView *chartView;
  16. @property (nonatomic, strong) IBOutlet UISlider *sliderX;
  17. @property (nonatomic, strong) IBOutlet UISlider *sliderY;
  18. @property (nonatomic, strong) IBOutlet UITextField *sliderTextX;
  19. @property (nonatomic, strong) IBOutlet UITextField *sliderTextY;
  20. @end
  21. @implementation BarChartViewController
  22. - (void)viewDidLoad
  23. {
  24. [super viewDidLoad];
  25. self.title = @"Bar Chart";
  26. self.options = @[
  27. @{@"key": @"toggleValues", @"label": @"Toggle Values"},
  28. @{@"key": @"toggleIcons", @"label": @"Toggle Icons"},
  29. @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
  30. @{@"key": @"animateX", @"label": @"Animate X"},
  31. @{@"key": @"animateY", @"label": @"Animate Y"},
  32. @{@"key": @"animateXY", @"label": @"Animate XY"},
  33. @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
  34. @{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
  35. @{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
  36. @{@"key": @"toggleData", @"label": @"Toggle Data"},
  37. @{@"key": @"toggleBarBorders", @"label": @"Show Bar Borders"},
  38. ];
  39. [self setupBarLineChartView:_chartView];
  40. _chartView.delegate = self;
  41. _chartView.drawBarShadowEnabled = NO;
  42. _chartView.drawValueAboveBarEnabled = YES;
  43. _chartView.maxVisibleCount = 60;
  44. ChartXAxis *xAxis = _chartView.xAxis;
  45. xAxis.labelPosition = XAxisLabelPositionBottom;
  46. xAxis.labelFont = [UIFont systemFontOfSize:10.f];
  47. xAxis.drawGridLinesEnabled = NO;
  48. xAxis.granularity = 1.0; // only intervals of 1 day
  49. xAxis.labelCount = 7;
  50. xAxis.valueFormatter = [[DayAxisValueFormatter alloc] initForChart:_chartView];
  51. NSNumberFormatter *leftAxisFormatter = [[NSNumberFormatter alloc] init];
  52. leftAxisFormatter.minimumFractionDigits = 0;
  53. leftAxisFormatter.maximumFractionDigits = 1;
  54. leftAxisFormatter.negativeSuffix = @" $";
  55. leftAxisFormatter.positiveSuffix = @" $";
  56. ChartYAxis *leftAxis = _chartView.leftAxis;
  57. leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
  58. leftAxis.labelCount = 8;
  59. leftAxis.valueFormatter = [[ChartDefaultAxisValueFormatter alloc] initWithFormatter:leftAxisFormatter];
  60. leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
  61. leftAxis.spaceTop = 0.15;
  62. leftAxis.axisMinimum = 0.0; // this replaces startAtZero = YES
  63. ChartYAxis *rightAxis = _chartView.rightAxis;
  64. rightAxis.enabled = YES;
  65. rightAxis.drawGridLinesEnabled = NO;
  66. rightAxis.labelFont = [UIFont systemFontOfSize:10.f];
  67. rightAxis.labelCount = 8;
  68. rightAxis.valueFormatter = leftAxis.valueFormatter;
  69. rightAxis.spaceTop = 0.15;
  70. rightAxis.axisMinimum = 0.0; // this replaces startAtZero = YES
  71. ChartLegend *l = _chartView.legend;
  72. l.horizontalAlignment = ChartLegendHorizontalAlignmentLeft;
  73. l.verticalAlignment = ChartLegendVerticalAlignmentBottom;
  74. l.orientation = ChartLegendOrientationHorizontal;
  75. l.drawInside = NO;
  76. l.form = ChartLegendFormSquare;
  77. l.formSize = 9.0;
  78. l.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
  79. l.xEntrySpace = 4.0;
  80. XYMarkerView *marker = [[XYMarkerView alloc]
  81. initWithColor: [UIColor colorWithWhite:180/255. alpha:1.0]
  82. font: [UIFont systemFontOfSize:12.0]
  83. textColor: UIColor.whiteColor
  84. insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0)
  85. xAxisValueFormatter: _chartView.xAxis.valueFormatter];
  86. marker.chartView = _chartView;
  87. marker.minimumSize = CGSizeMake(80.f, 40.f);
  88. _chartView.marker = marker;
  89. _sliderX.value = 12.0;
  90. _sliderY.value = 50.0;
  91. [self slidersValueChanged:nil];
  92. }
  93. - (void)didReceiveMemoryWarning
  94. {
  95. [super didReceiveMemoryWarning];
  96. // Dispose of any resources that can be recreated.
  97. }
  98. - (void)updateChartData
  99. {
  100. if (self.shouldHideData)
  101. {
  102. _chartView.data = nil;
  103. return;
  104. }
  105. [self setDataCount:_sliderX.value + 1 range:_sliderY.value];
  106. }
  107. - (void)setDataCount:(int)count range:(double)range
  108. {
  109. double start = 1.0;
  110. NSMutableArray *yVals = [[NSMutableArray alloc] init];
  111. for (int i = start; i < start + count + 1; i++)
  112. {
  113. double mult = (range + 1);
  114. double val = (double) (arc4random_uniform(mult));
  115. if (arc4random_uniform(100) < 25) {
  116. [yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:val icon: [UIImage imageNamed:@"icon"]]];
  117. } else {
  118. [yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:val]];
  119. }
  120. }
  121. BarChartDataSet *set1 = nil;
  122. if (_chartView.data.dataSetCount > 0)
  123. {
  124. set1 = (BarChartDataSet *)_chartView.data.dataSets[0];
  125. [set1 replaceEntries: yVals];
  126. [_chartView.data notifyDataChanged];
  127. [_chartView notifyDataSetChanged];
  128. }
  129. else
  130. {
  131. set1 = [[BarChartDataSet alloc] initWithEntries:yVals label:@"The year 2017"];
  132. [set1 setColors:ChartColorTemplates.material];
  133. set1.drawIconsEnabled = NO;
  134. NSMutableArray *dataSets = [[NSMutableArray alloc] init];
  135. [dataSets addObject:set1];
  136. BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
  137. [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
  138. data.barWidth = 0.9f;
  139. _chartView.data = data;
  140. }
  141. }
  142. - (void)optionTapped:(NSString *)key
  143. {
  144. [super handleOption:key forChartView:_chartView];
  145. }
  146. #pragma mark - Actions
  147. - (IBAction)slidersValueChanged:(id)sender
  148. {
  149. _sliderTextX.text = [@((int)_sliderX.value + 2) stringValue];
  150. _sliderTextY.text = [@((int)_sliderY.value) stringValue];
  151. [self updateChartData];
  152. }
  153. #pragma mark - ChartViewDelegate
  154. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
  155. {
  156. NSLog(@"chartValueSelected");
  157. }
  158. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  159. {
  160. NSLog(@"chartValueNothingSelected");
  161. }
  162. @end