-(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