AnotherBarChartViewController.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // AnotherBarChartViewController.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 "AnotherBarChartViewController.h"
  12. #import "ChartsDemo_iOS-Swift.h"
  13. @interface AnotherBarChartViewController () <ChartViewDelegate>
  14. @property (nonatomic, strong) IBOutlet BarChartView *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 AnotherBarChartViewController
  21. - (void)viewDidLoad
  22. {
  23. [super viewDidLoad];
  24. self.title = @"Another Bar Chart";
  25. self.options = @[
  26. @{@"key": @"toggleValues", @"label": @"Toggle Values"},
  27. @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
  28. @{@"key": @"animateX", @"label": @"Animate X"},
  29. @{@"key": @"animateY", @"label": @"Animate Y"},
  30. @{@"key": @"animateXY", @"label": @"Animate XY"},
  31. @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
  32. @{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
  33. @{@"key": @"toggleData", @"label": @"Toggle Data"},
  34. @{@"key": @"toggleBarBorders", @"label": @"Show Bar Borders"},
  35. ];
  36. _chartView.delegate = self;
  37. _chartView.chartDescription.enabled = NO;
  38. _chartView.maxVisibleCount = 60;
  39. _chartView.pinchZoomEnabled = NO;
  40. _chartView.drawBarShadowEnabled = NO;
  41. _chartView.drawGridBackgroundEnabled = NO;
  42. ChartXAxis *xAxis = _chartView.xAxis;
  43. xAxis.labelPosition = XAxisLabelPositionBottom;
  44. xAxis.drawGridLinesEnabled = NO;
  45. _chartView.leftAxis.drawGridLinesEnabled = NO;
  46. _chartView.rightAxis.drawGridLinesEnabled = NO;
  47. _chartView.legend.enabled = NO;
  48. _sliderX.value = 10.0;
  49. _sliderY.value = 100.0;
  50. [self slidersValueChanged:nil];
  51. }
  52. - (void)didReceiveMemoryWarning
  53. {
  54. [super didReceiveMemoryWarning];
  55. // Dispose of any resources that can be recreated.
  56. }
  57. - (void)updateChartData
  58. {
  59. if (self.shouldHideData)
  60. {
  61. _chartView.data = nil;
  62. return;
  63. }
  64. [self setDataCount:_sliderX.value + 1 range:_sliderY.value];
  65. }
  66. - (void)setDataCount:(int)count range:(double)range
  67. {
  68. NSMutableArray *yVals = [[NSMutableArray alloc] init];
  69. for (int i = 0; i < count; i++)
  70. {
  71. double mult = (range + 1);
  72. double val = (double) (arc4random_uniform(mult)) + mult / 3.0;
  73. [yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:val]];
  74. }
  75. BarChartDataSet *set1 = nil;
  76. if (_chartView.data.dataSetCount > 0)
  77. {
  78. set1 = (BarChartDataSet *)_chartView.data.dataSets[0];
  79. [set1 replaceEntries:yVals];
  80. [_chartView.data notifyDataChanged];
  81. [_chartView notifyDataSetChanged];
  82. }
  83. else
  84. {
  85. set1 = [[BarChartDataSet alloc] initWithEntries:yVals label:@"DataSet"];
  86. set1.colors = ChartColorTemplates.vordiplom;
  87. set1.drawValuesEnabled = NO;
  88. NSMutableArray *dataSets = [[NSMutableArray alloc] init];
  89. [dataSets addObject:set1];
  90. BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
  91. _chartView.data = data;
  92. _chartView.fitBars = YES;
  93. }
  94. [_chartView setNeedsDisplay];
  95. }
  96. - (void)optionTapped:(NSString *)key
  97. {
  98. [super handleOption:key forChartView:_chartView];
  99. }
  100. #pragma mark - Actions
  101. - (IBAction)slidersValueChanged:(id)sender
  102. {
  103. _sliderTextX.text = [@((int)_sliderX.value) stringValue];
  104. _sliderTextY.text = [@((int)_sliderY.value) stringValue];
  105. [self updateChartData];
  106. }
  107. #pragma mark - ChartViewDelegate
  108. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
  109. {
  110. NSLog(@"chartValueSelected");
  111. }
  112. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  113. {
  114. NSLog(@"chartValueNothingSelected");
  115. }
  116. @end