GDI+图表绘制(-) 之 柱型图

本文介绍了一种从数据库动态加载数据并以柱状图形式展示的方法。通过sumNum方法计算各月份商品销售总量,CreateImage方法绘制图表。文章详细展示了C#代码实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这次做的程序的功能是从数据库中动态加载数据,然后以柱型的形式显示。完成这一功能主要由以下两个方法显现,一个为sumNum方法,另一个为CreateImage方法,其中sumNum方法用来计算各月份的商品销售量总和,而CreateImage方法则用来调用Graphics对象的相关方法绘制柱型图。   

sumNum方法实现如下: 

 

///<summary>

///年总售量

///</summary>       

///<param name="P_int_year">年份</param>       

///<returns>年总售量</returns>

private int sumNum(int P_int_year) 

{

        string P_str_sum = "SELECT SUM(mouth1+mouth2+mouth3+mouth4+mouth5+mouth6+mouth7+mouth8+mouth9+mouth10+mouth11+mouth12) AS number FROM SaleInfo WHERE yearID=" + P_int_year + ""; 

        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["connectionString"]); 

       SqlDataAdapter myda = new SqlDataAdapter(P_str_sum, sqlcon);

        DataSet myds = new DataSet(); 

       myda.Fill(myds); 

       return Convert.ToInt32(myds.Tables[0].Rows[0][0].ToString());

}
CreateImage方法实现如下:

 private void CreateImage(int P_int_year)    

{

        int height = 400, width = 600;

        Bitmap image = new Bitmap(width, height); 

       string copyRight = "本作品为tinalucky原创,限于学习,欢迎转载"; 

       //创建Graphics类对象

        Graphics graphics = Graphics.FromImage(image);

        try

        { 

           //清空图片背景色

            graphics.Clear(Color.White); 

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

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

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

           graphics.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height); 

           Brush brush1 = new SolidBrush(Color.Blue); 

           graphics.DrawString("" + P_int_year + "年商品月销售情况", font1, brush1, new PointF(150, 30)); 

           graphics.DrawString(copyRight,new Font("宋体",15,FontStyle.Regular),new SolidBrush(Color.Red),new PointF(80,200)); 

           //画图片的边框线 

           graphics.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1); 

           Pen mypen = new Pen(brush, 1); 

           //绘制横向线条

            int x = 100; 

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

           { 

               graphics.DrawLine(mypen, x, 80, x, 366);

                x = x + 40; 

           } 

           Pen mypen1 = new Pen(Color.Blue, 2); 

           graphics.DrawLine(mypen1, x - 480, 80, x - 480, 366); 

           //绘制纵向线条

            int y = 106;  

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

            { 

               graphics.DrawLine(mypen, 60, y, 540, y); 

               y = y + 26;

            } 

           graphics.DrawLine(mypen1, 60, y, 540, y);

            //x轴

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

           x = 60; 

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

           { 

               graphics.DrawString(n[i].ToString(), font, Brushes.Red, x, 374); 

    //设置文字内容及输出位置

                x = x + 40;

            } 

           //y轴

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

           y = 98; 

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

           { 

               graphics.DrawString(m[i].ToString(), font, Brushes.Red, 25,y); 

    //设置文字内容及输出位置

                y = y + 26; 

           }

            int[] Count = new int[12]; 

           string P_str_yearID = "SELECT * FROM SaleInfo WHERE yearID=" + P_int_year + ""; 

           SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["connectionString"]); 

           SqlDataAdapter myda = new SqlDataAdapter(P_str_yearID, sqlcon); 

           DataSet myds = new DataSet(); 

           myda.Fill(myds); 

           int P_int_num = sumNum(P_int_year); 

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

           { 

               Count[j] = Convert.ToInt32(myds.Tables[0].Rows[0][j + 1].ToString()) * 100 / P_int_num; 

           }

            //显示柱状效果

            x = 70; 

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

           { 

               SolidBrush mybrush = new SolidBrush(Color.Red); 

               graphics.FillRectangle(mybrush, x, 366 - Count[i] * 26 / 10, 20, Count[i] * 26 / 10); 

               x = x + 40; 

           } 

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

           image.Save(MStream, System.Drawing.Imaging.ImageFormat.Gif); 

           Response.ClearContent(); 

           Response.ContentType = "image/Gif"; 

           Response.BinaryWrite(MStream.ToArray()); 

       } 

       finally

        { 

           graphics.Dispose(); 

           image.Dispose();

        }

    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值