PositiveNegativeBarChartViewController.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // PositiveNegativeBarChartViewController.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 "PositiveNegativeBarChartViewController.h"
  12. #import "ChartsDemo_iOS-Swift.h"
  13. @interface PositiveNegativeBarChartViewController () <ChartViewDelegate, ChartAxisValueFormatter>
  14. {
  15. NSArray<NSDictionary *> *dataList;
  16. }
  17. @property (nonatomic, strong) IBOutlet BarChartView *chartView;
  18. @end
  19. @implementation PositiveNegativeBarChartViewController
  20. - (void)viewDidLoad
  21. {
  22. [super viewDidLoad];
  23. self.title = @"Positive/Negative Bar Chart";
  24. self.options = @[
  25. @{@"key": @"toggleValues", @"label": @"Toggle Values"},
  26. @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
  27. @{@"key": @"animateX", @"label": @"Animate X"},
  28. @{@"key": @"animateY", @"label": @"Animate Y"},
  29. @{@"key": @"animateXY", @"label": @"Animate XY"},
  30. @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
  31. @{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
  32. @{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
  33. @{@"key": @"toggleData", @"label": @"Toggle Data"},
  34. @{@"key": @"toggleBarBorders", @"label": @"Show Bar Borders"},
  35. ];
  36. [self setupBarLineChartView:_chartView];
  37. _chartView.delegate = self;
  38. _chartView.extraTopOffset = -30.f;
  39. _chartView.extraBottomOffset = 10.f;
  40. _chartView.extraLeftOffset = 70.f;
  41. _chartView.extraRightOffset = 70.f;
  42. _chartView.drawBarShadowEnabled = NO;
  43. _chartView.drawValueAboveBarEnabled = YES;
  44. _chartView.chartDescription.enabled = NO;
  45. // scaling can now only be done on x- and y-axis separately
  46. _chartView.pinchZoomEnabled = NO;
  47. _chartView.drawGridBackgroundEnabled = NO;
  48. ChartXAxis *xAxis = _chartView.xAxis;
  49. xAxis.labelPosition = XAxisLabelPositionBottom;
  50. xAxis.labelFont = [UIFont systemFontOfSize:13.f];
  51. xAxis.drawGridLinesEnabled = NO;
  52. xAxis.drawAxisLineEnabled = NO;
  53. xAxis.labelTextColor = [UIColor lightGrayColor];
  54. xAxis.labelCount = 5;
  55. xAxis.centerAxisLabelsEnabled = YES;
  56. xAxis.granularity = 1.0;
  57. xAxis.valueFormatter = self;
  58. ChartYAxis *leftAxis = _chartView.leftAxis;
  59. leftAxis.drawLabelsEnabled = NO;
  60. leftAxis.spaceTop = 0.25;
  61. leftAxis.spaceBottom = 0.25;
  62. leftAxis.drawAxisLineEnabled = NO;
  63. leftAxis.drawGridLinesEnabled = NO;
  64. leftAxis.drawZeroLineEnabled = YES;
  65. leftAxis.zeroLineColor = UIColor.grayColor;
  66. leftAxis.zeroLineWidth = 0.7f;
  67. _chartView.rightAxis.enabled = NO;
  68. _chartView.legend.enabled = NO;
  69. [self updateChartData];
  70. }
  71. - (void)didReceiveMemoryWarning
  72. {
  73. [super didReceiveMemoryWarning];
  74. // Dispose of any resources that can be recreated.
  75. }
  76. - (void)updateChartData
  77. {
  78. if (self.shouldHideData)
  79. {
  80. _chartView.data = nil;
  81. return;
  82. }
  83. [self setChartData];
  84. }
  85. - (void)setChartData
  86. {
  87. // THIS IS THE ORIGINAL DATA YOU WANT TO PLOT
  88. dataList = @[
  89. @{@"xValue": @(0),
  90. @"yValue": @(-224.1f),
  91. @"xLabel": @"12-19"},
  92. @{@"xValue": @(1),
  93. @"yValue": @(238.5f),
  94. @"xLabel": @"12-30"},
  95. @{@"xValue": @(2),
  96. @"yValue": @(1280.1f),
  97. @"xLabel": @"12-31"},
  98. @{@"xValue": @(3),
  99. @"yValue": @(-442.3f),
  100. @"xLabel": @"01-01"},
  101. @{@"xValue": @(4),
  102. @"yValue": @(-2280.1f),
  103. @"xLabel": @"01-02"},
  104. ];
  105. NSMutableArray<BarChartDataEntry *> *values = [[NSMutableArray alloc] init];
  106. NSMutableArray<UIColor *> *colors = [[NSMutableArray alloc] init];
  107. UIColor *green = [UIColor colorWithRed:110/255.f green:190/255.f blue:102/255.f alpha:1.f];
  108. UIColor *red = [UIColor colorWithRed:211/255.f green:74/255.f blue:88/255.f alpha:1.f];
  109. for (int i = 0; i < dataList.count; i++)
  110. {
  111. NSDictionary *d = dataList[i];
  112. BarChartDataEntry *entry = [[BarChartDataEntry alloc] initWithX:[d[@"xValue"] doubleValue] y:[d[@"yValue"] doubleValue]];
  113. [values addObject:entry];
  114. // specific colors
  115. if ([d[@"yValue"] doubleValue] >= 0.f)
  116. {
  117. [colors addObject:red];
  118. }
  119. else
  120. {
  121. [colors addObject:green];
  122. }
  123. }
  124. BarChartDataSet *set = set = [[BarChartDataSet alloc] initWithEntries:values label:@"Values"];
  125. set.colors = colors;
  126. set.valueColors = colors;
  127. BarChartData *data = [[BarChartData alloc] initWithDataSet:set];
  128. [data setValueFont:[UIFont systemFontOfSize:13.f]];
  129. NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
  130. formatter.maximumFractionDigits = 1;
  131. [data setValueFormatter:[[ChartDefaultValueFormatter alloc] initWithFormatter:formatter]];
  132. data.barWidth = 0.8;
  133. _chartView.data = data;
  134. }
  135. - (void)optionTapped:(NSString *)key
  136. {
  137. [super handleOption:key forChartView:_chartView];
  138. }
  139. #pragma mark - ChartViewDelegate
  140. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
  141. {
  142. NSLog(@"chartValueSelected");
  143. }
  144. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  145. {
  146. NSLog(@"chartValueNothingSelected");
  147. }
  148. #pragma mark - AxisValueFormatter
  149. - (NSString *)stringForValue:(double)value
  150. axis:(ChartAxisBase *)axis
  151. {
  152. return dataList[MIN(MAX((int) value, 0), dataList.count - 1)][@"xLabel"];
  153. }
  154. @end