【问题标题】:Drawing line on a click on ZedGraph Pane在 ZedGraph 窗格上单击画线
【发布时间】:2012-09-14 10:09:14
【问题描述】:

我对 ZedGraph 的要求有点不同。

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

我已将 MouseCLick 事件与 FindNearestObject 方法一起使用,如下所示:

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

【问题讨论】:

    标签: c# zedgraph


    【解决方案1】:

    你可以试试下面的代码,它会为鼠标点击事件画一条垂直线。

    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 即可绘制水平线。

    快乐编码.....:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      相关资源
      最近更新 更多