我在绘制下图时遇到了问题。
首先,我使用CGContextMoveToPoint,CGContextAddLineToPoint和CGContextStrokePath绘制网格线。然后,我用CGContextFillRect绘制图条。
为什么在图表上方绘制线条?
这是请求的代码。有点复杂,因为我想支持几个图表配置。
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rectHistView = self.bounds;
CGFloat red, green, blue, alpha;
NSUInteger valueIndex = 0;
CGFloat histX, histY, histHeight;
CGFloat gridX, gridY, gridHeight;
NSString *txt;
CGSize txtSize;
// Initialize font color and set font drawing mode
CGContextSetTextDrawingMode(context, kCGTextFill);
NSDictionary *attrDictAxisValues = @{NSFontAttributeName:[UIFont systemFontOfSize:cfgAxisValuesFontSize],
NSForegroundColorAttributeName:cfgColorAxisValues};
NSDictionary *attrDictBarValues = @{NSFontAttributeName:[UIFont systemFontOfSize:cfgAxisValuesFontSize],
NSForegroundColorAttributeName:cfgColorBarValues};
// Calculate bar width and gap offset
CGFloat histWidth = rectHistView.size.width / [histValues count];
CGFloat histMaxViewHeight = bCfgShowXAxisValues ? rectHistView.size.height - cfgAxisValuesFontSize : rectHistView.size.height;
CGFloat gapOffset = histWidth * cfgBarGapPercentage / 2;
//--------------------------------------------------------
// Draw y-grid
//--------------------------------------------------------
if (bCfgShowYGrid) {
CGContextSetStrokeColorWithColor(context, HIST_CFG_UICOLOR_GRID.CGColor);
CGContextSetLineWidth(context, HIST_CFG_LINE_WIDTH_GRID);
gridHeight = bCfgShowBarValues ? histMaxViewHeight - cfgAxisValuesFontSize - HIST_CFG_FONT_GAP: histMaxViewHeight;
for (int y = 1; y <= histValueMax; y++) {
gridY = bCfgShowXAxisValues ? rectHistView.origin.y + rectHistView.size.height - (gridHeight * y / histValueMax) - cfgAxisValuesFontSize
: rectHistView.origin.y + rectHistView.size.height - (gridHeight * y / histValueMax);
CGContextMoveToPoint(context, rectHistView.origin.x, gridY);
CGContextAddLineToPoint(context, rectHistView.origin.x + rectHistView.size.width, gridY);
}
CGContextStrokePath(context);
}
//--------------------------------------------------------
// Draw histogram
//--------------------------------------------------------
for (NSString *val in histValues) {
histX = rectHistView.origin.x + (valueIndex * histWidth);
histHeight = bCfgShowBarValues ? (histMaxViewHeight - cfgAxisValuesFontSize - HIST_CFG_FONT_GAP) * [val floatValue] / histValueMax
: histMaxViewHeight * [val floatValue] / histValueMax;
histY = bCfgShowXAxisValues ? rectHistView.origin.y + rectHistView.size.height - histHeight - cfgAxisValuesFontSize
: rectHistView.origin.y + rectHistView.size.height - histHeight;
// draw x-grid
if (bCfgShowXGrid) {
gridX = rectHistView.origin.x + ((valueIndex+1) * histWidth);
gridY = bCfgShowXAxisValues ? rectHistView.origin.y + rectHistView.size.height - cfgAxisValuesFontSize
: rectHistView.origin.y + rectHistView.size.height;
CGContextSetStrokeColorWithColor(context, HIST_CFG_UICOLOR_GRID.CGColor);
CGContextSetLineWidth(context, HIST_CFG_LINE_WIDTH_GRID);
CGContextMoveToPoint(context, gridX, bCfgShowBarValues ? rectHistView.origin.y + cfgAxisValuesFontSize + HIST_CFG_FONT_GAP
: rectHistView.origin.y);
CGContextAddLineToPoint(context, gridX, gridY);
CGContextStrokePath(context);
}
// draw x-axis values
if (bCfgShowXAxisValues) {
txt = [NSString stringWithFormat:@"%lu", (unsigned long)valueIndex];
txtSize = [txt sizeWithAttributes:attrDictAxisValues];
[txt drawAtPoint:CGPointMake(rectHistView.origin.x + (valueIndex * histWidth) + (histWidth / 2.0f - txtSize.width / 2.0f),
rectHistView.origin.y + rectHistView.size.height - cfgAxisValuesFontSize)
withAttributes:attrDictAxisValues];
}
// get color from array
UIColor *color = [histColor objectAtIndex:valueIndex];
[color getRed:&red green:&green blue:&blue alpha:&alpha];
// draw histogram bar
CGContextSetRGBFillColor(context, red, green, blue, alpha);
CGContextFillRect(context, CGRectMake(histX+gapOffset, histY, histWidth-(2*gapOffset), histHeight));
// draw histogram bar values
if (bCfgShowBarValues) {
if (bCfgShowZeroBarValues || [val intValue] > 0) { // skip zero values if configured
txt = [NSString stringWithFormat:@"%d", [val intValue]];
CGSize txtSize = [txt sizeWithAttributes:attrDictBarValues];
[txt drawAtPoint:CGPointMake(rectHistView.origin.x + (valueIndex * histWidth) + (histWidth / 2.0f - txtSize.width / 2.0f),
histY - cfgAxisValuesFontSize - HIST_CFG_FONT_GAP)
withAttributes:attrDictBarValues];
}
}
valueIndex++;
}
//--------------------------------------------------------
// Draw diagram axis
//--------------------------------------------------------
CGContextSetStrokeColorWithColor(context, HIST_CFG_UICOLOR_AXIS.CGColor);
CGContextSetLineWidth(context, HIST_CFG_LINE_WIDTH_AXIS);
// draw y-axis
if (bCfgShowYAxis) {
CGContextMoveToPoint(context, rectHistView.origin.x,
bCfgShowBarValues ? rectHistView.origin.y + cfgAxisValuesFontSize + HIST_CFG_FONT_GAP: rectHistView.origin.y);
CGContextAddLineToPoint(context, rectHistView.origin.x, rectHistView.origin.y + histMaxViewHeight);
CGContextStrokePath(context);
}
// draw x-axis
if (bCfgShowXAxis) {
histY = bCfgShowXAxisValues ? rectHistView.origin.y + rectHistView.size.height - cfgAxisValuesFontSize
: rectHistView.origin.y + rectHistView.size.height;
CGContextMoveToPoint(context, rectHistView.origin.x, histY);
CGContextAddLineToPoint(context, rectHistView.origin.x + rectHistView.size.width, histY);
CGContextStrokePath(context);
}
}