|
Using System;
Using System.Collections;
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;
Namespace WebApplication1
{
public partial class _default:system.web.ui.page
{
Private string[,] data = new string[6, 2];
protected void Page_Load (object sender, EventArgs e)
{
Drawingapic ();
}
private void Drawingapic ()
{
int i;
Instantiating a Bitmap object
Bitmap Objbitmap;
Objbitmap = new Bitmap (400, 300);
Graphics Objgraphics;
Instantiate the Graphics class
Objgraphics = Graphics.fromimage (Objbitmap);
Fill background color
Objgraphics.clear (Color.White);
Draw a Circle
Objgraphics.drawrectangle (Pens.black, 1, 1, 398, 298);
Write title
Objgraphics.drawstring ("The first half of the company turnover statistics Chart", New Font ("XXFarEastFont-Arial", FontStyle.Bold), Brushes.black, New PointF (60, 5));
Get data, here simulate 6 months of company business data, the actual application can read 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 ("XXFarEastFont-Arial,"), Brushes.black, fontinfor);
Move the coordinate position and move only the Y-direction value.
Monthcolor. Y + 15;
Fontinfor. Y + 15;
}
Traverses each data source and draws a rectangular chart (that is, a column of columns) based on the size of the data.
for (i = 0; I <= 5; i++)
{
Draw the fill rectangle.
Objgraphics.fillrectangle (New SolidBrush (GetColor (i)), (I *) +, 270-system.convert.toint32 (Data[i, 1]), System . Convert.ToInt32 (Data[i, 1]));
' Draw a rectangular border line.
Objgraphics.drawrectangle (Pens.black, (i *) +, 270-system.convert.toint32 (Data[i, 1]), System.Convert.ToInt32 (Data[i, 1]));
}
Draw the schematic coordinates
Objgraphics.drawline (New Pen (Color.Blue, 1), 10, 0, 10, 320);
Objgraphics.drawline (New Pen (Color.Blue, 1), 10, 270, 200, 270);
Add a numeric marker to the schematic coordinates and pay attention to the calculation of the coordinates
for (i = 0; I <= 5; i++)
{
Objgraphics.drawline (New Pen (Color.Blue, 1), I * +, I * 50 + 20);
Objgraphics.drawstring ((250-i * 50). ToString (), New Font ("XXFarEastFont-Arial"), Brushes.black, I * 50 + 8);
}
Total statistics Sales
float Scount = 0;
for (i = 0; I <= 5; i++)
{
Scount + = float. Parse ((data[i, 1]);
}
Define a pie angle variable
float SCG = 0;
float STG = 0;
for (i = 0; I <= 5; i++)
{
Calculates the current angle value: The month sales/Total sales * 360, the size of the month in the pie chart.
float num = float. Parse (Data[i, 1]);
SCG = (num/scount) * 360;
Draw a filled arc.
Objgraphics.fillpie (GetColor (i)), SolidBrush, STG, SCG);
Draw the arc line.
Objgraphics.drawpie (Pens.black, STG, SCG);
Add the current arc angle to the general angle.
STG + = SCG;
}
Draw a descriptive text
Objgraphics.drawstring ("Histogram", New Font ("XXFarEastFont-Arial", FontStyle.Bold), Brushes.blue, 50, 272);
Objgraphics.drawstring ("Pie chart", New Font ("Song Body", "fontstyle.bold"), Brushes.blue, 250, 272);
Output to Client
Objbitmap. Save (Response.outputstream, System.Drawing.Imaging.ImageFormat.Gif);
}
assigning values to arrays
That is, generating simulated business data
private void GetData ()
{
Data[0, 0] = "January";
data[1, 0] = "February";
data[2, 0] = "March";
Data[3, 0] = "April";
Data[4, 0] = "May";
Data[5, 0] = "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";
}
Produces color values for easy display of differences
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;
}
}
} |