Ios drawing, drawing coordinate system, drawing Coordinate System

Source: Internet
Author: User

Ios drawing, drawing coordinate system, drawing Coordinate System
Let's take a look at the effect:

 

Create a View class and add the Code directly:

 

// Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. -(void) drawRect :( CGRect) rect {// obtain the current environment CGContextRef context = UIGraphicsGetCurrentContext (); // Save the current environment to restore CGContextSaveGState (context) in the future ); // organize the data for (int I = 0; I <self. dataArray. count; I ++) {NSString * x_data = self. dataArray [I] [@ xxx]; NSString * y_data = sel F. 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];} // CGRect Rectangle = rect; // define a rectangular path UIBezierPath * path = [UIBezierPath bezierPathWithRect: Rectangle]; // draw a rectangular path [path stroke]; //////////////////////////////////////// /////////////// // public data float fdistance_left_fr Ame = 30.0; // The width of the border from the left X axis, used to draw the text float fdistance_bottom_frame = 15.0; // float fdistance_right_frame = 10.0 from the border on the left Y axis; // float fdraw_line_height = rect. size. height-fdistance_bottom_frame; // draw the height of the coordinate float fdraw_line_width = rect. size. width-fdistance_left_frame-fdistance_right_frame; // width of the drawn coordinate float f_x_axis_scale_number = 7.0; // float f_y_axis_scale_number = 7.0 on the X axis ;// Y axis scale 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; // span of the X axis Scale (one Proportional unit) float y_unit_scale = 0.0; // span of the Y axis Scale (one Proportional unit) // start to draw the 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); // coordinate axis origin // define a start Path UIBezierPath * x_startPath = [UIBezierPath bezierPath]; [x_startPath setLineWidth: 1.5]; [x_startPath moveToPoint: point_origin]; // set the starting point (coordinate origin) for (int x = 0; x <f_x_axis_scale_number; x ++) // draw a straight line {x_unit_scale = fdraw_line_height/f_x_axis_scale_number; // you can use the first-level equi-score to scale x_unit_distance_scale = x * x_unit_scale; // offset point relative to the origin [x_startPath addLineToPoint: CGPointMake (left_bottom_x, left_bottom_y-x_unit _ Distance_scale)]; // "|" float margin = left_bottom_y; float text_rect_top = middle-8-x_unit_distance_scale; float text_rect_bottom = Lower + 8-percent; // + 8-8, float text_rect_height = 16; CGRect margin = CGRectMake (2, text_rect_top, bottom, text_rect_height); CGContextSetLineWidth (context, 1.0 ); CG ContextSetRGBFillColor (context, 0.5, 0.5, 0.5, 0.5); UIFont * font = [UIFont boldSystemFontOfSize: 12.0]; // use NSString * x_strtext = [NSString stringWithFormat: @ % zi.00, x]; // draw the x axis scale value [x_strtext drawInRect: x_axis_rect withFont: font]; if (0 = x) {// when it is "0, no or the painting 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); // The line color CGContextMoveToPoint (Context, left_bottom_x, left_bottom_y-rows); CGContextAddLineToPoint (context, left_bottom_x + y_width, left_bottom_y-rows); CGContextStrokePath (context ); // start to draw the Y axis UIBezierPath * y_startPath = [UIBezierPath bezierPath]; [y_startPath setLineWidth: 1.5]; [y_startPath moveToPoint: point_origin]; // the start point of the y axis is also the scale start point of the X axis. for (int y = 0; y <f_y_axis_scale_number + 1; y ++) // Draw a straight line {y_unit_scale = fdraw_line_width/border; // The first-level alignment scale scaled = y * y_unit_scale; // The offset point relative to the origin [y_startPath addLineToPoint: CGPointMake, left_bottom_y-x_unit_distance_scale)]; // draw the text float bottom of the "-" Y axis = left_bottom_x; float rows = middle-15 + rows; float rows = left_bottom_y + 2; Float rows = rows + 15 + rows; // + 10-10, float y_text_rect_height = 16; CGRect y_axis_rect = CGRectMake (rows, rows, y_text_rect_width, y_text_rect_height); CGContextSetLineWidth (context, 1.5); // The line width CGContextSetRGBFillColor (context, 0.5, 0.5, 0.5); UIFont * font = [UIFont boldSystemFontOfSize: 0.5]; // The font number is 12 // NSString * y_strtext = [NSString stringWithFormat: @ % zi.00, y]; // draw the y axis scale value NSString * y_strtext = [y_array objectAtIndex: required-y]; y_strtext = [y_strtext substringFromIndex: 5]; // draw the y axis scale value [y_strtext drawInRect: y_axis_rect withFont: font]; if (y = 0) {} else {// upper part of the "-" Y axis, draw the scale float fscale_width = 5.0; CGContextSetRGBStrokeColor (context, 0.5, 0.5, 0.5, 0.5); // The line color CGContextMoveToP Oint (context, callback + callback, left_bottom_y); CGContextAddLineToPoint (context, left_bottom_x + callback, callback-fscale_width); CGContextStrokePath (context);} [y_startPath stroke]; // Draws line Based on coordinate points} else {// | draw float fscale_width = 5.0; CGContextSetRGBStrokeColor (context, 0.5, 0.5, 0.5, 0.5 ); // line color CGContextMoveToPoint (context, left _ Bottom_x, left_bottom_y-points); CGContextAddLineToPoint (context, left_bottom_x + fscale_width, left_bottom_y-x_unit_distance_scale); CGContextStrokePath (context );} /// draw the second-level 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_sca Le_height = hour/10.0; // The height of each small scale remains unchanged // CGContextMoveToPoint (context, point_origin.x, point_origin.y-hour); // CGContextAddLineToPoint (context, point_origin.x + hour, point_origin.y-fsmall_scale_height); // CGContextStrokePath (context); //} [x_startPath stroke]; // Draws line is connected by coordinate point [[UIColor blueColor] setFill]; [x_startPath fill]; // | draw a dotted line on the X axis, with a horizontal dotted line Line 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; x ++) // draw the dotted line {x_unit_distance_scale = x * (x_unit_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 <strong + 1; y ++) // draw the dotted line {y_unit_distance_scale = y * (y_unit_scale ); // CGContextMoveToPoint (context, point_origin.x + hour, point_origin.y-5); CGContextAddLineToPoint (context, point_origin.x + hour, point_origin.y-fdraw_line_height + hour + 3);} CGContextStrokePath (context); // start to draw the graph CGContextSetLineDash (context, 0.0, NULL, 0); // restore the paint brush CGContextSetLineWidth (context, 1.0 ); // set it to the solid-line paint brush CGContextSetRGBStrokeColor (context, 1.0, 0, 0, 0.5); // line color for (int a = 0; a <x_array.count; a ++) {// the Y axis dates are traversed backwards, and here the data is also traversed float fdata = [[x_array objectAtIndex: x_array.count-1-a] floatValue]; CGPoint data_point = CGPointMake (point_origin.x + * Y_unit_scale, point_origin.y-fdata * x_unit_scale); // if (0 = a) {CGContextMoveToPoint (context, data_point.x, data_point.y);} else {context (context, data_point.x, data_point.y);} NSLog (@ % zi = (% f, % f), a, data_point.x, data_point.y);} CGContextStrokePath (context ); // start the border dot CGContextSetRGBStrokeColor (context, 1.0, 0.0, 0.0, 1.0); // The color CGContextSetLineWidth (conte Xt, 2.0); // line width // void CGContextAddArc (CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise) 1 radian = 180 °/π (≈ 57.3 °) degrees = radians × 180 °/π 360 ° = 360 × π/180 = 2 π radians // x, y is the coordinate of the dot, radius, startAngle is the start radian, endAngle is the end radian, clockwise 0 is clockwise, and 1 is counter-clockwise. For (int a = 0; a <x_array.count; a ++) {// the y-axis date is traversed backwards. Here the data is also traversed float fdata = [[x_array objectAtIndex: x_array.count-1-a] floatValue]; CGPoint data_point = CGPointMake (cost + a * y_unit_scale, cost-fdata * x_unit_scale); // coordinate axis origin CGContextAddArc (context, data_point.x, data_point.y, 1, 0, 2 * PI, 0); // Add a circle} CGContextDrawPath (context, kCGPathStroke); // draw path} Bible 17:24:10 // Add macro definition to the file header # define PI 3.1415926

 

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.