单军华
2016-12-21 8329ef237d1841d377718813a0b452ea0df64378
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
//  JHWaveChart.m
//  JHChartDemo
//
//  Created by cjatech-简豪 on 16/4/13.
//  Copyright © 2016年 JH. All rights reserved.
//
 
#import "JHWaveChart.h"
 
@interface JHWaveChart()
 
 
@property (nonatomic,strong) NSArray * yLineDataArr;
@property (assign , nonatomic) JHWaveChartType  waveChartType ;
@property (assign , nonatomic) CGFloat  perXLength ;
@property (assign , nonatomic) CGFloat  xLength ;
@property (assign , nonatomic) CGFloat  yLength ;
@property (assign , nonatomic) CGPoint  originPoint ;
@end
 
 
@implementation JHWaveChart
 
 
-(instancetype)initWithFrame:(CGRect)frame andType:(JHWaveChartType)waveChartType{
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        _waveChartType = waveChartType;
        _xLineDataArr = @[@0,@1,@2,@3,@4,@5,@6,@7];
        _xAndYLineColor = [UIColor darkGrayColor];
        self.contentInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    }
    
    return self;
}
 
 
 
 
- (void)countPerXandPerYLength{
    _xLength = CGRectGetWidth(self.frame) - self.contentInsets.left - self.contentInsets.right;
    _yLength = CGRectGetHeight(self.frame) - self.contentInsets.top - self.contentInsets.bottom;
    
    if (_xLineDataArr.count) {
        _perXLength = _xLength/(_xLineDataArr.count-1);
    }
    
    switch (_waveChartType) {
        case JHWaveChartUpType:
        {
            _originPoint = P_M(self.contentInsets.left, CGRectGetHeight(self.frame) - self.contentInsets.bottom);
            
        }
            break;
        case JHWaveChartUpAndDownType:
        {
            
        }
            break;
        default:
            break;
    }
    
    
}
 
 
- (void)drawRect:(CGRect)rect {
 
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self countPerXandPerYLength];
    if (_xLineDataArr.count) {
        /* 绘制X轴 */
        [self drawXLineWithContext:context];
        
    }
    
    
}
 
/**
 *  绘制X轴
 *
 */
- (void)drawXLineWithContext:(CGContextRef)contex{
    
    [self drawLineWithContext:contex andStarPoint:P_M(self.contentInsets.left, CGRectGetHeight(self.frame)-self.contentInsets.bottom) andEndPoint:P_M(CGRectGetWidth(self.frame)-self.contentInsets.right, CGRectGetHeight(self.frame)-self.contentInsets.bottom) andIsDottedLine:NO andColor:_xAndYLineColor];
    
    for (NSInteger i =0 ; i<_xLineDataArr.count ; i++) {
        
        [self drawLineWithContext:contex andStarPoint:P_M(i*_perXLength, _originPoint.y) andEndPoint:P_M(i*_perXLength, _originPoint.y-3) andIsDottedLine:NO andColor:_xAndYLineColor];
        CGFloat len = [self getTextWithWhenDrawWithText:_xLineDataArr[i]];
        [self drawText:_xLineDataArr[i] andContext:contex atPoint:P_M(i*_perXLength-len/2+_originPoint.x, _originPoint.y+5) WithColor:_xAndYLineColor andFontSize:8];
        
    }
    
}
 
-(void)setValueDataArr:(NSArray *)valueDataArr{
    
    
    _valueDataArr = valueDataArr;
    
    [self setNeedsDisplay];
    
}
 
 
@end