NegativeStackedBarChartViewController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // NegativeStackedBarChartViewController.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 "NegativeStackedBarChartViewController.h"
  12. #import "ChartsDemo_iOS-Swift.h"
  13. @interface NegativeStackedBarChartViewController () <ChartViewDelegate, ChartAxisValueFormatter>
  14. @property (nonatomic, strong) IBOutlet HorizontalBarChartView *chartView;
  15. @end
  16. @implementation NegativeStackedBarChartViewController
  17. - (void)viewDidLoad
  18. {
  19. [super viewDidLoad];
  20. self.title = @"Stacked Bar Chart Negative";
  21. self.options = @[
  22. @{@"key": @"toggleValues", @"label": @"Toggle Values"},
  23. @{@"key": @"toggleIcons", @"label": @"Toggle Icons"},
  24. @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
  25. @{@"key": @"animateX", @"label": @"Animate X"},
  26. @{@"key": @"animateY", @"label": @"Animate Y"},
  27. @{@"key": @"animateXY", @"label": @"Animate XY"},
  28. @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
  29. @{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
  30. @{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
  31. @{@"key": @"toggleData", @"label": @"Toggle Data"},
  32. @{@"key": @"toggleBarBorders", @"label": @"Show Bar Borders"},
  33. ];
  34. NSNumberFormatter *customFormatter = [[NSNumberFormatter alloc] init];
  35. customFormatter.negativePrefix = @"";
  36. customFormatter.positiveSuffix = @"m";
  37. customFormatter.negativeSuffix = @"m";
  38. customFormatter.minimumSignificantDigits = 1;
  39. customFormatter.minimumFractionDigits = 1;
  40. _chartView.delegate = self;
  41. _chartView.chartDescription.enabled = NO;
  42. _chartView.drawBarShadowEnabled = NO;
  43. _chartView.drawValueAboveBarEnabled = YES;
  44. _chartView.highlightFullBarEnabled = NO;
  45. // scaling can now only be done on x- and y-axis separately
  46. _chartView.pinchZoomEnabled = NO;
  47. _chartView.drawBarShadowEnabled = NO;
  48. _chartView.leftAxis.enabled = NO;
  49. _chartView.rightAxis.axisMaximum = 25.0;
  50. _chartView.rightAxis.axisMinimum = -25.0;
  51. _chartView.rightAxis.drawGridLinesEnabled = NO;
  52. _chartView.rightAxis.drawZeroLineEnabled = YES;
  53. _chartView.rightAxis.labelCount = 7;
  54. _chartView.rightAxis.valueFormatter = [[ChartDefaultAxisValueFormatter alloc] initWithFormatter:customFormatter];
  55. _chartView.rightAxis.labelFont = [UIFont systemFontOfSize:9.f];
  56. ChartXAxis *xAxis = _chartView.xAxis;
  57. xAxis.labelPosition = XAxisLabelPositionBothSided;
  58. xAxis.drawGridLinesEnabled = NO;
  59. xAxis.drawAxisLineEnabled = NO;
  60. xAxis.axisMinimum = 0.0;
  61. xAxis.axisMaximum = 110.0;
  62. xAxis.centerAxisLabelsEnabled = YES;
  63. xAxis.labelCount = 12;
  64. xAxis.granularity = 10.0;
  65. xAxis.valueFormatter = self;
  66. _chartView.rightAxis.labelFont = [UIFont systemFontOfSize:9.f];
  67. ChartLegend *l = _chartView.legend;
  68. l.horizontalAlignment = ChartLegendHorizontalAlignmentRight;
  69. l.verticalAlignment = ChartLegendVerticalAlignmentBottom;
  70. l.orientation = ChartLegendOrientationHorizontal;
  71. l.drawInside = NO;
  72. l.formSize = 8.f;
  73. l.formToTextSpace = 4.f;
  74. l.xEntrySpace = 6.f;
  75. [self updateChartData];
  76. }
  77. - (void)updateChartData
  78. {
  79. if (self.shouldHideData)
  80. {
  81. _chartView.data = nil;
  82. return;
  83. }
  84. [self setChartData];
  85. }
  86. - (void)setChartData
  87. {
  88. NSMutableArray *yValues = [NSMutableArray array];
  89. [yValues addObject:[[BarChartDataEntry alloc] initWithX:5 yValues:@[ @-10, @10 ]]];
  90. [yValues addObject:[[BarChartDataEntry alloc] initWithX:15 yValues:@[ @-12, @13 ]]];
  91. [yValues addObject:[[BarChartDataEntry alloc] initWithX:25 yValues:@[ @-15, @15 ]]];
  92. [yValues addObject:[[BarChartDataEntry alloc] initWithX:35 yValues:@[ @-17, @17 ]]];
  93. [yValues addObject:[[BarChartDataEntry alloc] initWithX:45 yValues:@[ @-19, @20 ] icon: [UIImage imageNamed:@"icon"]]];
  94. [yValues addObject:[[BarChartDataEntry alloc] initWithX:55 yValues:@[ @-19, @19 ]]];
  95. [yValues addObject:[[BarChartDataEntry alloc] initWithX:65 yValues:@[ @-16, @16 ]]];
  96. [yValues addObject:[[BarChartDataEntry alloc] initWithX:75 yValues:@[ @-13, @14 ]]];
  97. [yValues addObject:[[BarChartDataEntry alloc] initWithX:85 yValues:@[ @-10, @11 ]]];
  98. [yValues addObject:[[BarChartDataEntry alloc] initWithX:95 yValues:@[ @-5, @6 ]]];
  99. [yValues addObject:[[BarChartDataEntry alloc] initWithX:105 yValues:@[ @-1, @2 ]]];
  100. BarChartDataSet *set = nil;
  101. if (_chartView.data.dataSetCount > 0)
  102. {
  103. set = (BarChartDataSet *)_chartView.data.dataSets[0];
  104. [set replaceEntries: yValues];
  105. [_chartView.data notifyDataChanged];
  106. [_chartView notifyDataSetChanged];
  107. }
  108. else
  109. {
  110. NSNumberFormatter *customFormatter = [[NSNumberFormatter alloc] init];
  111. customFormatter.negativePrefix = @"";
  112. customFormatter.positiveSuffix = @"m";
  113. customFormatter.negativeSuffix = @"m";
  114. customFormatter.minimumSignificantDigits = 1;
  115. customFormatter.minimumFractionDigits = 1;
  116. set = [[BarChartDataSet alloc] initWithEntries:yValues label:@"Age Distribution"];
  117. set.drawIconsEnabled = NO;
  118. set.valueFormatter = [[ChartDefaultValueFormatter alloc] initWithFormatter:customFormatter];
  119. set.valueFont = [UIFont systemFontOfSize:7.f];
  120. set.axisDependency = AxisDependencyRight;
  121. set.colors = @[
  122. [UIColor colorWithRed:67/255.f green:67/255.f blue:72/255.f alpha:1.f],
  123. [UIColor colorWithRed:124/255.f green:181/255.f blue:236/255.f alpha:1.f]
  124. ];
  125. set.stackLabels = @[
  126. @"Men", @"Women"
  127. ];
  128. BarChartData *data = [[BarChartData alloc] initWithDataSet:set];
  129. data.barWidth = 8.5;
  130. _chartView.data = data;
  131. [_chartView setNeedsDisplay];
  132. }
  133. }
  134. - (void)didReceiveMemoryWarning
  135. {
  136. [super didReceiveMemoryWarning];
  137. // Dispose of any resources that can be recreated.
  138. }
  139. - (void)optionTapped:(NSString *)key
  140. {
  141. [super handleOption:key forChartView:_chartView];
  142. }
  143. #pragma mark - ChartViewDelegate
  144. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
  145. {
  146. NSLog(@"chartValueSelected, stack-index %ld", (long)highlight.stackIndex);
  147. }
  148. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  149. {
  150. NSLog(@"chartValueNothingSelected");
  151. }
  152. #pragma mark - AxisValueFormatter
  153. - (NSString *)stringForValue:(double)value
  154. axis:(ChartAxisBase *)axis
  155. {
  156. return [NSString stringWithFormat:@"%03.0f-%03.0f", value, value + 10.0];
  157. }
  158. @end