iOS uses Cgcontextref to draw a line chart dynamically

Source: Internet
Author: User

-(void) DrawRect: (cgrect) Rect {cgcontextref context = Uigraphicsgetcurrentcontext ();    Cgcontextclearrect (context, rect);                                 Cgcontextsetlinewidth (context, _linewidth);  Set Brush width cgcontextsetfillcolorwithcolor (context, [[self backgroundcolor] cgcolor]);                                           Sets the background fill color cgcontextfillrect (context, rect);              Fill background cgcontextsetstrokecolorwithcolor (context, _linecolor.cgcolor);            Sets the brush color//arr for real-time obtained Dynamic Data array for (int i = 0; i < _arr.count; i + +) {if (I < _arr.count-1) {            Cgpoint pp[2];            [_arr[i] getbytes:&pp[0] length:sizeof (cgpoint)];                        [_arr[i+1] getbytes:&pp[1] length:sizeof (cgpoint)];            Cgcontextmovetopoint (context, pp[0].x, pp[0].y);            Cgcontextaddlinetopoint (context, pp[1].x, pp[1].y);        Cgcontextstrokepath (context); }}}-(void) Adddatatoarr: (cgfloat) Data {if (!_arr) _arR = [[Nsmutablearray alloc] init];    Cgpoint Point; Max_x_count represents the maximum number displayed on the x-axis, moving the x-axis to the left, moving one unit at a time (_arr.count >= max_x_count) {//moving x-axis CGRect frame =        Self.frame;        Frame.origin.x-= (Self.frame.size.width/max_x_count);        Frame.size.width + = (self.frame.size.width/max_x_count);    Self.frame = frame;        } point.x = _arr.count * (self.frame.size.width/max_x_count); If the current value is greater than the maximum value represented by the y-axis, expand the y-axis maximum to twice times the current value if (Data > _maxypower) {//change the maximum value of y to currentpower _maxypower = data    * 2;        } point.y = data; Depositing data in real time into _arr [_arr addobject:[nsdata datawithbytes:&point length:sizeof (Cgpoint)]];}
Just call Adddatatoarr dynamically: Then call [Self.view Setneeddisplay] to

iOS uses Cgcontextref to draw a line chart dynamically

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.