using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
public partial class Statistics_test1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//图片大小imgx,imgy
int imgx = 800, imgy = 700;//整个图片的长宽
String[] nw = { "第一天", "第二天", "第三天", "第四天", "第9天" };
int[] ss = { 100, 200, 300,400, 10000};//暂时不支持负数
int tempmax = 0, tempmix = 0,ping=0;
for (int i = 0; i < ss.Length; i++)
{
if (tempmax < ss[i])
tempmax = ss[i];
}
ping = (tempmax - tempmix) / 10;
String[] mw = { "0人", " 100人", " 200人", "300人", " 400人", " 500人", " 600人", " 700人", "800人", " 900人",tempmax.ToString()};
for (int i = 0; i < 10; i++)
{
mw[i] = (tempmix + (ping * i)).ToString();
}
CreateImage(imgx, imgy, nw, mw, ss, tempmax);//调用画图函数
}
private void CreateImage(int width, int height, string[] nw, string[] mw, int[] ss, int tempmax)
{
int xshu = nw.Length, yshu = mw.Length-1;//x上面点数量
Bitmap image = new Bitmap(width, height);
Graphics g = Graphics.FromImage(image);
//定义一个26个颜色数组
Brush[] yanse ={Brushes.SeaGreen,Brushes.DarkBlue,Brushes.LightSlateGray,Brushes.Tomato,Brushes.Maroon
,Brushes.Blue,Brushes.Purple,Brushes.DarkKhaki,Brushes.Red,Brushes.RosyBrown
,Brushes.Chocolate,Brushes.DarkOrchid,Brushes.CornflowerBlue,Brushes.Salmon,Brushes.SlateBlue,
Brushes.SteelBlue,Brushes.Black,Brushes.DarkGoldenrod,Brushes.DarkGreen,Brushes.DarkMagenta
,Brushes.YellowGreen,Brushes.DeepPink,Brushes.DeepSkyBlue,Brushes.Teal,Brushes.Lime,
Brushes.Turquoise};
try
{
//清空图片背景色
g.Clear(Color.White);
Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
Font font1 = new System.Drawing.Font("宋体", 20, FontStyle.Regular);
Font font2 = new System.Drawing.Font("Arial", 8, FontStyle.Regular);
LinearGradientBrush brush = new LinearGradientBrush(
new Rectangle(0, 0, image.Width, image.Height), Color.LightGray, Color.LightGray, 1.2f, true);
g.FillRectangle(Brushes.AliceBlue, 0, 0, width, height);
Brush brush1 = new SolidBrush(Color.Black);
Brush brush2 = new SolidBrush(Color.SaddleBrown);
g.DrawString(" 折线图", font1, brush1, new PointF(image.Width/2-80, 30));//文字的位置
//画图片的边框线
// g.DrawRectangle(new Pen(Color.Red), 0, 0, image.Width - 1, image.Height - 1);
Pen mypen = new Pen(brush, 1);
//绘制线条
//绘制纵向线条
int x = 60;
for (int i = 0; i < xshu; i++)
{
if(i==0)
g.DrawLine(mypen, x + 2, 80, x + 2, image.Height - 160);//内部竖线,第一个数值是开始高度,第二个数值结束高度,
else
g.DrawLine(mypen, x, 80, x, image.Height - 160);//内部竖线,第一个数值是开始高度,第二个数值结束高度,
x = x + (width - 140) / xshu;
}
Pen mypen1 = new Pen(Color.Black, 3);
x = 60;
g.DrawLine(mypen1, x, 76, x, image.Height - 160);//图标的Y轴开始对应图原点的X,Y坐标
////绘制横向线条
int y = image.Height - 160;//第一条线横线,相对于顶点的高度
for (int i = 0; i < yshu+1; i++)
{
g.DrawLine(mypen, 62, y, image.Width - 80, y);
y = y - ((image.Height - 240) / yshu);
}
y = 106;
//画出了内部的横线
//for (int i = 0; i < yshu; i++)
//{
// g.DrawLine(mypen, 60, y, 620, y);
// y = y + (260 / yshu);
//}
// y = 106;
g.DrawLine(mypen1, 60, image.Height - 160, image.Width - 80, image.Height - 160);
//x轴
String[] n = nw;//使用外部数组
x = 45;
for (int i = 0; i < n.Length; i++)
{
g.DrawString(n[i].ToString(), font, Brushes.Black, x, image.Height - 160+10); //设置文字内容及输出位置
x = x + (width - 140) / xshu;
}
//y轴
String[] m = mw;
y = image.Height - 160;
for (int i = 0; i <m.Length; i++)
{
g.DrawString(m[i].ToString(), font, Brushes.Black, 20, y); //设置文字内容及输出位置
y = y - ((image.Height - 240) / yshu);
}
//int[] Count1 = new int[7];
//int[] Count2 = new int[7];
//SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeTraining;Uid=sa;Pwd=eesoft");
//Con.Open();
//string cmdtxt2 = "SELECT * FROM ##Count where Company='" + this.ddlTaget.SelectedItem.Text.Trim() + "'";
//SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con);
//DataSet ds = new DataSet();
//da.Fill(ds);
//报名人数
//Count1[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count1"].ToString());
//Count1[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count3"].ToString());
//Count1[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count5"].ToString());
//Count1[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count7"].ToString());
//Count1[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count9"].ToString()); //全年
//Count1[4] = Count1[0] + Count1[1];
//Count1[5] = Count1[2] + Count1[3];
//Count2[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count2"].ToString());
//Count2[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count4"].ToString());
//Count2[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count6"].ToString());
//Count2[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count8"].ToString());
//Count2[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count10"].ToString()); //全年
//Count2[4] = Count2[0] + Count2[1];
//Count2[5] = Count2[2] + Count2[3];
//显示折线效果
Font font3 = new System.Drawing.Font("Arial", 10);
Pen mypen2 = null;
Point[] points1 = null;
mypen2 = new Pen(yanse[0], 2);
points1 = new Point[xshu];
for (int i = 0; i < xshu; i++)
{
//((image.Height - 240) / yshu//高度
points1[i].X = 60 + ((image.Width - 140) / xshu * i); points1[i].Y = image.Height - 160 - (image.Height - 240 ) * ss[i] / tempmax; //从106纵坐标开始, 到(0, 0)坐标时
g.DrawString(ss[i].ToString(), font3, yanse[0], 60 + ((image.Width - 140) / xshu * i) - 20, points1[i].Y - 20);
}
g.DrawLines(mypen2, points1); //绘制折线
//绘制数字
//这里要放置26个 (width - 140)
//高度
int tempgx = 40 , tempgy = image.Height - 120;
int byl = tempgx;
for (int i = 0; i < 26; i++)
{
if (240 + (byl * 150) > width - 60)
{
byl = 0;
tempgx = 40;
tempgy = tempgy + 16;
}
else
{
byl += 1;
tempgx = 40 + (byl * 150);
}
g.FillRectangle(yanse[i], tempgx, tempgy, 20, 10); //绘制小矩形
g.DrawString("报名人数", font2, yanse[i], tempgx + 22, tempgy - 2);
}
//g.FillRectangle(Brushes.Green, 100, 322, 20, 10);
//g.DrawString("通过人数", font2, Brushes.Green, 122, 320);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
//页面显示图片
//把图片保存到本地
//string uploadFilePath = null;
//uploadFilePath = System.Web.HttpContext.Current.Server.MapPath("../") + "zxt.jpg";
//image.Save(uploadFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
引用空间:http://www.cnblogs.com/ziyiFly/archive/2008/09/24/1297841.html