2015-10-17 34 views
2

我想知道如何获得(具体),只是我使用OxyPlot绘制的散点图的x坐标。从OxyPlot ScatterPlot获取数据点

//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; 
       } 
      } 
     }; 

回答

1

您可以将鼠标按下事件为您的系列,像这样:

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/ =>向下滚动鼠标事件

编辑:我发现你从X得到的位置是屏幕坐标,你必须转换为找到正确的轴点,像这样:

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

虽然我需要在这个鼠标点击事件中,当我这样做时,我得到一个错误,说'e'不能在此范围内声明,因此我将它重命名为'e0'。当我这样做时,我总是得到一个零。我该如何解决? – John

+0

也许试着让int x成为double值,这样它就不会将任何double值截断为0.除此之外,您可以编辑您的代码,我可以看看您正在做什么。 – Zachary

+0

哦,如果你正在寻找点击的实际坐标(而不是像我可能假设的点),请执行:“e0.Position.X” – Zachary