5

I am trying to plot some circles on a scatter plot via:

<Grid>
    <oxy:PlotView x:Name="PlotView" Title="{Binding Title}" >
        <oxy:PlotView.Axes>
            <oxy:LinearAxis Position="Bottom" Minimum="-30" Maximum="30" IsAxisVisible="False" IsZoomEnabled="False" IsPanEnabled="False" />
            <oxy:LinearAxis Position="Left" Minimum="0" Maximum="35" IsAxisVisible="False" IsZoomEnabled="False" IsPanEnabled="False"/>
        </oxy:PlotView.Axes>
        <oxy:PlotView.Series>
            <oxy:ScatterSeries Height="100" Width="100" ItemsSource="{Binding Points}" MarkerType="Circle" />
        </oxy:PlotView.Series>
    </oxy:PlotView>
</Grid>

I cannot figure out how to enable some sort of click handler to have an event fired when a user clicks on a DataPoint.

Examaple:

User clicks DataPoint at (X, Y) = (0, 5), I would like to fire an event so I can handle the click of that point.

Is this possible with OxyPlot? I am currently investigating the Tracker to see if its possible that route, but starting to run out of ideas.

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
Matt
  • 619
  • 3
  • 8
  • 23
  • have you tried `plotView.MouseDown` event? – kennyzx Jan 17 '15 at 16:03
  • I have. I am able to get the x and y position of the mouse, but it is not relative to the actual plot. – Matt Jan 17 '15 at 16:06
  • 2
    I have not tried this, but `InverseTransform` is used to translate mouse coordinates to plot coordinates. – kennyzx Jan 17 '15 at 16:15
  • kenny: can you add that as an Answer so I can mark it as the correct one? Looks like that's what I needed to move forward! – Matt Jan 17 '15 at 17:25

3 Answers3

11

PlotView has defined mouse events, from which you can get the mouse coordinates, and InverseTransform is used to translate mouse coordinates to plot coordinates.

Example:

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

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

double x;

s1.MouseDown += (s, e) =>
{
    x = (s as LineSeries).InverseTransform(e.Position).X;
};
Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
kennyzx
  • 12,845
  • 6
  • 39
  • 83
2

I couldn't get the accepted answer to work. The MouseDown handler wouldn't receive events when the plot was left-clicked. It would, however, receive events for right-clicks and double-clicks.

My workaround is to listen for PreviewMouseDown on the PlotView.

Nora Powers
  • 1,597
  • 13
  • 31
  • `MouseDown` doesn't receive events because you likely have a custom series and don't override `TrackerHitResult GetNearestPoint(ScreenPoint point, bool interpolate)`. I had the same issue, but once I added an override for that method to just return the InverseTransform - everything worked. – Dmitry Avtonomov Oct 06 '20 at 00:21
1

The series' MouseDown events are deprecated since v4.0. What you can do now is add a TrackerManipulator and catch the mouse down events there:

public class DoSomethingWithPointTrackerManipulator : OxyPlot.TrackerManipulator
{
  public DoSomethingWithPointTrackerManipulator (IPlotView plotView) : base(plotView)
  {
    Snap = true;
    PointsOnly = true;
  }

  public override void Started(OxyMouseEventArgs e)
  {
    base.Started(e);
    DoSomethingWithPoint(e);
  }

  private void DoSomethingWithPoint(OxyMouseEventArgs e)
  {
    var series = PlotView.ActualModel.GetSeriesFromPoint(e.Position) as ScatterSeries; // Or other type of series...
    if (series == null) return;

    var nearestPoint = series.GetNearestPoint(e.Position, false); // Possible set interpolate parameter to true.
    if (nearestPoint == null) return;

    var dataPoint = nearestPoint.DataPoint;
    var scatterPoint = series.Points.FirstOrDefault(x => Equals(x.X, dataPoint.X) && Equals(x.Y, dataPoint.Y));

  // Do whatever you want with the point.
  }

  public override void Completed(OxyMouseEventArgs e)
  {
    // Do nothing to avoid closing the tracker.
  }
}

Then add this to your PlotController:

var mouseButton = OxyMouseButton.Left // Or whatever button you want the event to be fired with.
yourPlotController.BindMouseDown(mouseButton, 
    new DelegatePlotCommand<OxyMouseDownEventArgs>((view, controller, args) =>
      controller.AddMouseManipulator(view, new DoSomethingWithPointTrackerManipulator (view), args)));
Anno
  • 761
  • 1
  • 10
  • 22