每次写正题前都想啰嗦几句,其实主要是想记录一下每一个项目的出处和实现过程,其实这个项目也是客户提的,他们看外国佬的东西 有这个功能,也要求我们来做,刚开始我得到的需求就是简短的两三句话,具体什么样呢,给我下达任务的boss也不清楚,我也很头疼,怎么办呢,只能凭着自己的想象力去做吧,利用GUI 之前我用ZedGraph实现了一个版本,发给我们的那个那个客服吧,给我来了一句太Low,把我给气的,简直是没文化,真可怕,瞎逼逼,声还大。可是没有办法,让他们把外国佬的东西截几张图看看,总得让我知道你们想要啥吧,别特么总让我猜你们的需求啊,老子又不是你们肚子里的蛔虫不是。于是照着人家图画吧。
其实整个项目并不难,主要是比较烦,你特么认为ZedGraph太Low,还特么得实现人家ZedGraph那种效果,可是没有办法,谁让咱得吃饭啊。好啦,下面开始正题。。。。。。。
首先将数据存在List里,然后Graph GrawRectangle函数画出矩形,以及对应的不同颜色,然后在Picturebox的paint事件重绘坐标系,最后重写鼠标滚轮,左键,右键事件,对图片进行操作。下面是部分代码:
public Bitmap DrawRectangles(float x, float y, List<float> lenList, List<Color> listColor, List<string> listname, float height, string ylable)//这里是将数据绘制出矩形,用bitmap防止界面最小化之后,图会被擦掉。
{
try
{
int count = lenList.Count;
float temp = x;
newlistColor = listColor;
newlistname = listname;
for (int i = 0; i < count; i++)
{
float pre = temp;
float templen = lenList[i];
if (temp<50f)
{
graph.DrawRectangle(new Pen(Color.Black, 2), 50, y, lenList[i] - Math.Abs(temp), height);
graph.FillRectangle(new SolidBrush(listColor[i]), 50, y, lenList[i] - Math.Abs(temp), height);
if (lenList[i] > 50)
graph.DrawString(listname[i], new Font("宋体", 8f), Brushes.Black, 50, y + 5);
}
else
{
graph.DrawRectangle(new Pen(Color.Black, 2), temp, y, lenList[i], height);
graph.FillRectangle(new SolidBrush(listColor[i]), temp, y, lenList[i], height);
if (lenList[i] > 50)
graph.DrawString(listname[i], new Font("宋体", 8f), Brushes.Black, temp, y + 5);
}
temp += lenList[i];
}
return img;
}
catch(Exception ex)
{
return null;
}
}
下面是paint事件重回坐标系,具体鼠标事件对图片放大缩小移动,我就不在这里放了
private void picCanvas_Paint(object sender, PaintEventArgs e)
{
try
{
//Cursor = Cursors.Hand;
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Black, 1);
g.DrawLine(pen, 50, h, w, h);
g.DrawLine(pen, 50, 20, 50, h);
g.DrawLine(pen, 50, 20, w, 20);
g.DrawLine(pen, w, 20, w, h);
foreach (DrawWaferData data in nowwafertrace.listDrawData)
{
g.DrawString(data.waferId, new Font("宋体", 8f, FontStyle.Bold), Brushes.Black, new PointF(15, data.y + 5));
}
for (int i = 1; i < 38; i++)
{
PointF px1 = new PointF(i * 30 + 50, h - 5);
PointF px2 = new PointF(i * 30 + 50, h + 5);
string sx = ((Xlable[i] + org_delta) / m_nScale).ToString("00");
if (changeScale)
sx = ((Xlable[i] + org_delta) / m_nScale).ToString("00");
else
sx = (Xlable[i]).ToString("00");
if (defaultScale)
{
sx = Org_xlable[i].ToString("00");
}
g.DrawLine(new Pen(Brushes.Black, 1), px1, px2);
if (i % 4 == 0)
{
g.DrawString(sx, new Font("宋体", 8f), Brushes.Black, new PointF(i * 30 + 50, h + 5));
}
if (changeScale)
Xlable[i] = ((Xlable[i] + org_delta) / m_nScale);
}
g.DrawString("Seconds", new Font("宋体 ", 10f), Brushes.Black, new PointF(w / 2 + 50, h + 20));
}
catch (Exception ex)
{
}
}
最后给大家看一下效果
有兴趣的伙伴可以给我评论留言。