DVYBarView.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // DVYBarView.m
  3. // xxxxx
  4. //
  5. // Created by Fire on 15/11/11.
  6. // Copyright © 2015年 DuoLaiDian. All rights reserved.
  7. //
  8. #import "DVYBarView.h"
  9. #import "UIView+Extension.h"
  10. #import "UIColor+Hex.h"
  11. #define random(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]
  12. #define randomColor random(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
  13. @interface DVYBarView ()
  14. @property (strong, nonatomic) UIView *separate;
  15. @end
  16. @implementation DVYBarView
  17. - (instancetype)initWithFrame:(CGRect)frame {
  18. if (self = [super initWithFrame:frame]) {
  19. // 垂直坐标轴
  20. UIView *separate = [[UIView alloc] init];
  21. self.separate = separate;
  22. [self addSubview:separate];
  23. self.textFont = [UIFont systemFontOfSize:12];
  24. }
  25. return self;
  26. }
  27. - (void)draw {
  28. self.backgroundColor = self.backColor;
  29. // 计算坐标轴的位置以及大小
  30. // NSDictionary *attr = @{NSFontAttributeName : self.textFont};
  31. // CGSize labelSize = [@"x" sizeWithAttributes:attr];
  32. CGSize labelSize = CGSizeMake(self.barWidth, 29);
  33. self.separate.backgroundColor = self.axisColor;
  34. self.separate.x = self.width - 1;
  35. self.separate.width = 1;
  36. self.separate.y = 0;
  37. if (self.landspace) {
  38. self.separate.height = self.height - labelSize.height - self.xAxisTextGap;
  39. }else{
  40. self.separate.height = self.height - self.xAxisTextGap;
  41. }
  42. // 为顶部留出的空白
  43. CGFloat topMargin = 50;
  44. // Label做占据的高度
  45. CGFloat allLabelHeight = self.height - topMargin - self.xAxisTextGap - labelSize.height;
  46. if (self.landspace) {
  47. allLabelHeight = self.height - topMargin - self.xAxisTextGap - labelSize.height;
  48. } else {
  49. allLabelHeight = self.height - topMargin - self.xAxisTextGap;
  50. }
  51. NSDictionary *attr = @{NSFontAttributeName : self.textFont};
  52. CGSize yLabelSize = [@"x" sizeWithAttributes:attr];
  53. // Label之间的间隙
  54. CGFloat labelMargin = (allLabelHeight + yLabelSize.height - (self.numberOfYAxisElements + 1) * yLabelSize.height) / self.numberOfYAxisElements;
  55. // 移除所有的Label
  56. for (UILabel *label in self.subviews) {
  57. if ([label isKindOfClass:[UILabel class]]) {
  58. [label removeFromSuperview];
  59. }
  60. }
  61. // 添加Label
  62. for (int i = 0; i < self.numberOfYAxisElements + 1; i++) {
  63. UILabel *label = [[UILabel alloc] init];
  64. CGFloat avgValue = self.yAxisMaxValue / (self.numberOfYAxisElements);
  65. if (self.isPercent) {
  66. label.text = [NSString stringWithFormat:@"%.0f%%", avgValue * i];
  67. }else{
  68. label.text = [NSString stringWithFormat:@"%.0f", avgValue * i];
  69. }
  70. label.text = @"";
  71. //label.backgroundColor = [UIColor redColor];
  72. label.textAlignment = NSTextAlignmentRight;// UITextAlignmentRight;
  73. label.font = self.textFont;
  74. label.textColor = self.textColor;
  75. label.x = 0;
  76. label.height = yLabelSize.height;
  77. if (self.landspace) {
  78. label.y = self.height - labelSize.height - self.xAxisTextGap - (label.height + labelMargin) * i - label.height/2;
  79. } else {
  80. label.y = self.height - self.xAxisTextGap - (label.height + labelMargin) * i - label.height/2;
  81. }
  82. label.width = self.width - 1 - self.yAxisTextGap;
  83. // [self addSubview:label];
  84. }
  85. //添加图例
  86. UIView *LegendView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width - 1 , self.height)];
  87. [ self addSubview:LegendView];
  88. //图例1
  89. UIView *legendView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 50, 20, 10)];
  90. legendView1.centerX = LegendView.centerX;
  91. legendView1.backgroundColor =[UIColor colorWithHexString:@"#FEC591"]; ;
  92. [LegendView addSubview:legendView1];
  93. //FEC591
  94. UILabel *legendLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 50 + 10 + 5, self.width - 1, 60)];
  95. legendLabel1.textColor = [UIColor colorWithHexString:@"888888"];
  96. legendLabel1.textAlignment = NSTextAlignmentCenter;
  97. legendLabel1.numberOfLines = 0;
  98. legendLabel1.font = [UIFont boldSystemFontOfSize:9];
  99. legendLabel1.text = self.legendTitle1;
  100. [LegendView addSubview:legendLabel1];
  101. //图例2
  102. UIView *legendView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 150, 20, 10)];
  103. legendView2.centerX = LegendView.centerX;
  104. legendView2.backgroundColor = [UIColor colorWithHexString:@"#FD8B23"];
  105. [LegendView addSubview:legendView2];
  106. UILabel *legendLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 150 + 10 + 5, self.width - 1, 60)];
  107. legendLabel2.textColor = [UIColor colorWithHexString:@"888888"];
  108. legendLabel2.textAlignment = NSTextAlignmentCenter;
  109. legendLabel2.numberOfLines = 0;
  110. legendLabel2.font = [UIFont boldSystemFontOfSize:9];
  111. legendLabel2.text = self.legendTitle2;
  112. [LegendView addSubview:legendLabel2];
  113. }
  114. @end