简体   繁体   中英

Get coordinates of a range using OxyPlot

I would like to read the start and end coordinates from a OxyPlot PlotView graph. The following is a code snippet from the ViewModel.

plotModel.MouseDown += (s1, e1) =>
{
    if (e1.ChangedButton == OxyMouseButton.Middle)
    {            
        plotModel.MouseUp += (s2, e2) =>
        {
             if (e2.IsControlDown)
             {
                 DateTime xstart = DateTime.FromOADate(xAxis.InverseTransform(e1.Position.X, e1.Position.Y, yAxis).X);
                 double ystart = yAxis.InverseTransform(e1.Position.X, e1.Position.Y, yAxis).Y;
                 DateTime xend = DateTime.FromOADate(xAxis.InverseTransform(e2.Position.X, e2.Position.Y, yAxis).X);
                 double yend = yAxis.InverseTransform(e2.Position.X, e2.Position.Y, yAxis).Y;
             }
         };
     } 
};

The problem is that the value I get for xstart and ystart is not correct, it seems like the value for y is just a random number, while the x value is somewhat acceptable. xend and yend are correct. Is there something I should change in the code or is there some alternative way to approach this?

I use the middle button since it highlights the selected area, but I couldn´t find a way to simply get the coordinates of the area.

There are several ways to implement this. You might have noticed that the events of the plot model are tagged as obsolete but they won't get removed in the near future because it is not yet clear how to replace them if a client needs to access information about internal changes from the outside (see here ).

So this is the easy way to implement it event-based using a private field for the mouse down point:

    /// <summary>
    /// Constructor
    /// </summary>
    public MyClass()
    {
        ...
        var plotModel = new PlotModel();
        plotModel.MouseDown += PlotModel_MouseDown;
        plotModel.MouseUp += PlotModel_MouseUp;
    }

    private ScreenPoint mouseDownPoint;

    private void PlotModel_MouseDown(object sender, OxyMouseDownEventArgs e)
    {
        if (e.ChangedButton == OxyMouseButton.Middle)
        {
            mouseDownPoint = e.Position;
        }
    }

    private void PlotModel_MouseUp(object sender, OxyMouseEventArgs e)
    {
        if (e.IsControlDown)
        {
            ScreenPoint mouseUpPoint = e.Position;

            // assuming your x-axis is at the bottom and your y-axis is at the left.
            Axis xAxis = plotModel.Axes.FirstOrDefault(a => a.Position == AxisPosition.Bottom);
            Axis yAxis = plotModel.Axes.FirstOrDefault(a => a.Position == AxisPosition.Left);

            DateTime xstart = DateTime.FromOADate(xAxis.InverseTransform(mouseDownPoint.X));
            double ystart = yAxis.InverseTransform(mouseDownPoint.Y);
            DateTime xend = DateTime.FromOADate(xAxis.InverseTransform(mouseUpPoint.X));
            double yend = yAxis.InverseTransform(mouseUpPoint.Y);
        }
    }

Note that I have also used the simpler overloads of the transformation methods.

The other way would be to derive a class from the 'ZoomRectangleManipulator' class and override it's 'Completed' method but in this case I don't know how you would access the calculated values from the outside. So whether this approach makes sense for you depends on what you are trying to achieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM