最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS實現(xiàn)的多條折線圖封裝實例

 更新時間:2017年07月04日 16:39:42   作者:大炮打小鳥  
這篇文章主要跟大家分享了關(guān)于利用iOS實現(xiàn)多條折線圖的封裝實例,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。

前言

有時候我們在處理一些數(shù)據(jù)的時候,需要用到折線圖來呈現(xiàn)數(shù)據(jù),讓用戶能夠?qū)?shù)據(jù)更加清晰明,本文主要給大家介紹了關(guān)于iOS實現(xiàn)多條折線圖的相關(guān)內(nèi)容,下面話不多說,來看看詳細(xì)的介紹吧。

效果圖如下:

1、封裝類

.h

#define XYQColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define XYQRandomColor XYQColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
#define MARGIN  30 // 坐標(biāo)軸與畫布間距
#define Y_EVERY_MARGIN 20 // y軸每一個值的間隔數(shù)

#import <UIKit/UIKit.h>
// 線條類型
typedef NS_ENUM(NSInteger, LineType) {
 LineType_Straight, // 折線
 LineType_Curve // 曲線
};
@interface BezierCurveView : UIView

//初始化畫布
+(instancetype)initWithFrame:(CGRect)frame;
//畫多根折線圖
-(void)drawMoreLineChartViewWithX_Value_Names:(NSMutableArray *)x_names TargetValues:(NSMutableArray *)targetValues LineType:(LineType) lineType;
@end

.m

#import "BezierCurveView.h"

static CGRect myFrame;

@interface BezierCurveView ()

@end
@implementation BezierCurveView

//初始化畫布
+(instancetype)initWithFrame:(CGRect)frame{

 BezierCurveView *bezierCurveView = [[BezierCurveView alloc]init]; 
 bezierCurveView.frame = frame;

 //背景視圖
 UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
 backView.backgroundColor = [UIColor clearColor];
 [bezierCurveView addSubview:backView];

 myFrame = frame;
 return bezierCurveView;
}
/**
 * 畫坐標(biāo)軸
 */
-(void)drawXYLine:(NSMutableArray *)x_names{

 UIBezierPath *path = [UIBezierPath bezierPath];

 //1.Y軸、X軸的直線
 [path moveToPoint:CGPointMake(MARGIN, CGRectGetHeight(myFrame)-MARGIN)];
 [path addLineToPoint:CGPointMake(MARGIN, MARGIN)];

 [path moveToPoint:CGPointMake(MARGIN, CGRectGetHeight(myFrame)-MARGIN)];
 [path addLineToPoint:CGPointMake(CGRectGetWidth(myFrame), CGRectGetHeight(myFrame)-MARGIN)];

// //2.添加箭頭
// [path moveToPoint:CGPointMake(MARGIN, MARGIN)];
// [path addLineToPoint:CGPointMake(MARGIN-5, MARGIN+5)];
// [path moveToPoint:CGPointMake(MARGIN, MARGIN)];
// [path addLineToPoint:CGPointMake(MARGIN+5, MARGIN+5)];
// 
// [path moveToPoint:CGPointMake(CGRectGetWidth(myFrame), CGRectGetHeight(myFrame)-MARGIN)];
// [path addLineToPoint:CGPointMake(CGRectGetWidth(myFrame)-5, CGRectGetHeight(myFrame)-MARGIN-5)];
// [path moveToPoint:CGPointMake(CGRectGetWidth(myFrame), CGRectGetHeight(myFrame)-MARGIN)];
// [path addLineToPoint:CGPointMake(CGRectGetWidth(myFrame)-5, CGRectGetHeight(myFrame)-MARGIN+5)];

 //3.添加索引格
 //X軸
 for (int i=0; i<x_names.count; i++) {
 CGFloat X = MARGIN + (CGRectGetWidth(myFrame)-30)/x_names.count*(i+1)-(CGRectGetWidth(myFrame)-30)/x_names.count/2.0;
 CGPoint point = CGPointMake(X,CGRectGetHeight(myFrame)-MARGIN);
 [path moveToPoint:point];
 [path addLineToPoint:CGPointMake(point.x, point.y-3)];
 }
 //Y軸(實際長度為200,此處比例縮小一倍使用)
 for (int i=0; i<11; i++) {
 CGFloat Y = CGRectGetHeight(myFrame)-MARGIN-Y_EVERY_MARGIN*i;
 CGPoint point = CGPointMake(MARGIN,Y);
 [path moveToPoint:point];
 [path addLineToPoint:CGPointMake(point.x+3, point.y)];
 }

 //4.添加索引格文字
 //X軸
 for (int i=0; i<x_names.count; i++) {
 CGFloat X = MARGIN + (CGRectGetWidth(myFrame)-30)/x_names.count/2.0 + (CGRectGetWidth(myFrame)-30)/x_names.count*i-(CGRectGetWidth(myFrame)-30)/x_names.count/2.0;
 UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(X, CGRectGetHeight(myFrame)-MARGIN, (CGRectGetWidth(myFrame)-60)/x_names.count, 20)];
 textLabel.text = x_names[i];
 textLabel.font = [UIFont systemFontOfSize:10];
 textLabel.textAlignment = NSTextAlignmentCenter;
 textLabel.textColor = [UIColor blueColor];
 [self addSubview:textLabel];
 }
 //Y軸
 for (int i=0; i<11; i++) {
 CGFloat Y = CGRectGetHeight(myFrame)-MARGIN-Y_EVERY_MARGIN*i;
 UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, Y-5, MARGIN, 10)];
 textLabel.text = [NSString stringWithFormat:@"%d",10*i];
 textLabel.font = [UIFont systemFontOfSize:10];
 textLabel.textAlignment = NSTextAlignmentCenter;
 textLabel.textColor = [UIColor redColor];
 [self addSubview:textLabel];
 }
 //5.渲染路徑
 CAShapeLayer *shapeLayer = [CAShapeLayer layer];
 shapeLayer.path = path.CGPath;
 shapeLayer.strokeColor = [UIColor blackColor].CGColor;
 shapeLayer.fillColor = [UIColor clearColor].CGColor;
 shapeLayer.borderWidth = 2.0;
 [self.subviews[0].layer addSublayer:shapeLayer];
}
/**
 * 畫多根折線圖
 */
