using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using System.IO;using System.Drawing.Drawing2D;namespace CallSystem{ public partial class Chart : Form { public Chart() { InitializeComponent(); } private void Chart_Load(object sender, EventArgs e) { CreateImage(); } private void CreateImage() { string[] month = new string[7] { "第一季度", "第二季度", "第三季度", "第四季度", "上半年", "下半年", "全年统计" }; Bitmap bMap = new Bitmap(750, 750); Graphics gph = Graphics.FromImage(bMap); gph.Clear(Color.White); PointF cpt = new PointF(40, 420); //X轴三角形 PointF[] xpt = new PointF[3] { new PointF(578, cpt.Y), new PointF(570, cpt.Y - 4), new PointF(570, cpt.Y + 4) }; //Y轴三角形 PointF[] ypt = new PointF[3] { new PointF(cpt.X, cpt.X - 8), new PointF(cpt.X - 4, cpt.X), new PointF(cpt.X + 4, cpt.X) }; gph.DrawString("呼叫统计图", new Font("宋体", 14), Brushes.Black, new PointF(cpt.X + 60, cpt.X)); //画X轴 gph.DrawLine(Pens.Black, cpt.X, cpt.Y, 570, cpt.Y); gph.DrawPolygon(Pens.Black, xpt); gph.FillPolygon(new SolidBrush(Color.Black), xpt); gph.DrawString("月份", new Font("宋体", 12), Brushes.Black, new PointF(580, cpt.Y + 10)); //画Y轴 gph.DrawLine(Pens.Black, cpt.X, cpt.Y, cpt.X, cpt.X); gph.DrawPolygon(Pens.Black, ypt); gph.FillPolygon(new SolidBrush(Color.Black), ypt); gph.DrawString("单位(次)", new Font("宋体", 12), Brushes.Black, new PointF(0, 7)); for (int i = 1; i <= 15; i++) { //画Y轴刻度 if (i < 15) { gph.DrawString((i * 25).ToString(), new Font("宋体", 11), Brushes.Black, new PointF(15, cpt.Y - i * 25 - 6)); gph.DrawLine(Pens.Black, cpt.X + 3, cpt.Y - i * 25, cpt.X, cpt.Y - i * 25); } } int a = 60; for (int i = 0; i < 7; i++) { gph.DrawString(month[i].ToString(), new Font("宋体", 11), Brushes.Blue, a, 430); //设置文字内容及输出位置 a = a + 71; } int[] Count1 = new int[7]; int[] Count2 = new int[7]; int[] Count3 = new int[7]; try { string sql = null; for (int n = 1; n <= 10; n = n + 3) { //根据季度来查询 sql = string.Format("select count(*) from History where month(time) between '" + n + "'and '" + (n + 2) + "'and datediff(year, time,getdate())=0"); SqlDataAdapter da = new SqlDataAdapter(sql, DBHelper.connection); DataSet ds = new DataSet(); int num = da.Fill(ds, "History"); Count1[n / 3] = (int)ds.Tables[0].Rows[0][0]; //根据条件查找自己所需要的数据,然后赋值 string sql1 = sql + "and status='服务超时'"; SqlDataAdapter da1 = new SqlDataAdapter(sql1, DBHelper.connection); DataSet ds1 = new DataSet(); int num1 = da1.Fill(ds1, "History"); Count2[n / 3] = (int)ds1.Tables[0].Rows[0][0]; string sql2 = sql + "and status!='服务超时'"; SqlDataAdapter da2 = new SqlDataAdapter(sql2, DBHelper.connection); DataSet ds2 = new DataSet(); int num2 = da2.Fill(ds2, "History"); Count3[n / 3] = (int)ds2.Tables[0].Rows[0][0]; } //Count1[4][5][6]得到上半年数据 Count1[4] = Count1[0] + Count1[1]; Count1[5] = Count1[2] + Count1[3]; Count1[6] = Count1[4] + Count1[5]; //Count2[4][5][6]得到下半年数据 Count2[4] = Count2[0] + Count2[1]; Count2[5] = Count2[2] + Count2[3]; Count2[6] = Count2[4] + Count2[5]; //Count3[4][5][6]得到全年数据 Count3[4] = Count3[0] + Count3[1]; Count3[5] = Count3[2] + Count3[3]; Count3[6] = Count3[4] + Count3[5]; float x = cpt.X; Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold); Brush brush1 = new SolidBrush(Color.Red); for (int m = 0; m < 7; m++) { x = x + 29; gph.FillRectangle(brush1, x, 420 - Count1[m], 20, Count1[m]);//(brush,x,y,width,height) gph.DrawString(Count1[m].ToString(), font2, Brushes.Red, x, 420 - Count1[m] - 15); x = x + 20; gph.FillRectangle(new SolidBrush(Color.Green), x, 420 - Count2[m], 20, Count2[m]); gph.DrawString(Count2[m].ToString(), font2, Brushes.Green, x, 420 - Count2[m] - 15); x = x + 20; gph.FillRectangle(new SolidBrush(Color.Blue), x, 420 - Count3[m], 20, Count3[m]); gph.DrawString(Count3[m].ToString(), font2, Brushes.Blue, x, 420 - Count3[m] - 15); } //绘制标识 Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Regular); gph.DrawRectangle(new Pen(Brushes.Blue), 520, 15, 100, 70); //绘制范围框 gph.FillRectangle(Brushes.Red, 530, 20, 20, 15); //绘制小矩形 gph.DrawString("呼叫次数", font3, Brushes.Red, 550, 20); gph.FillRectangle(Brushes.Green, 530, 40, 20, 15); gph.DrawString("应答次数", font3, Brushes.Green, 550, 40); gph.FillRectangle(Brushes.Blue, 530, 60, 20, 15); gph.DrawString("超时次数", font3, Brushes.Blue, 550, 60); System.IO.MemoryStream ms = new System.IO.MemoryStream(); bMap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); /* Asp.Net image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); Response.ClearContent(); Response.ContentType = "image/Jpeg"; */ pictureBox1.Image = new Bitmap(bMap); } catch (Exception) { throw; } finally { gph.Dispose(); bMap.Dispose();//释放bmp文件资源 } } //打印功能 private void btn_Print_Click(object sender, EventArgs e) { //通过打印功能,把图片保存到本地 Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap Graphics g = Graphics.FromImage(bit); g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高 g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片 bit.Save("D:\\统计图.png");//默认保存格式为PNG,保存成jpg格式质量不是很好 MessageBox.Show("保存到D盘","保存提示",MessageBoxButtons.OK,MessageBoxIcon.Information); } }}