波浪线图等同于折线图, 只是添加了填充, 另外实现波浪的效果.
实现同样自定义封装了view, 在UIBezierPath的分类中实现波浪的效果:
// .h
#import <UIKit/UIKit.h>
@interface LineChartView : UIView
@property (nonatomic, strong) NSArray *titleForYArr;
@property (nonatomic, strong) NSArray *titleForXArr;
@property (nonatomic, strong) NSArray *valueArr;
@property (nonatomic, strong) UIColor *lineColor;
- (instancetype)initWithFrame:(CGRect)frame;
- (void)startDraw;
@end
// .m
#import "LineChartView.h"
#import "UIBezierPath+category.h"
@interface LineChartView()<CAAnimationDelegate>
@property (nonatomic, strong) CAShapeLayer *lineChartLayer;
@end
@implementation LineChartView {
CGFloat width;
CGFloat height;
}
static CGFloat edgeLeft = 30;
static CGFloat edgeRight = 20;
static CGFloat edgeUp = 50;
static CGFloat edgeDown = 30;
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor lightGrayColor];
width = frame.size.width - edgeLeft - edgeRight;
height = frame.size.height - edgeUp - edgeDown;
}
return self;
}
// 画出坐标轴
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1.0);
CGContextSetRGBStrokeColor(context, 0.6, 0.6, 0.6, 1);
CGContextMoveToPoint(context, edgeLeft, edgeUp);
CGContextAddLineToPoint(context, edgeLeft, edgeUp + height);
CGContextAddLineToPoint(context,edgeLeft + width, edgeUp + height);
CGContextStrokePath(context);
}
#pragma mark 画折线图
- (void)dravLine{
NSInteger yearNum = self.titleForXArr.count;
if (yearNum <= 0) {
return;
}
CGFloat widthForX = width / yearNum;
CGFloat maxValue = [[self.titleForYArr lastObject] floatValue];
if (maxValue <= 0) {
return;
}
UIBezierPath *pathLine = [[UIBezierPath alloc] init];
[pathLine moveToPoint:CGPointMake(edgeLeft + widthForX * 0.5, ((maxValue - [self.valueArr[0] floatValue]) / maxValue) * height + edgeUp)];
// 创建折线点标记
for (NSInteger i = 1; i < self.valueArr.count; i++) {
CGPoint pointCenter = CGPointMake(edgeLeft + widthForX * (i + 0.5), (maxValue - [self.valueArr[i] floatValue]) / maxValue * height + edgeUp);
[pathLine addLineToPoint:pointCenter];
}
// 实现波浪线
pathLine = [pathLine smoothedPathWithGranularity:20];
[pathLine addLineToPoint:CGPointMake(edgeLeft + width - widthForX * 0.5, height + edgeUp)];
[pathLine addLineToPoint:CGPointMake(edgeLeft + widthForX * 0.5, height + edgeUp)];
CAShapeLayer *lineChartLayer = [CAShapeLayer layer];
lineChartLayer.path = pathLine.CGPath;
lineChartLayer.strokeColor = _lineColor.CGColor;
lineChartLayer.fillColor = [[UIColor greenColor] CGColor];
lineChartLayer.lineWidth = 0.0;
lineChartLayer.lineCap = kCALineCapRound;
lineChartLayer.lineJoin = kCALineJoinBevel;
lineChartLayer.lineDashPhase = 5.0;
lineChartLayer.miterLimit = 10.0;
[self.layer addSublayer:lineChartLayer];
}
#pragma mark 创建x轴的数据
- (void)setTitleForXArr:(NSArray *)titleForXArr {
_titleForXArr = titleForXArr;
[self createLabelX];
}
- (void)createLabelX{
NSInteger yearNum = self.titleForXArr.count;
if (yearNum <= 0) {
return;
}
CGFloat widthForX = width / yearNum;
for (NSInteger i = 0; i < yearNum; i++) {
UILabel *labelYear = [[UILabel alloc] initWithFrame:CGRectMake(edgeLeft + widthForX * i, height + edgeUp, widthForX, edgeDown)];
labelYear.tag = 1000 + i;
labelYear.text = self.titleForXArr[i];
labelYear.font = [UIFont systemFontOfSize:14];
labelYear.textAlignment = NSTextAlignmentCenter;
[self addSubview:labelYear];
}
}
#pragma mark 创建y轴数据及虚线
- (void)setTitleForYArr:(NSArray *)titleForYArr {
_titleForYArr = titleForYArr;
[self createLabelY];
[self setLineDash];
}
- (void)createLabelY{
NSInteger numForY = _titleForYArr.count;
if (numForY <= 1) {
return;
}
CGFloat widthForY = edgeLeft;
CGFloat heightForY = height / (numForY - 1);
for (NSInteger i = 0; i < numForY; i++) {
UILabel *labelForY = [[UILabel alloc] initWithFrame:CGRectMake(0, edgeUp + (i - 0.5) * heightForY, widthForY, heightForY)];
labelForY.tag = 2000 + i;
labelForY.text = _titleForYArr[numForY - i - 1];
labelForY.font = [UIFont systemFontOfSize:12];
labelForY.textAlignment = NSTextAlignmentCenter;
[self addSubview:labelForY];
}
}
#pragma mark - 添加虚线
- (void)setLineDash {
NSInteger numForY = _titleForYArr.count - 1;
if (numForY <= 0) {
return;
}
CGFloat heightForY = height / numForY;
for (NSInteger i = 0; i < numForY; i++) {
CAShapeLayer *dashLayer = [CAShapeLayer layer];
dashLayer.strokeColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1].CGColor;
dashLayer.lineWidth = 1.0;
UIBezierPath *path = [[UIBezierPath alloc] init];
path.lineWidth = 1.0;
[path moveToPoint:CGPointMake(edgeLeft, edgeUp + i * heightForY)];
[path addLineToPoint:CGPointMake(edgeLeft + width, edgeUp + i * heightForY)];
CGFloat dash[] = {10,10};
[path setLineDash:dash count:2 phase:10];
[path stroke];
dashLayer.path = path.CGPath;
[self.layer addSublayer:dashLayer];
}
}
#pragma mark - 开始画折线
- (void)startDraw {
[self dravLine];
}
@end
// 分类.h
#import <UIKit/UIKit.h>
@interface UIBezierPath (category)
- (UIBezierPath *)smoothedPathWithGranularity:(NSInteger)granularity;
@end
// 分类.m
#import "UIBezierPath+category.h"
@implementation UIBezierPath (category)
void getPointsFromBezier(void * info, const CGPathElement * element){
NSMutableArray *bezierPoints = (__bridge NSMutableArray *)info;
CGPathElementType type = element->type;
CGPoint * points = element->points;
if (type != kCGPathElementCloseSubpath) {
[bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
if ((type != kCGPathElementAddLineToPoint) && (type != kCGPathElementMoveToPoint)) {
[bezierPoints addObject:[NSValue valueWithCGPoint:points[1]]];
}
}
if (type == kCGPathElementAddCurveToPoint) {
[bezierPoints addObject:[NSValue valueWithCGPoint:points[2]]];
}
}
- (NSMutableArray *)pointsFromBezierPath:(UIBezierPath *)bezierPath{
NSMutableArray * points = [NSMutableArray array];
CGPathApply(bezierPath.CGPath, (__bridge void *)points, getPointsFromBezier);
return points;
}
- (UIBezierPath *)smoothedPathWithGranularity:(NSInteger)granularity{
NSMutableArray * points = [NSMutableArray arrayWithArray:[self pointsFromBezierPath:self]];
if (points.count < 4) {
return [self copy];
}
[points insertObject:[points objectAtIndex:0] atIndex:0];
[points addObject:[points lastObject]];
UIBezierPath * smoothedPath = [self copy];
[smoothedPath removeAllPoints];
[smoothedPath moveToPoint:[(NSValue *)[points objectAtIndex:0] CGPointValue]];
for (NSUInteger i = 1; i < points.count - 2; i++) {
CGPoint p0 = [(NSValue *)[points objectAtIndex:i-1] CGPointValue];
CGPoint p1 = [(NSValue *)[points objectAtIndex:i] CGPointValue];
CGPoint p2 = [(NSValue *)[points objectAtIndex:i+1] CGPointValue];
CGPoint p3 = [(NSValue *)[points objectAtIndex:i+2] CGPointValue];
for (int i = 1; i < granularity; i++)
{
float t = (float) i * (1.0f / (float) granularity);
float tt = t * t;
float ttt = tt * t;
CGPoint pi;
pi.x = 0.5 * (2*p1.x+(p2.x-p0.x)*t + (2*p0.x-5*p1.x+4*p2.x-p3.x)*tt + (3*p1.x-p0.x-3*p2.x+p3.x)*ttt);
pi.y = 0.5 * (2*p1.y+(p2.y-p0.y)*t + (2*p0.y-5*p1.y+4*p2.y-p3.y)*tt + (3*p1.y-p0.y-3*p2.y+p3.y)*ttt);
[smoothedPath addLineToPoint:pi];
}
[smoothedPath addLineToPoint:p2];
}
[smoothedPath addLineToPoint:[(NSValue *)[points objectAtIndex:points.count - 1] CGPointValue]];
return smoothedPath;
}
@end
// 使用
- (void)viewDidLoad {
[super viewDidLoad];
LineChartView *lineView = [[LineChartView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 190)];
lineView.titleForYArr = @[@"0",@"1",@"2",@"3",@"4",@"5"];
lineView.titleForXArr = @[@"2013年",@"2014年",@"2015年",@"2016年",@"2017年"];
lineView.valueArr = @[@"0.8",@"1.9",@"4.0",@"1.3",@"2.5"];
lineView.lineColor = [UIColor redColor];
[self.view addSubview:lineView];
[lineView startDraw];
}