Here is a graph generated from the data in the Dataset.
My Dataset is the data read from the table Sendrec. There are several fields such as Id, Sendid (order number), Sendtime (recording time), and Sendnum (amount sent per unit time/five minutes in my case)
. The process is as follows:
public void draw(Page page,DataSet ds,int Tnum){}
The page is used to pass the page that references this process, so that the page can directly output the generated curve graph to the client in JPG mode.
ds is the extracted data set.
Tnum is just a parameter I want to use here. I don’t want this class to be exposed to the reading process, so I take the total amount of the order directly and pass it to it.
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.HtmlControls;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
public class imgdraw
{
public imgdraw()
{
}
public void draw(Page page,DataSet ds,int Tnum)
{
//Get the number of records
int count = ds.Tables[0].Rows.Count;
//Calculate chart width
int wd = 80 + 20 * (count - 1);
//Set the minimum width to 800
if (wd < 800) wd = 800;
//Generate Bitmap object
Bitmap img=new Bitmap(wd,400);
//Generate drawing object
Graphics g = Graphics.FromImage(img);
//Define black brush
Pen Bp = new Pen(Color.Black);
//Define the red brush
Pen Rp = new Pen(Color.Red);
//Define silver gray brush
Pen Sp = new Pen(Color.Silver);
//Define the title font
Font Bfont = new Font("Arial", 12, FontStyle.Bold);
//Define general font
Font font = new Font("Arial", 6);
//Define a larger font
Font Tfont = new Font("Arial", 9);
//Draw background color
g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height);
//Define black transition brush
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Black, Color.Black, 1.2F, true);
//Define blue transition brush
LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);
//Draw the big title
g.DrawString(ds.Tables[0].Rows[0]["sendid"].ToString() + "Order delivery status curve", Bfont, brush, 40, 5);
//Get the current sending volume
int nums=0;
for (int i = 0; i < count; i++)
{
nums+=Convert.ToInt32(ds.Tables[0].Rows[i]["sendnum"]);
}
//Draw information briefing
string info="Order sending time:"+ds.Tables[0].Rows[0]["sendtime"].ToString()+" Curve generation time: "+DateTime.Now.ToString()+" Order total Amount: "+Tnum.ToString()+" Current total amount sent: "+nums.ToString();
g.DrawString(info, Tfont, Bluebrush, 40, 25);
//Draw picture border
g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1);
//Draw vertical coordinate line
for (int i = 0; i < count; i++)
{
g.DrawLine(Sp, 40+20 * i, 60, 40+20 * i, 360);
}
//Draw timeline coordinate labels
for (int i = 0; i < count; i+=2)
{
string st = Convert.ToDateTime(ds.Tables[0].Rows[i]["sendtime"]).ToString("hh:mm");
g.DrawString(st, font, brush, 30 + 20 * i, 370);
}
//Draw the horizontal coordinate line
for (int i = 0; i < 10; i++)
{
g.DrawLine(Sp, 40, 60+30*i, 40+20*(count-1), 60+30*i);
int s = 2500 - 50 * i * 5;
//Draw the sending amount axis coordinate label
g.DrawString(s.ToString(), font, brush, 10, 60 + 30 * i);
}
//Draw the vertical axis
g.DrawLine(Bp, 40, 55, 40, 360);
//Draw the horizontal axis
g.DrawLine(Bp, 40, 360, 45 + 20 * (count - 1), 360);
//Define the turning point of the curve
Point[] p = new Point[count];
for (int i = 0; i < count; i++)
{
p[i].X = 40 + 20 * i;
p[i].Y = 360- Convert.ToInt32(ds.Tables[0].Rows[i]["sendnum"]) / 5*3/5;
}
//Draw sending curve
g.DrawLines(Rp, p);
for (int i = 0; i < count; i++)
{
//Draw the sending amount of the sending record point
g.DrawString(ds.Tables[0].Rows[i]["sendnum"].ToString(), font, Bluebrush, p[i].X, p[i].Y - 10);
//Draw the sending record point
g.DrawRectangle(Rp, p[i].X - 1, p[i].Y - 1, 2, 2);
}
//Draw vertical coordinate title
g.DrawString("Send Amount", Tfont, brush, 5, 40);
//Draw the abscissa title
g.DrawString("Send Time", Tfont, brush, 40, 385);
//Save the drawn picture
MemoryStream stream = new MemoryStream();
img.Save(stream, ImageFormat.Jpeg);
//Picture output
page.Response.Clear();
page.Response.ContentType = "image/jpeg";
page.Response.BinaryWrite(stream.ToArray());
}
}
Hey! Posting this is just a record of how to draw a picture. There are still a lot of mistakes here, I hope you can give me some advice.
http://www.cnblogs.com/aowind/archive/2006/11/23/569536.html