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