HorizontalBarChartViewController.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // HorizontalBarChartViewController.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 "HorizontalBarChartViewController.h"
  12. #import "ChartsDemo_iOS-Swift.h"
  13. @interface HorizontalBarChartViewController () <ChartViewDelegate>
  14. @property (nonatomic, strong) IBOutlet HorizontalBarChartView *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 HorizontalBarChartViewController
  21. - (void)viewDidLoad
  22. {
  23. [super viewDidLoad];
  24. self.title = @"Horizontal Bar Chart";
  25. self.options = @[
  26. @{@"key": @"toggleValues", @"label": @"Toggle Values"},
  27. @{@"key": @"toggleIcons", @"label": @"Toggle Icons"},
  28. @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
  29. @{@"key": @"animateX", @"label": @"Animate X"},
  30. @{@"key": @"animateY", @"label": @"Animate Y"},
  31. @{@"key": @"animateXY", @"label": @"Animate XY"},
  32. @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
  33. @{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
  34. @{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
  35. @{@"key": @"toggleData", @"label": @"Toggle Data"},
  36. @{@"key": @"toggleBarBorders", @"label": @"Show Bar Borders"},
  37. ];
  38. [self setupBarLineChartView:_chartView];
  39. _chartView.delegate = self;
  40. _chartView.drawBarShadowEnabled = NO;
  41. _chartView.drawValueAboveBarEnabled = YES;
  42. _chartView.maxVisibleCount = 60;
  43. ChartXAxis *xAxis = _chartView.xAxis;
  44. xAxis.labelPosition = XAxisLabelPositionBottom;
  45. xAxis.labelFont = [UIFont systemFontOfSize:10.f];
  46. xAxis.drawAxisLineEnabled = YES;
  47. xAxis.drawGridLinesEnabled = NO;
  48. xAxis.granularity = 10.0;
  49. ChartYAxis *leftAxis = _chartView.leftAxis;
  50. leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
  51. leftAxis.drawAxisLineEnabled = YES;
  52. leftAxis.drawGridLinesEnabled = YES;
  53. leftAxis.axisMinimum = 0.0; // this replaces startAtZero = YES
  54. ChartYAxis *rightAxis = _chartView.rightAxis;
  55. rightAxis.enabled = YES;
  56. rightAxis.labelFont = [UIFont systemFontOfSize:10.f];
  57. rightAxis.drawAxisLineEnabled = YES;
  58. rightAxis.drawGridLinesEnabled = NO;
  59. rightAxis.axisMinimum = 0.0; // this replaces startAtZero = YES
  60. ChartLegend *l = _chartView.legend;
  61. l.horizontalAlignment = ChartLegendHorizontalAlignmentLeft;
  62. l.verticalAlignment = ChartLegendVerticalAlignmentBottom;
  63. l.orientation = ChartLegendOrientationHorizontal;
  64. l.drawInside = NO;
  65. l.form = ChartLegendFormSquare;
  66. l.formSize = 8.0;
  67. l.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
  68. l.xEntrySpace = 4.0;
  69. _chartView.fitBars = YES;
  70. _sliderX.value = 12.0;
  71. _sliderY.value = 50.0;
  72. [self slidersValueChanged:nil];
  73. [_chartView animateWithYAxisDuration:2.5];
  74. }
  75. - (void)didReceiveMemoryWarning
  76. {
  77. [super didReceiveMemoryWarning];
  78. // Dispose of any resources that can be recreated.
  79. }
  80. - (void)updateChartData
  81. {
  82. if (self.shouldHideData)
  83. {
  84. _chartView.data = nil;
  85. return;
  86. }
  87. [self setDataCount:_sliderX.value + 1 range:_sliderY.value];
  88. }
  89. - (void)setDataCount:(int)count range:(double)range
  90. {
  91. double barWidth = 9.0;
  92. double spaceForBar = 10.0;
  93. NSMutableArray *yVals = [[NSMutableArray alloc] init];
  94. for (int i = 0; i < count; i++)
  95. {
  96. double mult = (range + 1);
  97. double val = (double) (arc4random_uniform(mult));
  98. [yVals addObject:[[BarChartDataEntry alloc] initWithX:i * spaceForBar y:val icon: [UIImage imageNamed:@"icon"]]];
  99. }
  100. BarChartDataSet *set1 = nil;
  101. if (_chartView.data.dataSetCount > 0)
  102. {
  103. set1 = (BarChartDataSet *)_chartView.data.dataSets[0];
  104. [set1 replaceEntries:yVals];
  105. [_chartView.data notifyDataChanged];
  106. [_chartView notifyDataSetChanged];
  107. }
  108. else
  109. {
  110. set1 = [[BarChartDataSet alloc] initWithEntries:yVals label:@"DataSet"];
  111. set1.drawIconsEnabled = NO;
  112. NSMutableArray *dataSets = [[NSMutableArray alloc] init];
  113. [dataSets addObject:set1];
  114. BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
  115. [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
  116. data.barWidth = barWidth;
  117. _chartView.data = data;
  118. }
  119. }
  120. - (void)optionTapped:(NSString *)key
  121. {
  122. [super handleOption:key forChartView:_chartView];
  123. }
  124. #pragma mark - Actions
  125. - (IBAction)slidersValueChanged:(id)sender
  126. {
  127. _sliderTextX.text = [@((int)_sliderX.value) stringValue];
  128. _sliderTextY.text = [@((int)_sliderY.value) stringValue];
  129. [self updateChartData];
  130. }
  131. #pragma mark - ChartViewDelegate
  132. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
  133. {
  134. NSLog(@"chartValueSelected");
  135. }
  136. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  137. {
  138. NSLog(@"chartValueNothingSelected");
  139. }
  140. @end