wpf oxyPlot 峰值画线标注

 public ObservableCollection<DataPoint> dpList { get; set; } = new ObservableCollection<DataPoint>();
        public ObservableCollection<DataPoint> ReferenceDpList { get; set; } = new ObservableCollection<DataPoint>();

void CallbackFunction(IntPtr tick, int length)
        {
            try
            {
                if (length <= 0) { return; }
                double X = 190;
                dpList.Clear();
                ReferenceDpList.Clear();

                List<double> dList = new List<double>();
                int c = 9102 * 3 + 7;//((1100-190)/0.2+1)*2
                for (int i = 7; i < c; i++)
                {
                    int Y = Marshal.ReadByte(tick, i) * 65536 + Marshal.ReadByte(tick, i + 1) * 256 + Marshal.ReadByte(tick, i + 2);
                    dList.Add(Y);

                    if (i <= 13657) dpList.Add(new DataPoint(X, Y));
                    else { ReferenceDpList.Add(new DataPoint(X, Y)); }

                    if (X == double.Parse("1100"))
                    {
                        X = 190;
                    }
                    else
                    {
                        X = Convert.ToDouble((X + 0.2).ToString("N2")); //保留2位小数
                    }
                    i += 2;
                }

                Thread.Sleep(500);
                PlotModel plotModel = new PlotModel { PlotType = PlotType.Cartesian, PlotAreaBorderThickness = new OxyThickness(1, 0, 0, 1), };
                var l = new Legend
                {
                    LegendPosition = LegendPosition.TopLeft,
                    LegendOrientation = LegendOrientation.Horizontal,
                    LegendPlacement = LegendPlacement.Outside
                };

                plotModel.Legends.Add(l);
                var linearAxis1 = new OxyPlot.Axes.LinearAxis()
                {
                    MajorGridlineStyle = LineStyle.Solid,
                    MinorGridlineStyle = LineStyle.Dot,
                    Position = OxyPlot.Axes.AxisPosition.Left,
                    Title = Util.FindKey("Result_Absorbance"),
                    Minimum = dList.Min(),
                    Maximum = dList.Max() + (dList.Max() - dList.Min()) / 25,
                    AbsoluteMinimum = dList.Min(),
                    AbsoluteMaximum = dList.Max() + (dList.Max() - dList.Min()) / 25,
                    MajorStep = (dList.Max() - dList.Min()) / 5,
                    IsZoomEnabled = false,//坐标轴缩放关闭
                    IsPanEnabled = false,//图表缩放功能关闭
                };
                plotModel.Axes.Add(linearAxis1);

                var linearAxis2 = new OxyPlot.Axes.LinearAxis()
                {
                    MajorGridlineStyle = LineStyle.Solid,
                    MinorGridlineStyle = LineStyle.Dot,
                    Position = OxyPlot.Axes.AxisPosition.Bottom,
                    Minimum = 190,
                    Maximum = 1100.2,
                    AbsoluteMaximum = 1100.2,
                    AbsoluteMinimum = 190,
                    MajorStep = (1100 - 190) / 5,
                    IsZoomEnabled = false,//坐标轴缩放关闭
                    IsPanEnabled = false,//图表缩放功能关闭
                };
                plotModel.Axes.Add(linearAxis2);       

                if (dpList?.Count > 0)
                {
                    var series3 = new LineSeries
                    {
                        MarkerType = MarkerType.None,
                        CanTrackerInterpolatePoints = false,
                        Title = Util.FindKey("Maintain_LightIntensity"),
                    };
                    List<DataPoint> P_229 = new List<DataPoint>();
                    List<DataPoint> P_484 = new List<DataPoint>();
                    List<DataPoint> P_882 = new List<DataPoint>();

                    foreach (var item in dpList)
                    {
                        if (item.X >= 219 && item.X <= 239)
                        {
                            P_229.Add(item);
                        }
                        else if (item.X >= 474 && item.X <= 494)
                        {
                            P_484.Add(item);
                        }
                        else if (item.X >= 872 && item.X <= 892)
                        {
                            P_882.Add(item);
                        }
                        series3.Points.Add(item);
                    }
                    var YMin = dpList.Min(a => a.Y) + 100;
                    if (P_229?.Count > 0)
                    {
                        double y = P_229.Max(a => a.Y);
                        double x = P_229.FirstOrDefault(b => b.Y == y).X;
                        LineAnnotation annoHorizontal = new LineAnnotation();
                        annoHorizontal.Type = LineAnnotationType.Vertical;
                        annoHorizontal.X = x;
                        annoHorizontal.Text = "X:" + x + "\tY:" + y;
                        annoHorizontal.TextLinePosition = 0.01;
                        annoHorizontal.TextOrientation = AnnotationTextOrientation.Horizontal;
                        annoHorizontal.TextHorizontalAlignment = OxyPlot.HorizontalAlignment.Left;
                        plotModel.Annotations.Add(annoHorizontal);
                    }
                    if (P_484?.Count > 0)
                    {
                        double y = P_484.Max(a => a.Y);
                        double x = P_484.FirstOrDefault(b => b.Y == y).X;
                        LineAnnotation annoHorizontal = new LineAnnotation();
                        annoHorizontal.Type = LineAnnotationType.Vertical;
                        annoHorizontal.X = x;
                        annoHorizontal.Text = "X:" + x + "\tY:" + y;
                        annoHorizontal.TextLinePosition = 0.01;
                        annoHorizontal.TextOrientation = AnnotationTextOrientation.Horizontal;
                        annoHorizontal.TextHorizontalAlignment = OxyPlot.HorizontalAlignment.Left;
                        plotModel.Annotations.Add(annoHorizontal);
                    }
                    if (P_882?.Count > 0)
                    {
                        double y = P_882.Max(a => a.Y);
                        double x = P_882.FirstOrDefault(b => b.Y == y).X;
                        LineAnnotation annoHorizontal = new LineAnnotation();
                        annoHorizontal.Type = LineAnnotationType.Vertical;
                        annoHorizontal.X = x;
                        annoHorizontal.Text = "X:" + x + "\tY:" + y;
                        annoHorizontal.TextLinePosition = 0.01;
                        annoHorizontal.TextOrientation = AnnotationTextOrientation.Horizontal;
                        annoHorizontal.TextHorizontalAlignment = OxyPlot.HorizontalAlignment.Left;
                        plotModel.Annotations.Add(annoHorizontal);
                    }

                    plotModel.Series.Add(series3);//将线添加到图标的容器中
                }
                if (ReferenceDpList?.Count > 0)
                {
                    var series4 = new LineSeries
                    {
                        MarkerType = MarkerType.None,
                        CanTrackerInterpolatePoints = false,
                        Title = Util.FindKey("Maintain_Reference"),
                    };
                    foreach (var item in ReferenceDpList)
                    {
                        series4.Points.Add(new DataPoint(item.X, item.Y));
                    }
                    plotModel.Series.Add(series4);//将线添加到图标的容器中
                }
                System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    UcSettingsContentView.view.pv.Model = plotModel;
                    if (WaveLength > 0)
                    {
                        var model = dpList.FirstOrDefault(x => x.X == WaveLength);
                        ABS = model.Y.ToString();
                    }
                    WeakReferenceMessenger.Default.Send("Close");
                }));
            }
            catch (Exception ex)
            {
                WeakReferenceMessenger.Default.Send("Close");
                LogUtil.LogError(ex);
            }
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值