// Use Asp.net to draw a pie chart (which can be used for various voting tasks) Program ) // Compared with ASP, Asp.net has more powerful functions. using GDI +, you can easily implement many graphics Functions that you previously could not do. // First, create the library mess. MDB in c: \ and create the table title. // Create two fields, title (char type) and point (INT type) // Very satisfied 281 // 297 satisfied // Merge 166 // Not satisfied: 416 // I have also written a line chart and a bar chart. I am writing them all into a class. You can contact me if you need: mailto: ouyang76@263.net <% @ Page Language = "C #" %> <% @ Import namespace = "system. Data" %> <% @ Import namespace = "system. Data. oledb" %> <% @ Import namespace = "system. Drawing" %> <% @ Import namespace = "system. Drawing. Imaging" %> <Script language = "C #" runat = "server"> Public void page_load (Object OBJ, eventargs E) { // Specify the connection string as a constant. Const string strconn = "provider = Microsoft. Jet. oledb.4.0;" + "Data Source = c: \ mess. mdb "; Oledbconnection conn = new oledbconnection (strconn ); Conn. open (); String SQL = "select * From Title "; Oledbcommand cmd = new oledbcommand (SQL, Conn ); Dataset DS = new dataset (); Oledbdataadapter adapter1 = new oledbdataadapter (CMD ); Adapter1.fill (DS ); Conn. Close (); Float Total = 0.0f, TMP; Int iloop; For (iloop = 0; iloop <Ds. Tables [0]. Rows. Count; iloop ++) { TMP = convert. tosingle (Ds. Tables [0]. Rows [iloop] ["point"]); // converts it to a single precision, and it is impossible to vote for half a vote. You can also write it as convert. toint32. Total + = TMP; } // Response. Write (convert. tostring (total )); Font fontlegend = new font ("verdana", 9), fonttitle = new font ("verdana", 10, fontstyle. Bold); // set the font // Fonttitle indicates the font of the title. Int width = 230; // white background width Const int bufferspace = 15; Int legendheight = fontlegend. Height * (Ds. Tables [0]. Rows. Count + 1) + bufferspace; Int titleheight = fonttitle. height + bufferspace; Int Height = width + legendheight + titleheight + bufferspace; // the white background height. Int pieheight = width; Rectangle pierect = new rectangle (0, titleheight, width, pieheight ); // Add various random colors Arraylist colors = new arraylist (); Random RND = new random (); For (iloop = 0; iloop <Ds. Tables [0]. Rows. Count; iloop ++) Colors. Add (New solidbrush (color. fromargb (RND. Next (255), RND. Next (255), RND. Next (255 )))); Bitmap objbitmap = new Bitmap (width, height); // create a bitmap instance // Bitmap objbitmap = new Bitmap (230,500); // create a bitmap instance Graphics objgraphics = graphics. fromimage (objbitmap ); Objgraphics. fillrectangle (New solidbrush (color. White), 0, 0, width, height); // draw a white background Objgraphics. fillrectangle (New solidbrush (color. lightyellow), pierect); // draw a bright yellow background // The following is a pie chart (several rows of rows are drawn) Float currentdegree = 0.0f; For (iloop = 0; iloop <Ds. Tables [0]. Rows. Count; iloop ++) { Objgraphics. fillpie (solidbrush) colors [iloop], pierect, currentdegree, Convert. tosingle (Ds. Tables [0]. Rows [iloop] ["point"])/Total * 360 ); Currentdegree + = convert. tosingle (Ds. Tables [0]. Rows [iloop] ["point"])/Total * 360; } // --- Generate the main title as follows Solidbrush blackbrush = new solidbrush (color. Black ); String title = "this" Programmer base camp "introduced a supporting special publication. Are you satisfied with this special publication? "; Stringformat = new stringformat (); Stringformat. Alignment = stringalignment. Center; Stringformat. linealignment = stringalignment. Center; Objgraphics. drawstring (title, fonttitle, blackbrush, New rectangle (0, 0, width, titleheight), stringformat ); // List fields and votes Objgraphics. drawrectangle (new pen (color. Black, 2), 0, height-legendheight, width, legendheight ); For (iloop = 0; iloop <Ds. Tables [0]. Rows. Count; iloop ++) { Objgraphics. fillrectangle (solidbrush) colors [iloop], 5, height-legendheight + fontlegend. Height * iloop + 5, 10, 10 ); Objgraphics. drawstring (string) ds. tables [0]. rows [iloop] ["title"]) + "-" + convert. tostring (Ds. tables [0]. rows [iloop] ["point"]), fontlegend, blackbrush, 20, height-legendheight + fontlegend. Height * iloop + 1 ); } // Objgraphics. drawstring ("total votes:" + convert. tostring (total), fontlegend, blackbrush, 5, height-fontlegend. Height ); // The total height of the image-the height of a row of font, that is, the height of the row of the base row (height-fontlegend. Height) Response. contenttype = "image/JPEG "; Objbitmap. Save (response. outputstream, imageformat. JPEG ); // Objbitmap. Save ("myyyyyyyyyyy.jpg", imageformat. JPEG); // output to the file Objgraphics. Dispose (); Objbitmap. Dispose (); } </SCRIPT> |