Im trying to render a vertical red line from the bottom to top of my y axis , i have a specific wavelength in the x axis so when the user use the slider it will stop and take that specific wavelength.
Here is my code :
public PlotViewModel()
{
InitializeRenderingLoop();
// Initialize SpectralPlotModel
SpectralPlotModel = new PlotModel
{
Title = "Data from Socket",
Background = OxyColors.Transparent,
TextColor = OxyColors.White,
PlotAreaBorderColor = OxyColors.White
};
SpectralPlotModel.Axes.Add(new LinearAxis
{
Position = AxisPosition.Bottom,
Title = "Wavelength",
TitleColor = OxyColors.White,
AxislineColor = OxyColors.White,
TextColor = OxyColors.White,
StringFormat = "0.###",
});
SpectralPlotModel.Axes.Add(new LinearAxis
{
Position = AxisPosition.Left,
Title = "Data",
TitleColor = OxyColors.White,
AxislineColor = OxyColors.White,
TextColor = OxyColors.White,
StringFormat = "0.###",
Minimum = 0,
Maximum = 5
});
SpectralPlotModel.Series.Add(new LineSeries
{
Title = "Data",
Color = OxyColors.White,
MarkerFill = OxyColors.White,
MarkerType = MarkerType.None,
LineStyle = LineStyle.Solid,
StrokeThickness = 2
});
SpectralPlotModel.Series.Add(new LineSeries
{
Title = "Red Line",
Color = OxyColors.Red,
MarkerFill = OxyColors.Red,
MarkerType = MarkerType.Cross,
LineStyle = LineStyle.Solid,
StrokeThickness = 15,
});
}
and the function logic :
public double WavelengthSliderValue
{
get => _wavelengthSliderValue;
set
{
if (_wavelengthSliderValue != value)
{
_wavelengthSliderValue = value;
OnPropertyChanged();
UpdateRedLine();
}
}
}
public void UpdateRedLine()
{
if (SpectralPlotModel != null && SpectralPlotModel.Series.Count > 1)
{
var redLineSeries = SpectralPlotModel.Series[1] as LineSeries;
if (redLineSeries != null)
{
var selectedWavelength = WavelengthSliderValue;
Debug.Print("Wavelength : " + selectedWavelength);
// Clear existing points
redLineSeries.Points.Clear();
// Ensure the axis limits are correct
var yAxis = SpectralPlotModel.Axes[1];
redLineSeries.Points.Add(new DataPoint(selectedWavelength, yAxis.Minimum)); // Min Y
redLineSeries.Points.Add(new DataPoint(selectedWavelength, yAxis.Maximum)); // Max Y
SpectralPlotModel.InvalidatePlot(true);
}
else
{
Debug.Write("No red line series found");
}
}
}
My xaml files (Im using MVVM) :
<Border Grid.Row="0">
<oxy:PlotView Name="SpectralPlot"
Width="Auto"
Height="Auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Transparent"
Model="{Binding SpectralPlotModel}" />
<Slider Minimum="1546"
Maximum="1558"
Value="{Binding WavelengthSliderValue}"
VerticalAlignment="Center"
HorizontalAlignment="Stretch"
Margin="10,0,10,0" />
Inside UpdateRedLine i take correctly the Wavelength so the number i take is fine but for some reason it does not render my red line at all .
Do i set it wrong?
Thanks .
5