Я пытаюсь построить график своих значений, я получаю их правильно, я передаю их в xData и yData, но по какой-то причине они не визуализируются, в чем проблема?
Я также передаю конструкторы .Я получаю их из udp.
Я установил 2 графика, но теперь пытаюсь перейти ко второму.
Я использую WPF с C#.
Я использую WPF с C#.
private async Task UpdateAEPlotAsync(string ipAddress, int port)
{
using (var udpClient = new UdpClient())
{
_cancellationTokenSource = new CancellationTokenSource();
var token = _cancellationTokenSource.Token;
udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpClient.Client.Bind(new IPEndPoint(IPAddress.Parse(ipAddress), port));
while (!_isPaused && !token.IsCancellationRequested)
{
if (_stopAll) break;
try
{
var result = await udpClient.ReceiveAsync();
if (result.Buffer.Length == 0) continue;
var receivedData = result.Buffer;
if (receivedData.Length >= 65507)
{
Debug.Print("Warning: Received packet may be truncated.");
}
Debug.Print(receivedData.Length.ToString());
string message = Encoding.UTF8.GetString(receivedData);
//Debug.Print($"Received data: {message}");
var jsonData2 = JsonConvert.DeserializeObject(message);
Debug.Print("JSON Timeline: " + jsonData2.Timeline[1]);
Debug.Print("JSON OffsetData: " + jsonData2.OffsetData[1]);
Debug.Print("JSON Timeline Length: " + jsonData2.Timeline.Length.ToString());
Debug.Print("JSON OffsetData Length: " + jsonData2.OffsetData.Length.ToString());
if (jsonData2 == null || jsonData2.Timeline == null || jsonData2.OffsetData == null)
{
Debug.Print("Failed to parse JSON or data is missing.");
return;
}
double[] xData = jsonData2.Timeline;
double[] yData = jsonData2.OffsetData;
if (xData == null || xData.Length == 0 || yData == null || yData.Length == 0)
{
Debug.Print("xData or yData is null or empty.");
return;
}
await Task.Run(() => UpdatePlotData2(xData,yData));
}
catch (Exception ex)
{
Debug.Print($"Error receiving UDP packet: {ex.Message}");
break;
}
}
}
}
SpectralAEPlotModel = new PlotModel
{
Title = "AE",
Background = OxyColors.Transparent,
TextColor = OxyColors.White,
PlotAreaBorderColor = OxyColors.White
};
SpectralAEPlotModel.Axes.Add(new LinearAxis
{
Position = AxisPosition.Bottom,
Title = "Time",
TitleColor = OxyColors.White,
AxislineColor = OxyColors.White,
TextColor = OxyColors.White,
TicklineColor = OxyColors.White,
StringFormat = "0.###",
});
SpectralAEPlotModel.Axes.Add(new LinearAxis
{
Position = AxisPosition.Left,
Title = "Amplitude",
TitleColor = OxyColors.White,
AxislineColor = OxyColors.White,
TextColor = OxyColors.White,
TicklineColor = OxyColors.White,
StringFormat = "0.###",
});
SpectralAEPlotModel.Series.Add(new LineSeries
{
Title = "AE Data",
Color = OxyColors.White,
MarkerFill = OxyColors.White,
MarkerType = MarkerType.None,
LineStyle = LineStyle.Solid,
StrokeThickness = 2,
});
public PlotModel SpectralAEPlotModel { get; set; }
public class PlotValues2
{
public double[] Timeline { get; set; }
public double[] OffsetData { get; set; }
}
private void UpdatePlotData2(double[] xData,double[] yData)
{
var series = (LineSeries)SpectralAEPlotModel.Series[0];
Application.Current.Dispatcher.Invoke(() =>
{
series.Points.Clear();
for (int i = 0; i < Math.Min(xData.Length, yData.Length); i++)
{
series.Points.Add(new DataPoint(xData, yData));
//Debug.Print(xData.ToString());
//Debug.Print(yData.ToString());
Debug.Print(i.ToString());
}
Debug.Print("xData Length: " + xData.Length);
Debug.Print("yData Length: " + yData.Length);
Debug.Print($"Added {series.Points.Count} points to the series.");
// Adjust the axis ranges to match the data
SpectralAEPlotModel.InvalidatePlot(true);
});
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... r-my-graph