SinusBarChartViewController.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // SinusBarChartViewController.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 "SinusBarChartViewController.h"
  12. #import "ChartsDemo_iOS-Swift.h"
  13. @interface SinusBarChartViewController () <ChartViewDelegate>
  14. @property (nonatomic, strong) IBOutlet BarChartView *chartView;
  15. @property (nonatomic, strong) IBOutlet UISlider *sliderX;
  16. @property (nonatomic, strong) IBOutlet UITextField *sliderTextX;
  17. @end
  18. @implementation SinusBarChartViewController
  19. - (void)viewDidLoad
  20. {
  21. [super viewDidLoad];
  22. self.title = @"Sinus Bar Chart";
  23. self.options = @[
  24. @{@"key": @"toggleValues", @"label": @"Toggle Values"},
  25. @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
  26. @{@"key": @"animateX", @"label": @"Animate X"},
  27. @{@"key": @"animateY", @"label": @"Animate Y"},
  28. @{@"key": @"animateXY", @"label": @"Animate XY"},
  29. @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
  30. @{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
  31. @{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
  32. @{@"key": @"toggleData", @"label": @"Toggle Data"},
  33. ];
  34. _chartView.delegate = self;
  35. _chartView.chartDescription.enabled = NO;
  36. _chartView.drawBarShadowEnabled = NO;
  37. _chartView.drawValueAboveBarEnabled = YES;
  38. _chartView.maxVisibleCount = 60;
  39. _chartView.pinchZoomEnabled = NO;
  40. _chartView.drawGridBackgroundEnabled = NO;
  41. ChartXAxis *xAxis = _chartView.xAxis;
  42. xAxis.labelPosition = XAxisLabelPositionBottom;
  43. xAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
  44. xAxis.drawGridLinesEnabled = NO;
  45. xAxis.enabled = NO;
  46. ChartYAxis *leftAxis = _chartView.leftAxis;
  47. leftAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
  48. leftAxis.labelCount = 6;
  49. leftAxis.axisMinimum = -2.5;
  50. leftAxis.axisMaximum = 2.5;
  51. leftAxis.granularityEnabled = true;
  52. leftAxis.granularity = 0.1;
  53. ChartYAxis *rightAxis = _chartView.rightAxis;
  54. rightAxis.drawGridLinesEnabled = NO;
  55. rightAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
  56. rightAxis.labelCount = 6;
  57. rightAxis.axisMinimum = -2.5;
  58. rightAxis.axisMaximum = 2.5;
  59. rightAxis.granularity = 0.1;
  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 = 9.0;
  67. l.font = [UIFont systemFontOfSize:11.f];
  68. l.xEntrySpace = 4.0;
  69. _sliderX.value = 150.0;
  70. [self slidersValueChanged:nil];
  71. [_chartView animateWithXAxisDuration:2.0 yAxisDuration:2.0];
  72. }
  73. - (void)didReceiveMemoryWarning
  74. {
  75. [super didReceiveMemoryWarning];
  76. // Dispose of any resources that can be recreated.
  77. }
  78. - (void)updateChartData
  79. {
  80. if (self.shouldHideData)
  81. {
  82. _chartView.data = nil;
  83. return;
  84. }
  85. [self setDataCount:(_sliderX.value)];
  86. }
  87. - (void)setDataCount:(int)count
  88. {
  89. NSMutableArray *entries = [[NSMutableArray alloc] init];
  90. for (int i = 0; i < count; i++)
  91. {
  92. [entries addObject:[[BarChartDataEntry alloc] initWithX:(double)i y:sinf(M_PI * (i % 128) / 64.0)]];
  93. }
  94. BarChartDataSet *set = nil;
  95. if (_chartView.data.dataSetCount > 0)
  96. {
  97. set = (BarChartDataSet *)_chartView.data.dataSets[0];
  98. [set replaceEntries: entries];
  99. [_chartView.data notifyDataChanged];
  100. [_chartView notifyDataSetChanged];
  101. }
  102. else
  103. {
  104. set = [[BarChartDataSet alloc] initWithEntries:entries label:@"Sinus Function"];
  105. [set setColor:[UIColor colorWithRed:240/255.f green:120/255.f blue:124/255.f alpha:1.f]];
  106. BarChartData *data = [[BarChartData alloc] initWithDataSet:set];
  107. [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
  108. [data setDrawValues:NO];
  109. data.barWidth = 0.8;
  110. _chartView.data = data;
  111. }
  112. }
  113. - (void)optionTapped:(NSString *)key
  114. {
  115. [super handleOption:key forChartView:_chartView];
  116. }
  117. #pragma mark - Actions
  118. - (IBAction)slidersValueChanged:(id)sender
  119. {
  120. _sliderTextX.text = [@((int)_sliderX.value) stringValue];
  121. [self updateChartData];
  122. }
  123. #pragma mark - ChartViewDelegate
  124. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
  125. {
  126. NSLog(@"chartValueSelected");
  127. }
  128. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  129. {
  130. NSLog(@"chartValueNothingSelected");
  131. }
  132. @end