BubbleChartViewController.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // BubbleChartViewController.m
  3. // ChartsDemo
  4. //
  5. // Bubble chart implementation:
  6. // Copyright 2015 Pierre-Marc Airoldi
  7. // Licensed under Apache License 2.0
  8. //
  9. // https://github.com/danielgindi/Charts
  10. //
  11. #import "BubbleChartViewController.h"
  12. #import "ChartsDemo_iOS-Swift.h"
  13. @interface BubbleChartViewController () <ChartViewDelegate>
  14. @property (nonatomic, strong) IBOutlet BubbleChartView *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 BubbleChartViewController
  21. - (void)viewDidLoad
  22. {
  23. [super viewDidLoad];
  24. self.title = @"Bubble 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. ];
  37. _chartView.delegate = self;
  38. _chartView.chartDescription.enabled = NO;
  39. _chartView.drawGridBackgroundEnabled = NO;
  40. _chartView.dragEnabled = YES;
  41. [_chartView setScaleEnabled:YES];
  42. _chartView.maxVisibleCount = 200;
  43. _chartView.pinchZoomEnabled = YES;
  44. ChartLegend *l = _chartView.legend;
  45. l.horizontalAlignment = ChartLegendHorizontalAlignmentRight;
  46. l.verticalAlignment = ChartLegendVerticalAlignmentTop;
  47. l.orientation = ChartLegendOrientationVertical;
  48. l.drawInside = NO;
  49. l.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
  50. ChartYAxis *yl = _chartView.leftAxis;
  51. yl.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
  52. yl.spaceTop = 0.3;
  53. yl.spaceBottom = 0.3;
  54. yl.axisMinimum = 0.0; // this replaces startAtZero = YES
  55. _chartView.rightAxis.enabled = NO;
  56. ChartXAxis *xl = _chartView.xAxis;
  57. xl.labelPosition = XAxisLabelPositionBottom;
  58. xl.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
  59. _sliderX.value = 10.0;
  60. _sliderY.value = 50.0;
  61. [self slidersValueChanged:nil];
  62. }
  63. - (void)didReceiveMemoryWarning
  64. {
  65. [super didReceiveMemoryWarning];
  66. // Dispose of any resources that can be recreated.
  67. }
  68. - (void)updateChartData
  69. {
  70. if (self.shouldHideData)
  71. {
  72. _chartView.data = nil;
  73. return;
  74. }
  75. [self setDataCount:_sliderX.value range:_sliderY.value];
  76. }
  77. - (void)setDataCount:(int)count range:(double)range
  78. {
  79. NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
  80. NSMutableArray *yVals2 = [[NSMutableArray alloc] init];
  81. NSMutableArray *yVals3 = [[NSMutableArray alloc] init];
  82. for (int i = 0; i < count; i++)
  83. {
  84. double val = (double) (arc4random_uniform(range));
  85. double size = (double) (arc4random_uniform(range));
  86. [yVals1 addObject:[[BubbleChartDataEntry alloc] initWithX:i y:val size:size icon: [UIImage imageNamed:@"icon"]]];
  87. val = (double) (arc4random_uniform(range));
  88. size = (double) (arc4random_uniform(range));
  89. [yVals2 addObject:[[BubbleChartDataEntry alloc] initWithX:i y:val size:size icon: [UIImage imageNamed:@"icon"]]];
  90. val = (double) (arc4random_uniform(range));
  91. size = (double) (arc4random_uniform(range));
  92. [yVals3 addObject:[[BubbleChartDataEntry alloc] initWithX:i y:val size:size]];
  93. }
  94. BubbleChartDataSet *set1 = [[BubbleChartDataSet alloc] initWithValues:yVals1 label:@"DS 1"];
  95. set1.drawIconsEnabled = NO;
  96. [set1 setColor:ChartColorTemplates.colorful[0] alpha:0.50f];
  97. [set1 setDrawValuesEnabled:YES];
  98. BubbleChartDataSet *set2 = [[BubbleChartDataSet alloc] initWithValues:yVals2 label:@"DS 2"];
  99. set2.iconsOffset = CGPointMake(0, 15);
  100. [set2 setColor:ChartColorTemplates.colorful[1] alpha:0.50f];
  101. [set2 setDrawValuesEnabled:YES];
  102. BubbleChartDataSet *set3 = [[BubbleChartDataSet alloc] initWithValues:yVals3 label:@"DS 3"];
  103. [set3 setColor:ChartColorTemplates.colorful[2] alpha:0.50f];
  104. [set3 setDrawValuesEnabled:YES];
  105. NSMutableArray *dataSets = [[NSMutableArray alloc] init];
  106. [dataSets addObject:set1];
  107. [dataSets addObject:set2];
  108. [dataSets addObject:set3];
  109. BubbleChartData *data = [[BubbleChartData alloc] initWithDataSets:dataSets];
  110. [data setDrawValues:NO];
  111. [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:7.f]];
  112. [data setHighlightCircleWidth: 1.5];
  113. [data setValueTextColor:UIColor.whiteColor];
  114. _chartView.data = data;
  115. }
  116. - (void)optionTapped:(NSString *)key
  117. {
  118. [super handleOption:key forChartView:_chartView];
  119. }
  120. #pragma mark - Actions
  121. - (IBAction)slidersValueChanged:(id)sender
  122. {
  123. _sliderTextX.text = [@((int)_sliderX.value) stringValue];
  124. _sliderTextY.text = [@((int)_sliderY.value) stringValue];
  125. [self updateChartData];
  126. }
  127. #pragma mark - ChartViewDelegate
  128. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull )entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
  129. {
  130. NSLog(@"chartValueSelected");
  131. }
  132. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  133. {
  134. NSLog(@"chartValueNothingSelected");
  135. }
  136. @end