Use ASP. NET to draw line charts and use asp.net to draw line charts.

Source: Internet
Author: User

Use ASP. NET to draw line charts and use asp.net to draw line charts.

Use the. Net drawing class to draw a line chart, generate an image, and display it on the page. The Code is as follows:

1 // <summary> 2 // obtain data 3 // strChartName: Graph name; 4 // yName: ordinate name; 5 // xName: abscissa name; 6 // iyMaxValue: Maximum ordinate value; 7 // dyAveValue: Maximum ordinate value = (maximum ordinate value/scalar 30) 8 // ---- 100 30: 3 9 // ---- 200 30: 1.5; 10 // xdbColumnName: column name of the Data Table value bound to the X coordinate; 11 // ydbColumnName: column name of the data table worthy of the data table bound to the Y coordinate; 12 /// </summary> 13 public void Get_CurveData (string strSql, string strChartName, string yName, string xName, int iyMaxValue, double dy AveValue, string xdbColumnName, string ydbColumnName) 14 {15 try 16 {17 DataSet ds = sqlAccess. readFromDB (strSql); 18 draw (ds. tables [0], strChartName, yName, xName, iyMaxValue, dyAveValue, xdbColumnName, ydbColumnName); 19} 20 catch (Exception exp) 21 {22 Response. write (sqlAccess. predictionmessage); 23} 24} 25 26 public void draw (DataTable dt, string strChartName, string yName, string xName, in T iyMaxValue, double dyAveValue, string xdbColumnName, string ydbColumnName) 27 {28 // number of records retrieved 29 int count = dt. rows. count; 30 // calculate the chart width 31 int wd = 80 + 20 * (count-1); 32 // set the minimum width to 800 33 if (wd <600) wd = 600; 34 // generate Bitmap to image 35 Bitmap img = new Bitmap (wd, 400); 36 // generate a drawing to image 37 Graphics g = Graphics. fromImage (img); 38 // defines the black brush 39 Pen Bp = new Pen (Color. black); 40 // defines the red paint brush 41 Pen Rp = new Pen (Co Lor. red); 42 // defines the silver-gray brush 43 Pen Sp = new Pen (Color. silver); 44 // defines the blue brush 45 Pen Blp = new Pen (Color. blue); 46 // define the title Font 47 Font Bfont = new Font ("Arial", 12, FontStyle. bold); 48 // defines the general Font 49 font = new Font ("Arial", 9 ); 50 // define the Font 51 Font Tfont = new Font ("Arial", 9); 52 // define the abscissa interval, (the best value is the total width-left blank width [required on the left and right])/(number of records-1) 53 int xSpace = (wd-100)/(count-1 ); 54 // define the ordinate interval, which cannot be modified at will. It is related to the height and the number of horizontal lines. The best value is (Drawing 55 int ySpace = 30; 56 // maximum ordinate value and interval value 57 int yMaxValue = iyMaxValue; 58 // draw background color 59g. drawRectangle (new Pen (Color. white, 400), 0, 0, img. width, img. height); 60 // defines the black transition brush 61 LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, img. width, img. height), Color. black, Color. black, 1.2F, true); 62 // defines the blue transition brush 63 LinearGradientBrush Bluebrush = new LinearGradientBrush (new R Ectangle (0, 0, img. width, img. height), Color. blue, Color. blue, 1.2F, true); 64 // draw the title 65g. drawString (strChartName, Bfont, brush, 40, 5); 66 // draw information briefing 67 // string info = "Graph Generation Time:" + DateTime. now. toString (); 68 // g. drawString (info, Tfont, Bluebrush, 40, 25); 69 // draw the image border 70g. drawRectangle (Bp, 0, 0, img. width-1, img. height-1); 71 // draw the vertical axis 72g. drawLine (Bp, 40, 55, 40,360); 73 // draw the right of the X-axis x2 60 74g. drawLine (Bp, 40,360, 60 + xSpace * (count-1), 360); 75 // draw the vertical coordinate title 76g. drawString (yName, Tfont, brush, 5, 40); 77 // draw the abscissa title 78g. drawString (xName, Tfont, brush, 40,385); 79 // draw the vertical coordinate line 80 for (int I = 0; I <count; I ++) 81 {82g. drawLine (Sp, 40 + xSpace * I, 60, 40 + xSpace * I, 360); 83} 84 // draw the timeline coordinate tag 85 for (int I = 0; I <count; I ++) 86 {87 // string st = Convert. toDateTime (dt. row S [I] ["testdate"]). toString ("MM: dd"); 88 // string st = "nth" + dt. rows [I] ["testdate"]. toString () + "Week"; 89 string st = dt. rows [I] [xdbColumnName]. toString (); 90g. drawString (st, font, brush, 30 + xSpace * I, 370); 91} 92 // draw the abscissa line 93 for (int I = 0; I <10; I ++) 94 {95g. drawLine (Sp, 40, 60 + ySpace * I, 40 + xSpace * (count-1), 60 + ySpace * I ); 96 // The value interval of the x-axis is the maximum value divided by the interval 97 int s = yMaxValue-I * (y MaxValue/10); 98 // draw the dispatch axis coordinate tag 99g. drawString (s. toString (), font, brush, 10, 60 + ySpace * I); 100} 101 102 39.6% // process data in the 103 Format string [] strArr = new string [dt. rows. count]; 104 for (int I = 0; I <count; I ++) 105 {106 string strValue = dt. rows [I] [ydbColumnName]. toString (); 107 if (strValue. contains ("%") 108 {109 strArr [I] = strValue. split ('%') [0]; 110} 111 else112 {113 strArr [I] = strValue; 114} 115} 11 6 // 200/30117 // defines the ordinate unit value = the maximum ordinate value/the maximum Scalar Value 118 double yAveValue = dyAveValue; 119 // defines the curve Turning Point 120 Point [] p = new Point [count]; 121 for (int I = 0; I <count; I ++) 122 {123 p [I]. X = 40 + X space * I; 124 p [I]. y= 360-Convert. toInt32 (Convert. toDouble (strArr [I]) * yAveValue); 125} 126 127 // draw a line chart 128 // g. drawLines (Rp, p); 129 // draw a curve 130 // g. drawCurve (Rp, p); 131 // draw a custom tension curve (132 F is the tension value, which is the default value) g. drawCurve (Rp, p, 0.5F); 133 // g. drawLines (Rp, p); 134 // when multiple curves need to be drawn in one worker, multiple point arrays are defined and then drawn. 135 for (int I = 0; I <count; I ++) 136 {137 // draw a sending record of 138 GB. drawString (strArr [I], font, Bluebrush, p [I]. x, p [I]. y-10); 139 // draw the sending Record Point 140g. drawRectangle (Rp, p [I]. x-1, p [I]. y-1, 2, 2 ); 141} 142 143 // ********************* draw the median line // 144 // for (int I = 0; I <count; I ++) 145 // {146 // p [I]. X = 40 + xSpace * I; 147 // p [I]. y= 360-Convert. toInt32 ("50") * yAveValue; 148 //} 149 // for (int I = 0; I <count; I ++) 150 // {151 // draw the number of sending records at 152 // g. drawString ("", font, Bluebrush, p [I]. x, p [I]. y-10); 153 // draw the sending Record Point 154 // g. drawRectangle (Rp, p [I]. x-1, p [I]. y-1, 2, 2); 155 //} 156 // g. drawLine (Blp, 40,360-Convert. toInt32 ("50") * yAveValue, 60 + xSpace * (count-1), 360-Convert. toInt32 ("50") * yAveValue ); 157 // ************************** // 158 159 // Save the drawn figure 160 MemoryStream stream = new MemoryStream (); 161 img. save (stream, ImageFormat. jpeg); 162 // output 163 Response. clear (); 164 Response. contentType = "image/jpeg"; 165 Response. binaryWrite (stream. toArray (); 166} 167}

 

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.