-(void)drawMoreLineChartViewWithX_Value_Names:(NSMutableArray *)x_names TargetValues:(NSMutableArray *)targetValues LineType:(LineType) lineType{

 //1.畫坐標(biāo)軸
 [self drawXYLine:x_names];

 for (int j=0; j<targetValues.count; j++) {
 //2.獲取目標(biāo)值點坐標(biāo)
 NSMutableArray *allPoints = [NSMutableArray array];
 for (int i=0; i<[targetValues[j] count]; i++) {
  CGFloat doubleValue = 2*[targetValues[j][i] floatValue]; //目標(biāo)值放大兩倍
  CGFloat X = MARGIN + (CGRectGetWidth(myFrame)-30)/x_names.count*(i+1)-(CGRectGetWidth(myFrame)-30)/x_names.count/2.0;
  CGFloat Y = CGRectGetHeight(myFrame)-MARGIN-doubleValue;
  CGPoint point = CGPointMake(X,Y);
  UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(point.x-1, point.y-1, 2.5, 2.5) cornerRadius:2.5];
  CAShapeLayer *layer = [CAShapeLayer layer];
  layer.strokeColor = [UIColor purpleColor].CGColor;
  layer.fillColor = [UIColor purpleColor].CGColor;
  layer.path = path.CGPath;
  [self.subviews[0].layer addSublayer:layer];
  [allPoints addObject:[NSValue valueWithCGPoint:point]];
 }

 //3.坐標(biāo)連線
 UIBezierPath *path = [UIBezierPath bezierPath];
 [path moveToPoint:[allPoints[0] CGPointValue]];

 CGPoint PrePonit;
 switch (lineType) {
  case LineType_Straight: //直線
  for (int i =1; i<allPoints.count; i++) {
   CGPoint point = [allPoints[i] CGPointValue];
   [path addLineToPoint:point];
  }
  break;
  case LineType_Curve: //曲線
  for (int i =0; i<allPoints.count; i++) {
   if (i==0) {
   PrePonit = [allPoints[0] CGPointValue];
   }else{
   CGPoint NowPoint = [allPoints[i] CGPointValue];
   [path addCurveToPoint:NowPoint controlPoint1:CGPointMake((PrePonit.x+NowPoint.x)/2, PrePonit.y) controlPoint2:CGPointMake((PrePonit.x+NowPoint.x)/2, NowPoint.y)]; //三次曲線
   PrePonit = NowPoint;
   }
  }
  break;
 }

 CAShapeLayer *shapeLayer = [CAShapeLayer layer];
 shapeLayer.path = path.CGPath;
 shapeLayer.strokeColor = XYQRandomColor.CGColor;
 shapeLayer.fillColor = [UIColor clearColor].CGColor;
 shapeLayer.borderWidth = 2.0;
 [self.subviews[0].layer addSublayer:shapeLayer];
 }
}

2、調(diào)用

#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height
 //1.初始化
 _bezierView = [BezierCurveView initWithFrame:CGRectMake(30, 30, SCREEN_W-60, 280)];
 _bezierView.center = self.view.center;
 [self.view addSubview:_bezierView];
 // 多根折線圖
 [_bezierView drawMoreLineChartViewWithX_Value_Names:(NSMutableArray *)@[@"語文",@"數(shù)學(xué)",@"英語",@"物理",@"化學(xué)",@"生物",@"政治",@"歷史",@"地理"] TargetValues:(NSMutableArray *)@[@[@60,@20,@50,@30,@90,@30,@100,@70, @20],@[@20,@40,@20,@50,@30,@90,@30,@100,@70],@[@10,@30,@40,@70,@50,@30,@20,@10,@80]] LineType:LineType_Straight];

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • iOS實現(xiàn)雙向滑動條效果

    iOS實現(xiàn)雙向滑動條效果

    這篇文章主要為大家詳細(xì)介紹了iOS實現(xiàn)雙向滑動條效果的相關(guān)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • IOS給圖片添加水?。▋煞N方式)

    IOS給圖片添加水印(兩種方式)

    為了防止自己辛苦做的項目被別人盜走,采取把圖片添加水印,在此表示圖片的獨一無二。加水印不是要在上面添加上幾個Label,而是我們要把字畫到圖片上成為一個整體,下面這篇文章主要介紹IOS給圖片添加水印,有需要的小伙伴可以來參考下
    2015-08-08
  • 最新評論

    文化| 龙泉市| 宽城| 西昌市| 大同县| 宕昌县| 兰考县| 霍林郭勒市| 冀州市| 永靖县| 横山县| 南雄市| 武山县| 金寨县| 舞钢市| 聂拉木县| 金坛市| 大厂| 兴山县| 泰和县| 乐都县| 南召县| 大连市| 巴彦淖尔市| 西安市| 平安县| 新源县| 新竹市| 米脂县| 徐闻县| 永定县| 江阴市| 金溪县| 渝中区| 东港市| 大埔区| 库车县| 保山市| 冷水江市| 双鸭山市| 宝鸡市|