单击ZedGraph窗格绘制线条

时间:2012-09-14 10:09:15

标签: c# zedgraph

我对ZedGraph有一点不同的要求。

我想在用户点击ZedGraph窗格时在ZedGraph窗格上创建曲线。此外,我在该窗格上绘制了其他图表。但我希望每当用户点击zedGraph区域时,我们就会得到用户点击的坐标,并在点击的坐标上绘制一条直线。

我使用了FindNearestObject方法的MouseCLick事件alren,如下所示:

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搜索一些曲线并找到最近的点,然后绘制线条,但我希望在用户点击的地方绘制线条。有没有办法这样做?

1 个答案:

答案 0 :(得分:5)

您可以尝试以下代码,这将为鼠标点击事件绘制一条垂直线。

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();
    }

enter image description here

您只需更改userClickList即可绘制水平线。

快乐编码..... :)