激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - IOS - iOS實現的多條折線圖封裝實例

iOS實現的多條折線圖封裝實例

2021-03-22 16:37大炮打小鳥 IOS

這篇文章主要跟大家分享了關于利用iOS實現多條折線圖的封裝實例,文中給出了詳細的示例代碼供大家參考學習,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。

前言

有時候我們在處理一些數據的時候,需要用到折線圖來呈現數據,讓用戶能夠對數據更加清晰明,本文主要給大家介紹了關于ios實現多條折線圖的相關內容,下面話不多說,來看看詳細的介紹吧。

效果圖如下:

iOS實現的多條折線圖封裝實例

iOS實現的多條折線圖封裝實例

1、封裝

.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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 // 坐標軸與畫布間距
#define y_every_margin 20 // y軸每一個值的間隔數
 
#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

?
1
2
3
4
5
6
7
#import "beziercurveview.h"
 
static cgrect myframe;
 
@interface beziercurveview ()
 
@end
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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;
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
 * 畫坐標軸
 */
-(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];
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
 * 畫多根折線圖
 */
-(void)drawmorelinechartviewwithx_value_names:(nsmutablearray *)x_names targetvalues:(nsmutablearray *)targetvalues linetype:(linetype) linetype{
 
 //1.畫坐標軸
 [self drawxyline:x_names];
 
 for (int j=0; j<targetvalues.count; j++) {
 //2.獲取目標值點坐標
 nsmutablearray *allpoints = [nsmutablearray array];
 for (int i=0; i<[targetvalues[j] count]; i++) {
  cgfloat doublevalue = 2*[targetvalues[j][i] floatvalue]; //目標值放大兩倍
  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.坐標連線
 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、調用

?
1
2
#define screen_w [uiscreen mainscreen].bounds.size.width
#define screen_h [uiscreen mainscreen].bounds.size.height
?
1
2
3
4
5
6
//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 *)@[@"語文",@"數學",@"英語",@"物理",@"化學",@"生物",@"政治",@"歷史",@"地理"] 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];

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://www.jianshu.com/p/ec73329c50b0

延伸 · 閱讀

精彩推薦
  • IOSiOS實現控制屏幕常亮不變暗的方法示例

    iOS實現控制屏幕常亮不變暗的方法示例

    最近在工作中遇到了要將iOS屏幕保持常亮的需求,所以下面這篇文章主要給大家介紹了關于利用iOS如何實現控制屏幕常亮不變暗的方法,文中給出了詳細的...

    隨風13332021-04-02
  • IOSiOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和Masonry簡單使用)

    iOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和

    這篇文章主要介紹了iOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和Masonry簡單使用)的相關資料,需要的朋友可以參考下...

    CodingFire13652021-02-26
  • IOSiOS中MD5加密算法的介紹和使用

    iOS中MD5加密算法的介紹和使用

    MD5加密是最常用的加密方法之一,是從一段字符串中通過相應特征生成一段32位的數字字母混合碼。對輸入信息生成唯一的128位散列值(32個字符)。這篇文...

    LYSNote5432021-02-04
  • IOS詳解iOS中多個網絡請求的同步問題總結

    詳解iOS中多個網絡請求的同步問題總結

    這篇文章主要介紹了詳解iOS中多個網絡請求的同步問題總結,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧...

    liang199111312021-03-15
  • IOSiOS開發之視圖切換

    iOS開發之視圖切換

    在iOS開發中視圖的切換是很頻繁的,獨立的視圖應用在實際開發過程中并不常見,除非你的應用足夠簡單。在iOS開發中常用的視圖切換有三種,今天我們將...

    執著丶執念5282021-01-16
  • IOSiOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果

    iOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果

    這篇文章主要介紹了iOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果的相關資料,需要的朋友可以參考下...

    jiangamh8882021-01-11
  • IOSiOS中UILabel實現長按復制功能實例代碼

    iOS中UILabel實現長按復制功能實例代碼

    在iOS開發過程中,有時候會用到UILabel展示的內容,那么就設計到點擊UILabel復制它上面展示的內容的功能,也就是Label長按復制功能,下面這篇文章主要給大...

    devilx12792021-04-02
  • IOSiOS開發技巧之狀態欄字體顏色的設置方法

    iOS開發技巧之狀態欄字體顏色的設置方法

    有時候我們需要根據不同的背景修改狀態欄字體的顏色,下面這篇文章主要給大家介紹了關于iOS開發技巧之狀態欄字體顏色的設置方法,文中通過示例代碼...

    夢想家-mxj8922021-05-10
主站蜘蛛池模板: 狠狠干五月天 | 羞羞漫画无遮挡观看 | 91精品国产一区二区三区四区在线 | 亚洲午夜在线视频 | 国产91九色 | 91精品欧美一区二区三区 | 欧美成人免费tv在线播放 | 国产精品1区 | 亚洲小视频在线 | 国产一级一级 | 久久精精 | 久久久久久久.comav | 久久精品23 | 在线成人av观看 | 中国大陆高清aⅴ毛片 | 日日噜噜噜噜久久久精品毛片 | 干色视频 | 国产超碰人人爽人人做人人爱 | 黄视频网站免费在线观看 | 情侣啪啪网站 | 蜜桃视频在线免费播放 | 免费的性生活视频 | 国产免费久久久久 | 国产精品久久久久久影视 | 欧美精品38videos性欧美 | 久久国产精品久久精品国产演员表 | 欧美a在线 | 国产午夜精品一区二区三区四区 | 特一级毛片 | 永久免费黄色大片 | 羞羞电影网| 91久久一区 | 欧美爱爱视频网站 | 精国产品一区二区三区 | 国产91免费看 | 色99999 | 成人毛片视频在线播放 | 色婷婷综合久久久中文一区二区 | 久久精品久久久久 | 在线看一区二区三区 | www.精品在线|