数据表:
将数据查询出来后,放在一个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);
}
效果图: