C# winform利用Graphics绘制数据折线图

int[] x = new int[20];
int[] y = new int[20];
float[] data = new float[20];
Point[] pot = new Point[20];
int i = 0;
Font f = new Font("隶书", 10, FontStyle.Bold);
private void Form1_Paint(object sender, PaintEventArgs e)
 {
       Graphics gobj = e.Graphics;
       HatchBrush bobj = new HatchBrush(HatchStyle.Vertical, Color.Gray, Color.Black);
       StringFormat sf = new StringFormat();
       using (SqlConnection conn = new SqlConnection(DBHelper.connString))
       {
              string sql = "select no from test1 order by id";
               SqlCommand cmd = new SqlCommand(sql, conn);
               conn.Open();
               SqlDataReader dr = cmd.ExecuteReader();
               if (dr.HasRows)
               {
                    x[0] = 20;
                    y[0] = 200;
                    pot[0] = (new Point(x[0], y[0]));
                    while (dr.Read())
                    {
                        i++;
                        x[i] = 20 + i * 20;
                        y[i] = 200 - int.Parse(dr["no"].ToString());
                        data[i] = float.Parse(dr["no"].ToString());
                        pot[i] = (new Point(x[i], y[i]));
                        //gobj.FillEllipse(Brushes.Black, x[i] - 2, y[i] - 2, 4, 4);//数据节点画黑
                    }
                    //string str = "";
                    //for (int j = 0; j <= i; j++){
                    //    str += (x[j].ToString() + y[j].ToString()) + ",";
                    //}
                    //MessageBox.Show(str);
              }
       }
       gobj.DrawCurve(new Pen(Color.Khaki, 2), pot, 1, i - 1, 0.05f);//数据间连曲线
       for (int j = 1; j <= i; j++)
       {
              gobj.FillEllipse(Brushes.Gray, x[j] - 2, y[j] - 2, 4, 4);//数据节点画黑
              gobj.DrawString(data[j].ToString(), f, bobj, x[j]-10, y[j]-13, sf);//数据文本显示在节点上方
       }
}

阅读更多
换一批

没有更多推荐了,返回首页