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(); }
}