效果如下图所示
zedgraph示例
话不多说,上代码
1. 在zedgraph的xyzPos12_MouseClick事件中,增加鼠标左键的处理事件
private void xyzPos12_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Middle)
{
gbRefLineName = "";
}
else
{
// 选择了要显示的线
if ("" != gbRefLineName)
{
for (int i = mCurvePane.CurveList.Count - 1; i >= 0; i--) // 反向遍历以避免索引问题
{
var curve = mCurvePane.CurveList[i];
if (curve.Tag != null && curve.Tag.Equals(gbRefLineName))
{
mCurvePane.CurveList.Remove(curve); // 删除曲线
}
}
// 再添加曲线
double[] xPos = new double[2];
double[] yPos = new double[2];
Point tmpTop = new Point();
tmpTop.X = e.X;
tmpTop.Y = (int)xyzPos12.GraphPane.Chart.Rect.Top;
Point tmpBottom = new Point();
tmpBottom.X = e.X;
tmpBottom.Y = (int)xyzPos12.GraphPane.Chart.Rect.Bottom;
mCurvePane.ReverseTransform(tmpTop, out xPos[0], out yPos[0]);
mCurvePane.ReverseTransform(tmpBottom, out xPos[1], out yPos[1]);
var tmpCurve = mCurvePane.AddCurve("", xPos, yPos, gbRefLineName.Contains("1") ? Color.Red : Color.DarkSeaGreen, SymbolType.None);
tmpCurve.Tag = gbRefLineName;
if (gbDicRefLines.Keys.Contains(gbRefLineName))
{
gbDicRefLines[gbRefLineName] = (int)xPos[0];
}
else
{
gbDicRefLines.Add(gbRefLineName, (int)xPos[0]);
}
fun_updateRefLineInfo();
}
}
}
2. 在鼠标的移动事件中,增加随鼠标移动的参考线显示
private void xyzPos12_MouseMove(object sender, MouseEventArgs e)
{
// 建立画布
using (Graphics gc = xyzPos12.CreateGraphics())
using (Font font = new Font("Arial", 10, FontStyle.Bold))
using (SolidBrush brush = new SolidBrush(Color.Black))
using (Pen pen = new Pen(Color.Gray))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
RectangleF rect = xyzPos12.GraphPane.Chart.Rect;
//确保在画图区域
if (rect.Contains(e.Location))
{
xyzPos12.Refresh();
gc.DrawLine(pen, e.X, rect.Top, e.X, rect.Bottom);
gc.DrawLine(pen, rect.Left, e.Y, rect.Right, e.Y);
gc.DrawString(gbRefLineName, font, brush, e.X, e.Y);
}
}
}
这样,就可以实现视频中的效果!