绘制柱形图(对排序数据进行分析)

 

private SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=sa;database= MyProject");

 


            
        /// <summary>
        /// 升序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string str = "asc";
            showPic(str);
        }

        /// <summary>
        /// 降序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string str = "desc";
            this.showPic(str);
        }

 

 

private void showPic(string str)
        {
                conn.Open();
                string sql = "select * from GetEmplores order by EmployeePrice " + str + "";
                SqlCommand cmd = new SqlCommand(sql,conn);
                SqlDataReader dr = cmd.ExecuteReader();
               
                Bitmap bit = new Bitmap(this.panel1.Width,this.panel1.Height);
                Graphics g = Graphics.FromImage(bit);

                try
                {
                    g.Clear(Color.White);

                    //绘制水平线条
                    for (int i = 0; i < 5; i++)
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.Red), 2.0f), 50, this.panel1.Height - 20 - i * 20, this.panel1.Width - 40, this.panel1.Height - 20 - i * 20);
                        g.DrawString(Convert.ToString(i * 100), new Font("Times New Roman", 10, FontStyle.Regular), new SolidBrush(Color.Black), 20, this.panel1.Height - 27 - i * 20);
                    }

                    //绘制垂直线条
                    for (int j = 0; j < 4; j++)
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.Red), 1.0f), 50 + 40 * j, this.panel1.Height - 20, 50 + 40 * j, 50);

                        if (dr.Read())
                        {
                            int x, y, w, h;

                            //绘制商品名称
                            g.DrawString(dr[0].ToString(), new Font("宋体", 8, FontStyle.Regular), new SolidBrush(Color.Black), 76 + 40 * j, this.panel1.Height - 16);
                            x = 78 + 40 * j;
                            y = this.panel1.Height - 20 - Convert.ToInt32((Convert.ToDouble(Convert.ToDouble(dr[1].ToString()) * 20 / 100)));

                            w = 24;                     //宽度
                            h = Convert.ToInt32(Convert.ToDouble(dr[1].ToString())*20/100);      //高度
                            g.FillRectangle(new SolidBrush(Color.FromArgb(56, 129, 78)), x, y, w, h);                //开始绘制柱形矩形图
                        }
                    }

                    if (str == "desc")
                    {
                        //绘制提示文字,提示商品按降序排列
                        g.DrawString("热卖前三名", new Font("宋体", 9), new SolidBrush(Color.Red), this.panel1.Width / 2 - 26, 20);
                    }
                    else if (str == "asc")
                    {
                        //绘制提示文字,提示商品按升序排列
                        g.DrawString("热卖后三名", new Font("宋体", 9), new SolidBrush(Color.Red), this.panel1.Width / 2 - 26, 20);
                    }
                    this.panel1.BackgroundImage = bit;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
                finally
                {
                    dr.Close();
                    conn.Close();
                }      

            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值