0

I'm using OxyPlot. I have a PlotView with a PlotModel. There are 2 axes: yAxis which is a LinearAxis, and xAxis which is a DateTimeAxis. I have multiple LineSeries. Here is a sample program:

public MainWindow()
        {
            InitializeComponent();

            PlotModel plotModel = new PlotModel();
            plotView.Model = plotModel;

            plotModel.Axes.Clear();
            var yAxis = new LinearAxis();
            var xAxis = new DateTimeAxis();
            yAxis.IsZoomEnabled = false;
            yAxis.AbsoluteMinimum = -50;
            yAxis.AbsoluteMaximum = 450;
            yAxis.MajorGridlineStyle = LineStyle.Solid;
            xAxis.MajorGridlineStyle = LineStyle.Solid;
            xAxis.AbsoluteMinimum = DateTimeAxis.ToDouble(DateTime.Today);
            xAxis.AbsoluteMaximum = DateTimeAxis.ToDouble(DateTime.Today.AddDays(1));
            yAxis.IsPanEnabled = false;
            yAxis.IsZoomEnabled = false;
            plotModel.Axes.Add(yAxis);
            plotModel.Axes.Add(xAxis);

            var series1 = new LineSeries();
            series1.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Today), 0));
            series1.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Today.AddHours(4)), 100));
            series1.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Today.AddHours(8)), 150));
            series1.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Today.AddHours(12)), 50));
            series1.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Today.AddHours(16)), 200));
            series1.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Today.AddHours(20)), 150));
            series1.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Today.AddHours(24)), 300));
            plotModel.Series.Add(series1);

            var series2 = new LineSeries();
            series2.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Today), 0));
            series2.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Today.AddHours(4)), 200));
            series2.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Today.AddHours(8)), 200));
            series2.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Today.AddHours(12)), 150));
            series2.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Today.AddHours(16)), 300));
            series2.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Today.AddHours(20)), 50));
            series2.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Today.AddHours(24)), 100));
            plotModel.Series.Add(series2);
        }

When I hold left click I can see the Y value only from the LineSeries I'm currently hovering. I want to get all the Y values that correspond to the X value my mouse pointer is currently at. I do not need a custom tracker, I want to show the values in a label next to the plot. It is also worth noting that this is only a sample program; in my main program the LineSeries contain around 50k points each, which is why I can't just loop through all points to find the values I want. What is the most efficient way of doing this?

Log Dog
  • 117
  • 7

1 Answers1

1

You could use GetNearestPoint for each series to fetch values of Y for the given X. For example,

        PlotModel.TrackerChanged += (sender, eventArgs) =>
        {
            if (eventArgs.HitResult != null)
            {
                var currentPoint = eventArgs.HitResult.Position;
                var allYValuesForX = PlotModel.Series.Select(x => x.GetNearestPoint(currentPoint, true).DataPoint.Y);
                CurrentTrackerValue = $"X = {currentPoint.X}, Y ={string.Join(",", allYValuesForX)}";
            }

            UpdateLabel(CurrentTrackerValue);
        };
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51