Figure 3: Pie Chart

Source: Internet
Author: User
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Using system. drawing;
Using system. Drawing. drawing2d;
Using system. collections;

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Createimage ();
}
Private void createimage ()
{
// Specify the connection string as a constant.
Sqlconnection con = new sqlconnection ("Server = (local); database = db_05; uid = sa; Pwd = eesoft ");
Con. open ();
String cmdtxt = "select * From tb_13_54"; // read type of books, number of books of each type
// Sqlcommand COM = new sqlcommand (cmdtxt, con );
Dataset DS = new dataset ();
Sqldataadapter da = new sqldataadapter (cmdtxt, con );
Da. Fill (DS );
Con. Close ();
Float Total = 0.0f, TMP;

For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
// Convert to single precision. You can also write it as convert. toint32.
TMP = convert. tosingle (Ds. Tables [0]. Rows [I] ["quantity"]);
Total + = TMP;
}

// Set the fonttitle as the title Font
Font fontlegend = new font ("verdana", 9 );
Font fonttitle = new font ("verdana", 10, fontstyle. Bold );

// Background width
Int width = 230;
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 (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
Colors. Add (New solidbrush (color. fromargb (RND. Next (255), RND. Next (255), RND. Next (255 ))));

// Create a bitmap instance
Bitmap objbitmap = new Bitmap (width, height );
Graphics objgraphics = graphics. fromimage (objbitmap );

// Draw a white background
Objgraphics. fillrectangle (New solidbrush (color. White), 0, 0, width, height );

// Draw a bright yellow background
Objgraphics. fillrectangle (New solidbrush (color. Beige), pierect );

// The following is a pie chart (several rows of rows are drawn)
Float currentdegree = 0.0f;
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Objgraphics. fillpie (solidbrush) colors [I], pierect, currentdegree,
Convert. tosingle (Ds. Tables [0]. Rows [I] ["quantity"])/Total * 360 );
Currentdegree + = convert. tosingle (Ds. Tables [0]. Rows [I] ["quantity"])/Total * 360;
}
// Generate the main title as follows
Solidbrush blackbrush = new solidbrush (color. Black );
String title = "sales ratio survey of various types of books ";
Stringformat = new stringformat ();
Stringformat. Alignment = stringalignment. Center;
Stringformat. linealignment = stringalignment. Center;

Objgraphics. drawstring (title, fonttitle, blackbrush,
New rectangle (0, 0, width, titleheight), stringformat );
// List the fields and numbers
Objgraphics. drawrectangle (new pen (color. Black, 2), 0, height-legendheight, width, legendheight );
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Objgraphics. fillrectangle (solidbrush) colors [I], 5, height-legendheight + fontlegend. Height * I + 5, 10, 10 );

Objgraphics. drawstring (string) ds. tables [0]. rows [I] ["bookkind"] + "---" + convert. tostring (convert. tosingle (Ds. tables [0]. rows [I] ["quantity"]) * 100/total ). substring (0, 5) + "%", fontlegend, blackbrush, 20, height-legendheight + fontlegend. height * I + 1 );

}
// 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)
Objgraphics. drawstring ("Total number of books sold:" + convert. tostring (total) + "", fontlegend, blackbrush, 5, height-fontlegend. Height );
Response. contenttype = "image/JPEG ";
Objbitmap. Save (response. outputstream, system. Drawing. imaging. imageformat. JPEG );
Objgraphics. Dispose ();
Objbitmap. Dispose ();
}
}

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.