C # generate a pie chart by using GDI

Source: Internet
Author: User
Tags getcolor

Using system;
Using system. configuration;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Using system. drawing;
Public partial class _ default: system. Web. UI. Page
{
Private string [,] DATA = new string [6, 2];

Protected void page_load (Object sender, eventargs E)
{
Drawingapic ();
}
Public void drawingapic ()
{
Int I;
// Instantiate a bitmap object
Bitmap objbitmap;
Objbitmap = new Bitmap (400,300 );
Graphics objgraphics;
// Instantiate the graphics class
Objgraphics = graphics. fromimage (objbitmap );

// Fill in the background color
Objgraphics. Clear (color. White );

// Circle
Objgraphics. drawrectangle (pens. Black, 1, 1,500,298 );
Objgraphics. drawstring ("the company's turnover statistics for the first half of the year", new font ("", 16, fontstyle. Bold), brushes. Black, new pointf (60, 5 ));

// Obtain data. Here, we simulate 6 months of company business data. The actual application can read data from the database.
Getdata ();

Pointf monthcolor = new pointf (260, 40 );
Pointf fontinfor = new pointf (285, 36 );
For (I = 0; I <= 5; I ++)
{
// Draw a filled rectangle
Objgraphics. fillrectangle (New solidbrush (getcolor (I), monthcolor. X, monthcolor. Y, 20, 10 );
 
// Draw a rectangular border.
Objgraphics. drawrectangle (pens. Black, monthcolor. X, monthcolor. Y, 20, 10 );
 
// Draw the legend description text-data (I, 0)
Objgraphics. drawstring (data [I, 0], new font ("", 10), brushes. Black, fontinfor );
 
// Move the coordinates and only move the value in the Y direction.
Monthcolor. Y + = 15;
Fontinfor. Y + = 15;
}
// Traverse each item of data from the data source and draw a rectangular chart (that is, the column of the column chart) based on the data size ).
For (I = 0; I <= 5; I ++)
{
// Draw a filled rectangle.
Objgraphics. fillrectangle (New solidbrush (getcolor (I), (I * 25) + 35,270-system. convert. toint32 (data [I, 1]), 15, system. convert. toint32 (data [I, 1]);

// 'Draw a rectangular border line.
Objgraphics. drawrectangle (pens. black, (I * 25) + 35,270-system. convert. toint32 (data [I, 1]), 15, system. convert. toint32 (data [I, 1]);
}

// Draw schematic coordinates
Objgraphics. drawline (new pen (color. Blue, 1), 10, 0, and 10,320 );
Objgraphics. drawline (new pen (color. Blue, 1), 10,270,200,270 );
 
// Add a numerical mark to the coordinate. Note the calculation of coordinates.
For (I = 0; I <= 5; I ++)
{
Objgraphics. drawline (new pen (color. Blue, 1), 10, I * 50 + 20, 20, I * 50 + 20 );
Objgraphics. drawstring (250-I * 50). tostring (), new font ("", 10), brushes. Black, 12, I * 50 + 8 );
}
// Count the total sales
Float scount = 0;
For (I = 0; I <= 5; I ++)
{
Scount + = float. parse (data [I, 1]);
}
 
// Define the sector angle variable
Float SCG = 0;
Float STG = 0;
For (I = 0; I <= 5; I ++)
{
// Calculate the current angle value: current month's sales/total sales * 360, and obtain the current month's angle in the pie chart.
Float num = float. parse (data [I, 1]);
SCG = (Num/scount) * 360;
 
// Draw the filled arc.
Objgraphics. fillpie (New solidbrush (getcolor (I), 220,150,120,120, Stg, SCG );
 
// Draw the arc line.
Objgraphics. drawpie (pens. Black, 220,150,120,120, Stg, SCG );
 
// Add the current arc angle to the total angle.
STG + = SCG;
}
 
// Draw instructions
Objgraphics. drawstring ("Bar Chart", new font ("", 15, fontstyle. Bold), brushes. Blue, 50,272 );
Objgraphics. drawstring ("Pie Chart", new font ("", 15, fontstyle. Bold), brushes. Blue, 250,272 );
 
// Output to the client
Objbitmap. Save (response. outputstream, system. Drawing. imaging. imageformat. GIF );
 
}

Private void getdata ()
{
Data [0, 0] = "January ";
Data [1, 0] = "January February ";
Data [2, 0] = "January March ";
Data [3, 0] = "January April ";
Data [4, 0] = "January May ";
Data [5, 0] = "January June ";
Data [0, 1] = "85 ";
Data [1, 1] = "135 ";
Data [2, 1] = "85 ";
Data [3, 1] = "110 ";
Data [4, 1] = "130 ";
Data [5, 1] = "200 ";
}
// Generate color values for easy display
Private color getcolor (int I)
{
Color newcolor;
I + = 1;
If (I = 1)
{
Newcolor = color. blue;
}
Else if (I = 2)
{
Newcolor = color. forestgreen;
}
Else if (I = 3)
{
Newcolor = color. gainsboro;
}
Else if (I = 4)
{
Newcolor = color. moccasin;
}
Else if (I = 5)
{
Newcolor = color. Indigo;
}
Else if (I = 6)
{
Newcolor = color. burlywood;
}
Else
Newcolor = color. goldenrod;
Return newcolor;
}
}

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.