我对ZedGraph有一点不同的要求。
我想在用户单击ZedGraph窗格时在ZedGraph窗格上创建曲线。此外,我还在该窗格上绘制了其他图形。但我希望每当用户点击zedGraph区域时,我们就会得到用户点击的坐标,然后我在点击的坐标上画一条直线。
我在FindNearestObject方法中使用了MouseCLick事件alogn,如下所示:
private void zedGraph_RenderedTrack_MouseClick(object sender, EventArgs e)
{
MouseEventArgs xx = (MouseEventArgs)e;
object nearestObject;
int index;
this.zedGraph_RenderedTrack.GraphPane.FindNearestObject(new PointF(xx.X, xx.Y), this.CreateGraphics(), out nearestObject, out index);
if (nearestObject != null)
{
DrawALine(xx.X, Color.Red, true);
}
} 但是使用这个,ZedGraph会搜索一些曲线,找到最近的点,然后绘制这条线,但我希望这条线是在用户点击的地方绘制的。有什么方法可以做到这一点吗?
发布于 2012-09-14 23:07:31
您可以尝试下面的代码,它将为鼠标单击事件绘制一条垂直线。
public Form1()
{
InitializeComponent();
}
PointPairList userClickrList = new PointPairList();
LineItem userClickCurve = new LineItem("userClickCurve");
private void zedGraphControl1_MouseClick(object sender, MouseEventArgs e)
{
// Create an instance of Graph Pane
GraphPane myPane = zedGraphControl1.GraphPane;
// x & y variables to store the axis values
double xVal;
double yVal;
// Clear the previous values if any
userClickrList.Clear();
myPane.Legend.IsVisible = false;
// Use the current mouse locations to get the corresponding
// X & Y CO-Ordinates
myPane.ReverseTransform(e.Location, out xVal, out yVal);
// Create a list using the above x & y values
userClickrList.Add(xVal, myPane.YAxis.Scale.Max);
userClickrList.Add(xVal, myPane.YAxis.Scale.Min);
// Add a curve
userClickCurve = myPane.AddCurve(" ", userClickrList, Color.Red, SymbolType.None);
zedGraphControl1.Refresh();
}

您只需更改userClickList即可绘制水平线。
快乐编码...:)
https://stackoverflow.com/questions/12422398
复制相似问题