当前位置 : 主页 > 网络编程 > net编程 >

Net绘制柱形图

来源:互联网 收集:自由互联 发布时间:2023-09-07
//绘制柱形图 Total方法 是计算一年内的总流量,先忽略根据自己情况编写; int w = 600; int h = 400; Bitmap b = new Bitmap(w,h); Graphics g = Graphics.FromImage(b); g.Clear(Color.White); LinearGradientBrush brush =

 //绘制柱形图

 Total方法是计算一年内的总流量,先忽略根据自己情况编写;

       int w = 600;

       int h = 400;

       Bitmap b = new Bitmap(w,h);

       Graphics g = Graphics.FromImage(b);

       g.Clear(Color.White);

       LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, b.Width, b.Height), Color.Blue, Color.BlueViolet, 1.2f, true);

       Font font = new Font("Arial",7, FontStyle.Regular);

       Font font1 = new Font("宋体", 20, FontStyle.Bold);

       g.FillRectangle(Brushes.WhiteSmoke, 0, 0, w, h);

       g.DrawString("2022年各月份流量统计", font1, brush, new PointF(130, 30));

       g.DrawRectangle(new Pen(Color.Blue), 0, 0, w - 1, h - 1);

       Pen p = new Pen(brush, 1);

       //绘制竖线

       int x = 100;

       for (int i = 0; i < 11; i++)

       {

           g.DrawLine(p, x, 80, x, 340);

           x = x + 40;

       }


       g.DrawLine(new Pen(Color.Blue, 2), x - 480, 80, x - 480, 340);

       //绘制横线

       int y1 = 106;

       for (int y = 0; y < 9; y++)

       {

           g.DrawLine(p, 60, y1, 540, y1);

           y1 = y1 + 26;

       }

       g.DrawLine(new Pen(Color.Blue, 2), 60, y1, 540, y1);

       string[] n = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};

       x = 62;

       for (int i = 0; i < 12; i++)

       {

           g.DrawString(n[i], font, Brushes.Black, x, 350);

           x = x + 40;

       }

       string[] m1 = {"100%", "90%", "80%", "70%", "60%", "50%", "40%", "30%", "20%", "10%","0%" };

       y1 = 85;

       for (int i = 0; i < 11; i++)

       {

           g.DrawString(m1[i], font, Brushes.Black, 25, y1);

           y1 = y1 + 26;

       }

       int[] count = new int[12];

       SqlConnection c = new SqlConnection(ConfigurationManager.AppSettings["cons"]);

       c.Open();

       SqlDataAdapter d;

       string cmdStr="";

       DataSet ds = new DataSet();

       for (int i = 0; i < 12; i++)

       {

           cmdStr = "select count(*) as count,month(logintime) logintime from tb_10 where year(logintime)=2007 and month(logintime)="+i+1+ " group by month(logintime)";

           d = new SqlDataAdapter(cmdStr, c);

           d.Fill(ds, i.ToString());

           if (ds.Tables[i].Rows.Count == 0)

           {

               count[i] = 0;

           }

           else

           {

               count[i] = Convert.ToInt32(ds.Tables[i].Rows[0][0])*100/Total();

           }

       }

       //绘制方形块

       x = 70;

       for (int i = 0; i < 12; i++)

       {

           g.FillRectangle(new SolidBrush(Color.Blue), x, 340 - count[i] * 26 / 10, 20, count[i] * 26 / 10);

           x = x + 40;

       }

       System.IO.MemoryStream m = new System.IO.MemoryStream();

       b.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);

       Response.ClearContent();

       Response.ContentType = "image/Jpeg";

       Response.BinaryWrite(m.ToArray());


图示:

Net绘制柱形图_gdi

上一篇:ASP.NET Core入门五
下一篇:没有了
网友评论