前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# winform利用Graphics绘制数据折线图

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

作者头像
静谧的小码农
发布2019-01-11 12:37:19
2.7K0
发布2019-01-11 12:37:19
举报
文章被收录于专栏:静谧的小码农
代码语言:javascript
复制
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);//数据文本显示在节点上方
       }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年06月20日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档