一喘帚、背景圖(表格):
- (void)drawTableLine {
NSInteger count = 9;
CGFloat spacing_X = self.frame.size.width/(count+1);
CGFloat spacing_Y = self.frame.size.height/8;
CGContextRef tableContext = UIGraphicsGetCurrentContext();
CGMutablePathRef verticalPath = CGPathCreateMutable();
for (int i = 0; i < count + 1; i++) {
// 垂直線(xiàn)
CGPathMoveToPoint(verticalPath, NULL, i*spacing_X, 0);
CGPathAddLineToPoint(verticalPath, NULL, i*spacing_X, self.frame.size.height);
CGContextStrokePath(tableContext);
// [[UIColor blueColor] setStroke];
// CGContextAddPath(tableContext, verticalPath);
// CGContextDrawPath(tableContext, kCGPathStroke);
}
for ( int i = 0; i < 8; i++) {
// 水平線(xiàn)
CGPathMoveToPoint(verticalPath, NULL, 0, i*spacing_Y);
CGPathAddLineToPoint(verticalPath, NULL, self.frame.size.width, i*spacing_Y);
CGContextStrokePath(tableContext);
[[UIColor orangeColor] setStroke];
CGContextAddPath(tableContext, verticalPath);
CGContextDrawPath(tableContext, kCGPathStroke);
}
CGPathRelease(verticalPath);
}
效果圖:
二诡壁、折線(xiàn)圖:
- (void)drawGraph {
NSInteger count = self.valueArray.count;
NSInteger max = [[self.valueArray valueForKeyPath:@"@max.floatValue"] integerValue]+2;
NSInteger min = [[self.valueArray valueForKeyPath:@"@min.floatValue"] integerValue]-2;
CGFloat spacing_X = self.frame.size.width/(count+1);
CGFloat spacing_Y = self.frame.size.height/(max-min);
CGContextRef graphContext = UIGraphicsGetCurrentContext();
CGMutablePathRef graphPath = CGPathCreateMutable();
for (int i = 0; i < count-1; i++) {
NSInteger value = [self.valueArray[i] integerValue];
NSInteger value1 = [self.valueArray[i+1] integerValue];
CGPathMoveToPoint(graphPath, NULL, (i+1)*spacing_X+PointRadius, (max-value)*spacing_Y);
CGPathAddLineToPoint(graphPath, NULL, (i+2)*spacing_X-PointRadius, (max-value1)*spacing_Y);
CGContextSetLineWidth(graphContext, 0.5);
CGContextAddPath(graphContext, graphPath);
CGContextStrokePath(graphContext);
[[UIColor blueColor] setStroke];
CGContextDrawPath(graphContext, kCGPathStroke);
}
CGPathRelease(graphPath);
}
效果如圖:
三障贸、圓點(diǎn):
在每個(gè)坐標(biāo)點(diǎn)加一個(gè)圓點(diǎn)
// 注:折線(xiàn)圖中的點(diǎn)可以使用button/label實(shí)現(xiàn) 也可以使用畫(huà)點(diǎn)實(shí)現(xiàn)
- (void)drawPoint {
NSInteger count = self.valueArray.count;
CGContextRef pointContext = UIGraphicsGetCurrentContext();
CGMutablePathRef pointPath = CGPathCreateMutable();
NSInteger max = [[self.valueArray valueForKeyPath:@"@max.floatValue"] integerValue]+2;
NSInteger min = [[self.valueArray valueForKeyPath:@"@min.floatValue"] integerValue]-2;
CGFloat spacing_X = self.frame.size.width/(count+1);
CGFloat spacing_Y = self.frame.size.height/(max-min);
for (int i = 0; i < count; i++) {
NSInteger value = [self.valueArray[i] integerValue];
CGRect rect = CGRectMake((i+1)*spacing_X-PointRadius, (max-value)*spacing_Y-PointRadius, 2*PointRadius, 2*PointRadius);
CGPathAddEllipseInRect(pointPath, NULL, rect);
CGContextAddPath(pointContext, pointPath);
CGContextStrokePath(pointContext);
[[UIColor blueColor] setStroke];
CGContextDrawPath(pointContext, kCGPathFill);
}
}
效果圖如下: