IOS使用Core Graphics繪制折線圖

先上效果圖

折線圖

好了命爬,上代碼

- (void)drawRect:(CGRect)rect{ ? ?

//step1.繪制帶圓角底圖

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10, 10)]; ? ?

[path addClip];

//step2.繪制漸變底色

CGContextRef context = UIGraphicsGetCurrentContext(); ? ?

CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();

CGFloat locations[2]={0,1.0};//漸變域

NSMutableArray *colors = [NSMutableArray arrayWithCapacity:2];

[colors addObject:(id)[UIColor colorWithRed:250/255.0 green:233/255.0 blue:222/255.0 alpha:1].CGColor];

?[colors addObject:(id)[UIColor colorWithRed:252/255.0 green:79/255.0 blue:8/255.0 alpha:1].CGColor];

CGGradientRef gradient = CGGradientCreateWithColors(space, (CFArrayRef)colors, locations);

CGPoint startPoint = CGPointZero;

CGPoint endPoint = CGPointMake(0, self.bounds.size.height);

CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);

//tips:如果折線圖需要多次刷新,最好把這些固定參數(shù)在初始化時就賦值好啦鸣,在drwaRect內(nèi)新建過多變量在高刷新頻率下會影響性能

//step3.繪制白色折線段

?[[UIColor whiteColor]setFill];

?[[UIColor whiteColor]setStroke];

for (int i = 0; i < _elevationPoints.count; i++) {

?????if (i == 0) {

? ???????[graphPath moveToPoint:CGPointMake([self getViewPointXWithCoordHeight:????????????????[_distancPoints[i] doubleValue]],[self getViewPointYWithCoordHeight:[_elevationPoints[i] doubleValue]])];//這里為獲取每個點坐標(biāo)的自定義方法颗胡,可忽略

? ? }else{

????????[graphPath addLineToPoint:CGPointMake([self getViewPointXWithCoordHeight:[_distancPoints[i] doubleValue]], [self getViewPointYWithCoordHeight:????????[_elevationPoints[i] doubleValue]])];

????}

graphPath.lineWidth = 2.0; ? ?

?[graphPath stroke];

//note:屏幕坐標(biāo)和drawrect內(nèi)的Y坐標(biāo)不一樣 一個從屏幕上方到下方遞增舶担,一個剛好相反惭婿,注意做好坐標(biāo)轉(zhuǎn)換

//step4:繪制折線下面的白色透明漸變色

CGContextSaveGState(context);//儲存當(dāng)前繪制狀態(tài)

UIBezierPath *clippingPath = [graphPath copy];//復(fù)制折線段

[clippingPath addLineToPoint:CGPointMake([self getViewPointXWithCoordHeight:[_distancPoints[_distancPoints.count -1] doubleValue]],rect.size.height - BottomBorder)];

[clippingPath addLineToPoint:CGPointMake([self getViewPointXWithCoordHeight:[_distancPoints[0] doubleValue]],rect.size.height - BottomBorder)];

[clippingPath closePath]; //將折線段添加兩條垂直線(一條垂直X軸,一條垂直Y軸)并閉合歌憨,形成封閉路徑

[clippingPath addClip];//獲取封閉路徑為編輯區(qū)域

CGContextDrawLinearGradient(context, gradient, CGPointMake(Margin, BottomBorder), CGPointMake(Margin, rect.size.height ),0);//繪制漸變色

CGContextRestoreGState(context);//恢復(fù)至儲存位置的繪制狀態(tài)(既編輯區(qū)域又返回為整個圓角rect着憨,F(xiàn)illColor和StrokeColor返回為白色)

//step5:繪制折點

for (int i = 0; i < _elevationPoints.count; i++) {

? ??double pointY = [self getViewPointYWithCoordHeight:[_elevationPoints[i] doubleValue]] - 2.5;

????double pointX = [self getViewPointXWithCoordHeight:[_distancPoints[i] doubleValue]] - 2.5;

????UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(pointX, pointY, 5, 5)];

? ??[circle fill];

}

//step6:繪制坐標(biāo)系白色橫線以及數(shù)字標(biāo)識

