我想知道如何(具体而言),仅是我使用OxyPlot绘制的散点图的x坐标。

//user clicks on graph line data...
//x-coordinate gets assigned to variable
int x = ...


我正在使用winforms。

编辑:

   private void plotView_Click(object sender, EventArgs e){
        plotView.ActualModel.Series[0].MouseDown += (s, e0) =>
        {
            if (e0.ChangedButton != OxyMouseButton.Left)
                return;
            else
                pointx = (int)e0.HitTestResult.NearestHitPoint.X;
        };
    }


工作代码:

        s0.MouseDown += (s, e0) =>
        {
            if (e0.ChangedButton == OxyMouseButton.Left)
            {
                var item = e0.HitTestResult.Item as ScatterPoint;
                if (item != null)
                {
                    pointx = (int)item.X;
                }
            }
        };

最佳答案

您可以为系列添加鼠标按下事件,如下所示:

var model = new PlotModel { Title = "Test Mouse Events" };

var s1 = new LineSeries();
model.Series.Add(s1);

double x;

s1.MouseDown += (s, e) =>
            {
                x = e.Position.X;
            };


改编自此处的示例代码:https://github.com/oxyplot/oxyplot/blob/09fc7c50e080f702315a51af57a70d7a47024040/Source/Examples/ExampleLibrary/Examples/MouseEventExamples.cs

并在此处演示:http://resources.oxyplot.org/examplebrowser/ =>向下滚动到Mouse Events

编辑:我发现您从x获得的位置是屏幕坐标,您必须将其转换为找到正确的轴点,如下所示:

x = (s as LineSeries).InverseTransform(e0.Position).X;

10-06 07:10