iOS drawing, drawing coordinate system, drawing coordinate system

Source: Internet
Author: User
Tags dashed line

Let's take a look at the effect:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>


creates a new view class. To add code directly:

Only override Drawrect:if-perform custom drawing.//an empty implementation adversely affects performance during a        nimation.-(void) DrawRect: (cgrect) rect{//Get current environment cgcontextref context = Uigraphicsgetcurrentcontext ();        Save the current environment to facilitate later recovery of cgcontextsavegstate (context);         Organize the data for (int i = 0; i < Self.dataArray.count; i++) {NSString * x_data = self.dataarray[i][@ "xxx"];        NSString * y_data = self.dataarray[i][@ "yyyy"];                NSString * rate_data = self.dataarray[i][@ "RRR"];        [X_array Addobject:x_data];        [Y_array Addobject:y_data];    [Rate_array Addobject:rate_data];    }//Rectangular drawing area cgrect Rectangle = rect;    Defines a rectangular path uibezierpath *path = [Uibezierpath bezierpathwithrect:rectangle];        Draw the rectangle path [path stroke];                    Public data float fdistance_left_frame = 30.0; The width of the left side x-axis distance border. Float Fdistance_botto for drawing textM_frame = 15.0;                   Left side y-axis distance border Width float fdistance_right_frame = 10.0;  Left x axis distance border width float fdraw_line_height = rect.size.height-fdistance_bottom_frame; Height of drawing coordinates float fdraw_line_width = Rect.size.width-fdistance_left_frame-fdistance_r  Ight_frame;                    Draw the width of the coordinate float f_x_axis_scale_number = 7.0;                    X-Axis Large tick count float f_y_axis_scale_number = 7.0;                    Y-axis number of ticks float x_unit_distance_scale = 0.0;                    Offset of the x-axis scale float Y_unit_distance_scale = 0.0;                             Offset of the y-axis scale float X_unit_scale = 0.0;                             The span of the x-axis scale (a proportional unit) float Y_unit_scale = 0.0;    The span of the y-axis scale (a proportional unit)//start drawing x-axis Float left_bottom_x = rect.origin.x + fdistance_left_frame;    Float left_bottom_y = rect.origin.y + fdraw_line_height;              Cgpoint Point_origin = Cgpointmake (left_bottom_x, left_bottom_y); Axis Origin//define a start path Uibezierpath * X_startpath = [Uibezierpath Bezierpath];        [X_startpath setlinewidth:1.5];                                        [X_startpath Movetopoint:point_origin]; Set start point (coordinate origin) for (int x = 0; x < F_x_axis_scale_number; + +)//Draw straight line {X_un                    It_scale = Fdraw_line_height/f_x_axis_scale_number;                                  One-level division large scale X_unit_distance_scale = x * X_unit_scale;                The offset point of the relative Origin [X_startpath addlinetopoint:cgpointmake (left_bottom_x, Left_bottom_y-x_unit_distance_scale)]; |        The left side of the x-axis draws text float text_height_certer = left_bottom_y;        float text_rect_top = text_height_certer-8-X_unit_distance_scale;    Float Text_rect_bottom = text_height_certer + 8-x_unit_distance_scale; +8-8.        Give the text a height of 16 pixels float text_rect_height = 16;             CGRect x_axis_rect = CGRectMake (2, Text_rect_top, Fdistance_left_frame, text_rect_height);   Cgcontextsetlinewidth (context, 1.0);        Cgcontextsetrgbfillcolor (context, 0.5, 0.5, 0.5, 0.5);                         Uifont *font = [Uifont boldsystemfontofsize:12.0];             Font with 12th number NSString * X_strtext = [NSString stringwithformat:@ "%zi.00", X];                Draw x-axis scale values [X_strtext drawinrect:x_axis_rect Withfont:font]; if (0 = = x) {//IS ' 0 '. No or that draws the scale.            Draw the horizontal line "Y" axis float y_width = Fdraw_line_width directly at the bottom; Cgcontextsetrgbstrokecolor (context, 0.5, 0.5, 0.5, 0.5);//Line Color Cgcontextmovetopoint (context, left_bottom_x, lef            T_bottom_y-x_unit_distance_scale);            Cgcontextaddlinetopoint (context, left_bottom_x + y_width, Left_bottom_y-x_unit_distance_scale);                        Cgcontextstrokepath (context);            Start drawing y-Axis Uibezierpath * Y_startpath = [Uibezierpath Bezierpath];            [Y_startpath setlinewidth:1.5];                 [Y_startpath Movetopoint:point_origin]; The starting point of the y-axis is alsois the x-axis tick start point for (int y = 0; y < F_y_axis_scale_number + 1; y++)//Draw straight line {               Y_unit_scale = Fdraw_line_width/f_y_axis_scale_number;                            One-level division large scale Y_unit_distance_scale = y * y_unit_scale;                                 The offset point of the relative Origin [Y_startpath addlinetopoint:cgpointmake (left_bottom_x, Left_bottom_y-x_unit_distance_scale)];                "-" Y-axis lower drawing text float y_text_left_certer = left_bottom_x;                Float Y_text_rect_left = y_text_left_certer-15 + Y_unit_distance_scale;                Float Y_text_rect_top = left_bottom_y + 2;                Float Y_text_rect_width = y_text_left_certer + + Y_unit_distance_scale;                                +10-10, gives the text 20 pixels width of float y_text_rect_height = 16;               CGRect y_axis_rect = CGRectMake (Y_text_rect_left, Y_text_rect_top, Y_text_rect_width, y_text_rect_height);                 Cgcontextsetlinewidth (context, 1.5);                Line width cgcontextsetrgbfillcolor (context, 0.5, 0.5, 0.5, 0.5);              Uifont *font = [Uifont boldsystemfontofsize:12.0];                                Font with 12th number//NSString * Y_strtext = [NSString stringwithformat:@ "%zi.00", y];//draw y-axis scale values                NSString * Y_strtext = [Y_array objectatindex:f_y_axis_scale_number-y];                    Y_strtext = [Y_strtext substringfromindex:5];                                Draw y-axis scale values [Y_strtext drawinrect:y_axis_rect Withfont:font]; if (y = = 0) {} else {//"-" Y-axis Upper draw scale short line float FSCA                    Le_width = 5.0;      Cgcontextsetrgbstrokecolor (context, 0.5, 0.5, 0.5, 0.5);                    Line Color Cgcontextmovetopoint (context, left_bottom_x + Y_unit_distance_scale, left_bottom_y); CgcontextaddlineTopoint (context, left_bottom_x + Y_unit_distance_scale, left_bottom_y-fscale_width);                Cgcontextstrokepath (context);   }} [Y_startpath stroke]; Draws line based on coordinate point} else {//"|"            The x-axis draws the right-hand scale short float fscale_width = 5.0; Cgcontextsetrgbstrokecolor (context, 0.5, 0.5, 0.5, 0.5);//Line Color Cgcontextmovetopoint (context, left_bottom_x, le            Ft_bottom_y-x_unit_distance_scale);            Cgcontextaddlinetopoint (context, left_bottom_x + fscale_width, Left_bottom_y-x_unit_distance_scale);        Cgcontextstrokepath (context); }////Draw level two small scale value//for (int xx = 0; xx < 5; xx++)//{//float fsmall_scale_width = 3.0;/ /Cgcontextsetrgbstrokecolor (context, 0.5, 0.5, 0.5, 0.5);//Line Color//cgcontextsetlinewidth (context, 1                    .0); Line width//float fsmall_scale_height = x_unit_distance_scale/10.0; The height of each small scaleInvariant//Cgcontextmovetopoint (context, point_origin.x, point_origin.y-fsmall_scale_height);//Cgcontex Taddlinetopoint (context, point_origin.x + fsmall_scale_width, point_origin.y-fsmall_scale_height);//CGContex   Tstrokepath (context);//}} [X_startpath stroke];    Draws line according to the coordinate point connection [[Uicolor Bluecolor] setfill];            [X_startpath fill]; // "|" The x-axis draws a dashed line.    Horizontal dashed cgfloat dasharray[] = {2.0, 2.0};    Cgcontextsetlinedash (context, 0, Dasharray, 2);    Cgcontextsetrgbstrokecolor (context,0.5, 0.5, 0.5, 0.5);//Line color for (int x = 1; x < F_x_axis_scale_number + 1; + +)        Draw a dashed line {X_unit_distance_scale = x * (X_unit_scale);        One-level division large scale Cgcontextmovetopoint (context, left_bottom_x + 5, Left_bottom_y-x_unit_distance_scale);    Cgcontextaddlinetopoint (context, left_bottom_x + fdraw_line_width, Left_bottom_y-x_unit_distance_scale); } for (int y = 1; y < F_y_axis_scale_number + 1; y++)//Draw dashed line   {Y_unit_distance_scale = y * (y_unit_scale);        One-level division large scale Cgcontextmovetopoint (context, point_origin.x + Y_unit_distance_scale, point_origin.y-5); Cgcontextaddlinetopoint (context, point_origin.x + Y_unit_distance_scale, Point_origin.y-fdraw_line_height +    Fdistance_bottom_frame + 3);           } cgcontextstrokepath (context);            Start drawing the graph Cgcontextsetlinedash (context, 0.0,null, 0);                    Restore Brush Cgcontextsetlinewidth (context,1.0);   Set to Solid line brush Cgcontextsetrgbstrokecolor (context, 1.0, 0, 0, 0.5); Line color for (int a = 0; a < X_array.count; a++) {//y-axis date backwards traversal, here data also backwards traverse float fdata = [[X_array obj        Ectatindex:x_array.count-1-A] floatvalue];                                 Cgpoint data_point = cgpointmake (point_origin.x + A * y_unit_scale, point_origin.y-fdata * x_unit_scale); Axis Origin if (0 = = a) {Cgcontextmovetopoint (context, Data_point.x, Data_point.        y); }        else {cgcontextaddlinetopoint (context, data_point.x, DATA_POINT.Y);            } NSLog (@ "%zi = = (%f,%f)", A, data_point.x, DATA_POINT.Y);        } cgcontextstrokepath (context); Start Border dot Cgcontextsetrgbstrokecolor (context, 1.0, 0.0, 0.0, 1.0);//Brush Line Color cgcontextsetlinewidth (context, 2.0);//width of line Degrees//void Cgcontextaddarc (cgcontextref c,cgfloat x, cgfloat y,cgfloat radius,cgfloat startangle,cgfloat endAngle, int cl ockwise) 1 radians =180°/π (≈57.3°) degrees = radians x180°/π360°=360xπ/180 =2π radians//x, y for dot coordinates, radius radius, startangle for start radians, endangle for end radians, cl        Ockwise 0 is clockwise and 1 is counterclockwise.  for (int a = 0; a < X_array.count; a++) {//y-axis date backwards traversal, here the data is also backwards traversing float fdata = [[X_array objectatindex:        X_array.count-1-A] floatvalue];                                 Cgpoint data_point = cgpointmake (point_origin.x + A * y_unit_scale, point_origin.y-fdata * x_unit_scale); Axis Origin Cgcontextaddarc (context, Data_point.x, Data_point.y, 1, 0, 2 * PI, 0); Join a circle} cgcontextdrawpath (context, kcgpathstroke); Draw path}bible 2015-07-15 17:24:10//file header Add macro definition # define PI 3.1415926

Source code Download: http://download.csdn.net/detail/bible521125/8902893

C + + to iOS soon used a bit of a mess, such as need to be able to organize themselves.

iOS drawing, drawing coordinate system, drawing coordinate system

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.