?UIBezierPath *linePath = [UIBezierPath bezierPath];

NSMutableParagraphStyle* style = [[NSMutableParagraphStyle alloc] init];

?[style setAlignment:NSTextAlignmentCenter];

for (int i = 0; i < 5; i++) {

????double pointY = [self getViewPointYWithCoordHeight:[_elevationLines[i] integerValue]];

????[linePath moveToPoint:CGPointMake(Margin,pointY)];

????[linePath addLineToPoint:CGPointMake(rect.size.width - Margin, pointY)];

????NSString *height = [NSString stringWithFormat:@"%lu",[_elevationLines[i]integerValue]];

????[height drawInRect:CGRectMake(Margin/2-20, pointY-10, 40, 20) ????withAttributes:@{NSFontAttributeName:[UIFont ????systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor ????whiteColor],NSParagraphStyleAttributeName:style}];

}

[[UIColor colorWithWhite:1.0 alpha:0.8] setStroke];

linePath.lineWidth = 1.0;

[linePath stroke];

//step7:繪制底部橫向數(shù)字和單位

NSString *elevationStr = @"(m)海拔";

[elevationStr drawInRect:CGRectMake(Margin/2-20, TopBorder/2-10, 80, 20) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor whiteColor],NSParagraphStyleAttributeName:style}];

NSString *distanceStr = [NSString stringWithFormat:@"%.02f km",self.totalDistance/1000];

[distanceStr drawInRect:CGRectMake(rect.size.width - Margin - 40, rect.size.height-BottomBorder + 10, 80, 20) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor whiteColor],NSParagraphStyleAttributeName:style}];

NSString *midDistanceStr = [NSString stringWithFormat:@"%.02f",self.totalDistance/2000];

[midDistanceStr drawInRect:CGRectMake(rect.size.width / 2.0 - 40, rect.size.height-BottomBorder + 10, 80, 20) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor whiteColor],NSParagraphStyleAttributeName:style}];

NSString *startDistanceStr = @"0";

?[startDistanceStr drawInRect:CGRectMake(Margin - 40, rect.size.height-BottomBorder+10, 80, 20) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor whiteColor],NSParagraphStyleAttributeName:style}];

}

整體流程就是如此,這里提供的是思路和方法务嫡,具體繪制實現(xiàn)需要讀者自己去計算每個點的位置甲抖、每條線的位置以及單位數(shù)量等

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市植袍,隨后出現(xiàn)的幾起案子惧眠,更是在濱河造成了極大的恐慌,老刑警劉巖于个,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異暮顺,居然都是意外死亡厅篓,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進(jìn)店門捶码,熙熙樓的掌柜王于貴愁眉苦臉地迎上來羽氮,“玉大人,你說我怎么就攤上這事惫恼〉笛海” “怎么了?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長令宿。 經(jīng)常有香客問我叼耙,道長,這世上最難降的妖魔是什么粒没? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任筛婉,我火速辦了婚禮,結(jié)果婚禮上癞松,老公的妹妹穿的比我還像新娘爽撒。我一直安慰自己,他們只是感情好响蓉,可當(dāng)我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布硕勿。 她就那樣靜靜地躺著,像睡著了一般枫甲。 火紅的嫁衣襯著肌膚如雪源武。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天言秸,我揣著相機(jī)與錄音软能,去河邊找鬼。 笑死举畸,一個胖子當(dāng)著我的面吹牛查排,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播抄沮,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼跋核,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了叛买?” 一聲冷哼從身側(cè)響起砂代,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎率挣,沒想到半個月后刻伊,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡椒功,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年捶箱,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片动漾。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡丁屎,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出旱眯,到底是詐尸還是另有隱情晨川,我是刑警寧澤证九,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站共虑,受9級特大地震影響愧怜,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜看蚜,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一叫搁、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧供炎,春花似錦渴逻、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至竭钝,卻和暖如春梨撞,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背香罐。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工卧波, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人庇茫。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓港粱,卻偏偏與公主長得像,于是被迫代替她去往敵國和親旦签。 傳聞我的和親對象是個殘疾皇子查坪,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容