如何从所有序列的Y值中获取特定的X值(OxyPlot)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从所有序列的Y值中获取特定的X值(OxyPlot)相关的知识,希望对你有一定的参考价值。

我正在使用OxyPlot。我有一个带有PlotModel的PlotView。有2个轴:yAxis是LinearAxis,xAxis是DateTimeAxis。我有多个LineSeries。这是一个示例程序:

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);
        

当我按住鼠标左键时,我只能从我当前徘徊的LineSeries中看到Y值。我想获取与鼠标指针当前位于的X值相对应的所有Y值。我不需要自定义跟踪器,我想在绘图旁边的标签中显示值。还值得注意的是,这只是一个示例程序。在我的主程序中,LineSeries每个包含大约5万个点,这就是为什么我不能仅遍历所有点来查找所需的值的原因。最有效的方法是什么?

答案

您可以为每个系列使用GetNearestPoint来获取给定X的Y值。例如,

        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);
        ;

以上是关于如何从所有序列的Y值中获取特定的X值(OxyPlot)的主要内容,如果未能解决你的问题,请参考以下文章

从Hashmap的所有值中获取所有嵌套项

如何从嵌套的 json 值中提取特定值。?

Python 语法:如何检查返回值中是不是存在“x”?

如何从python中的图像中获取特定像素(蓝色)的x,y坐标?

如何在 Python 的列表值中添加特定字符?

如何从字符串值中获取纬度?