C# winform动态画折线统计图

数据表:
在这里插入图片描述
将数据查询出来后,放在一个DataTable(以下变量dt),查询代码省略
找到要画图的容器,在容器的Paint事件中写上代码:

private void ZheXian_Paint(object sender, PaintEventArgs e)
        {
            Pen pen = new Pen(Color.Red, 2);
            Graphics ZheXianTu = e.Graphics;
            Font myFont = new Font("宋体", 10);
            Brush myBrush = new SolidBrush(Color.Red);
            GraphicsPath myGP = new GraphicsPath();
            myGP.AddLine(34, 40, 40, 20);
            myGP.AddLine(40, 20, 46, 40);
            myGP.AddLine(40, 20, 40, 420);
            myGP.AddLine(40, 420, 480, 420);
            myGP.AddLine(480, 420, 460, 414);
            myGP.AddLine(480, 420, 460, 426);
            ZheXianTu.DrawPath(pen, myGP);
            for (int i = 0; i < 19; i++)
            {
                ZheXianTu.DrawString((i * 10).ToString(), myFont, myBrush, 12, 413 - (i * 20));
                ZheXianTu.DrawString("-", myFont, myBrush, 39, 413 - (i * 20));
            }
            GraphicsPath ZheXian = new GraphicsPath();
            int x1 = 40, y1 = 420;
            for (int i = 1; i < dt.Rows.Count + 1; i++)//用点连点画折线
            {
                string ShuLiang = dt.Rows[i - 1]["数量"].ToString();
                int x = 20 * i * 3;
                int GaoDu = Convert.ToInt32(ShuLiang) * 2;
                int y = 420 - GaoDu;
                ZheXianTu.DrawString(dt.Rows[i - 1]["物品名称"].ToString(), myFont, myBrush, x, 425);
                ZheXian.AddLine(x1, y1, x, y);
                x1 = x; y1 = y;
                ZheXianTu.DrawString(ShuLiang, myFont, myBrush, x, y - 20);
                ZheXianTu.FillEllipse(myBrush, x, y, 3, 3);
            }
            ZheXianTu.DrawPath(pen, ZheXian);
        }

效果图:
在这里插入图片描述

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值