概述
项目中会使用坐标图来展示数据的变换,那么就会使用一些ios中的绘制线条的功能,例如:
//**两点之间画实线
-(void)drawLineFrom:(CGPoint)origion to:(CGPoint)endPoint context:(CGContextRef)context lineColor:(UIColor *)lineColor{
//**画线 指定背景 两点 和颜色
CGContextSetLineWidth(context, 2.0f);
CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
CGContextMoveToPoint(context, origion.x,origion.y);
CGContextAddLineToPoint(context,endPoint.x,endPoint.y);
CGContextStrokePath(context);
CGContextClosePath(context);
}
//**两点之间画虚线
-(void)drawDashedLineFrom:(CGPoint)origion to:(CGPoint)endPoint context:(CGContextRef)context lineColor:(UIColor *)lineColor{
CGFloat lengths[] = {3,3};
CGFloat lengthsEnd[]= {3,0};
//**画线 指定背景 两点 和颜色
CGContextSetLineDash(context, 0, lengths, 2);
CGContextSetLineWidth(context, 0.5f);
CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
CGContextMoveToPoint(context, origion.x,origion.y);
CGContextAddLineToPoint(context,endPoint.x,endPoint.y);
CGContextStrokePath(context);
CGContextClosePath(context);
//**关闭曲线
CGContextSetLineDash(context, 0, lengthsEnd, 2);
}
//**绘制Y轴上的文字标记
-(void)drawY_AxisDesc:(NSArray *)array origion:(CGPoint)zero heigh:(CGFloat)heigh context:(CGContextRef)ref{
if(!array||![array count])
return;
int max = [self maxNumerFrom:array];
self.maxValue = max;
//**Y轴上距离间隔
float lineDistance = (heigh-X_Y_distance-Y_end_distance)/[array count];
//**Y轴描绘的数字间隔
float numberDistance = ((float)max)/[array count];
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[self.axisColor colorWithAlphaComponent:1.0f],NSForegroundColorAttributeName, [UIFont systemFontOfSize:text_size_big_2],NSFontAttributeName,nil];
NSLog(@"lineDistance::%f,numberDistance:%lf,max::%d",lineDistance,numberDistance,max);
for(int i =0;i<=[array count];i++){
NSString *flagS = [NSString stringWithFormat:@"%0.0f",i*numberDistance];
//**如果numberDistance 跨度小于1,则应展示小数部分
if(numberDistance<=1)
flagS = [NSString stringWithFormat:@"%0.1f",i*numberDistance];
if(i==[array count]){
if(i*numberDistance <max)
flagS = [NSString stringWithFormat:@"%d",max];
}
//**左移距离
float toLeft;
if(flagS.length>=4)
toLeft = 48.0f;
else
toLeft = 40.0f;
//**文字绘图位置Y坐标与虚线相比有偏差,所以减去12个像素点,上移。如果最大值为零则除原点外,其他0值不需要绘制。
if(!(!self.maxValue&&i>0))
[flagS drawAtPoint:CGPointMake(zero.x-toLeft, zero.y-lineDistance*i-12) withAttributes:dic];
//**根据描绘文字的位置 画虚线
if(i)
[self drawDashedLineFrom:CGPointMake(zero.x, zero.y-lineDistance*i) to:CGPointMake(zero.x+self.totalSize.width-X_end_distance-X_Y_distance, zero.y-lineDistance*i) context:ref lineColor:self.axisColor];
// NSLog(@"lineDistace::%d,zero.y::%f",lineDistance,zero.y);
}
}
//**绘制X轴上的文字标记
-(void)drawX_AxisDesc:(NSArray *)array origion:(CGPoint)zero width:(CGFloat)width context:(CGContextRef)ref{
if(!array||![array count])
return;
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[self.axisColor colorWithAlphaComponent:1.0f],NSForegroundColorAttributeName, [UIFont systemFontOfSize:text_size_big_2],NSFontAttributeName,nil];
//**X轴上线段间隔
float lineDistance = (width-X_Y_distance-X_end_distance)/[array count];
for(int i =0;i<[array count];i++){
NSString *flagS = [array objectAtIndex:i];
[flagS drawAtPoint:CGPointMake(zero.x+i*lineDistance, zero.y+10) withAttributes:dic];
}
}
//**绘制数据折线图
-(UIImage *)drawBrokenLineAccordingHori:(NSArray *)horiArr Verti:(NSArray *)vertiArr AxisColor:(UIColor *)axColor BrokenColor:(UIColor *)brokColor inBgSize:(CGSize)bgSize{
//**X轴上文字间隔
float X_lineDistance = (self.totalSize.width-X_Y_distance-X_end_distance)/[horiArr count];
//**Y轴上文字间隔
// NSInteger Y_lineDistance = (self.totalSize.height-X_Y_distance)/[vertiArr count];
//**Y轴上单位数字占据的距离
float unitDistance;
//**检测最大值是否为零
if(self.maxValue)
unitDistance = (self.totalSize.height-X_Y_distance-Y_end_distance)/self.maxValue;
else
unitDistance = 0.0f;
//NSLog(@"maxValue::%d",self.maxValue);
NSMutableArray *x_Value_Arr = [NSMutableArray arrayWithCapacity:0];
NSMutableArray *y_Value_Arr = [NSMutableArray arrayWithCapacity:0];
for(int i =0;i<[horiArr count];i++){
//**取出对应Y轴上数字
float Y_Value = [[vertiArr objectAtIndex:i]floatValue];
//**根据数字计算出Y轴上对应的位置(距离原点长度)
float Y_Value_Real_Distance = Y_Value *unitDistance;
//**右移动40像素,使点位于日期中心
NSNumber *xNumber = [[NSNumber alloc]initWithFloat:self.origionPoint.x+X_lineDistance*i+40];
NSNumber *yNumber = [[NSNumber alloc]initWithFloat:self.origionPoint.y-Y_Value_Real_Distance];
[x_Value_Arr addObject:xNumber];
[y_Value_Arr addObject:yNumber];
}
// NSLog(@"%@:::%@",x_Value_Arr,y_Value_Arr);
//**画折线,并绘制对应月度业绩数据
[self drawLineWith_X_Arr:x_Value_Arr y_Arr:y_Value_Arr context:self.overallContext lineColor:brokColor descRealValue:vertiArr];
return UIGraphicsGetImageFromCurrentImageContext();
}
//**多点之间画折线
-(void)drawLineWith_X_Arr:(NSArray *)x_Arr y_Arr:(NSArray *)y_Arr context:(CGContextRef)context lineColor:(UIColor *)lineColor descRealValue:(NSArray*)descArr
{
if([x_Arr count]!=[y_Arr count]||!x_Arr|!y_Arr||![x_Arr count]||![y_Arr count])
return;
//**画线 指定背景 两点 和颜色
CGContextSetLineWidth(context, 2.0f);
CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
CGContextMoveToPoint(context, [[x_Arr objectAtIndex:0]floatValue],[[y_Arr objectAtIndex:0]floatValue]);
for(int i =1;i<[x_Arr count];i++){
CGContextAddLineToPoint(context,[[x_Arr objectAtIndex:i]floatValue],[[y_Arr objectAtIndex:i]floatValue]);
NSLog(@"x:%f---y:%f",[[x_Arr objectAtIndex:i]floatValue],[[y_Arr objectAtIndex:i]floatValue]);
}
// CGContextAddLineToPoint(context,100,12);
CGContextStrokePath(context);
CGContextClosePath(context);
//**在节点处圆点
[self drawCircleWith_X_Arr:x_Arr y_Arr:y_Arr context:context lineColor:lineColor];
//**在节点附近绘制业绩数据
[self drawAchievementValueWith_X_Arr:x_Arr y_Arr:y_Arr descRealValue:descArr context:context];
}
//**根据坐标画圆
-(void)drawCircleWith_X_Arr:(NSArray *)x_Arr y_Arr:(NSArray *)y_Arr context:(CGContextRef)context lineColor:(UIColor *)lineColor{
if([x_Arr count]!=[y_Arr count]||!x_Arr|!y_Arr||![x_Arr count]||![y_Arr count])
return;
for(int i =0;i<[x_Arr count];i++){
//**外部圆圈为指定颜色
CGContextAddArc(context, [[x_Arr objectAtIndex:i]floatValue], [[y_Arr objectAtIndex:i]floatValue], 8.0f, 0, 6.3, 0);
CGContextSetFillColorWithColor(context, lineColor.CGColor);
CGContextFillPath(context);
//**内部圆圈颜色为白色
CGContextAddArc(context, [[x_Arr objectAtIndex:i]floatValue], [[y_Arr objectAtIndex:i]floatValue], 6.0f, 0, 6.3, 0);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillPath(context);
}
}
最后
以上就是瘦瘦菠萝为你收集整理的绘制坐标图,折线图的全部内容,希望文章能够帮你解决绘制坐标图,折线图所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复