我在这里想要完成的是我想从一个 CSV 文件中获取一些数据,这些数据我从一个文件夹中复制并放入一个临时文件夹中(所以我不会篡改原始文件)。
然后我想从 CSV 文件中读取所有数据,并使用 Oxyplot 中的 Scatter Series 在图表上绘制最后 2000 个数据点(我希望它每 200 毫秒检查一次新数据,所以我使用了调度程序计时器)。我遇到的问题是情节的前几次更新看起来很棒,并且它完全按照我想要的方式绘制......但是,在可能 12 次更新之后,图表没有更新,或者更新非常缓慢导致 UI 变成反应迟钝。
下面是我的这部分项目的代码......
public MainWindow()
{
viewModel = new View_Model.MainWindowModel();
DataContext = viewModel;
CompositionTarget.Rendering += CompositionTargetRendering;
InitializeComponent();
}
private void AutoUpdateGraph() //begins when a check box IsChecked = true
{
sw.Start();
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 200);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
SleeveData sd = new SleeveData(); //class to deal with the data
FileManagement fm = new FileManagement(); //class to manage the files
string date = fm.GetDate(); //get the current date to use in finding the
//most recent CSV file
_newPath = fm.InitialFileSetup(date); //create a new path for the temp file
fm.RewriteFile(_newPath); //write the temp file to the temp path
if (fm.IsFileLocked(_newPath) == false) //checking if the file is being written to
{
IEnumerable<SleeveData> newSD = sd.GetMostRecentCSVData(_newPath); //getting the latest data from the temp file
viewModel.LoadData(newSD); //updating the data on the graph
}
}
这是 MainWindowModel 中的 LoadData 方法...
public void LoadData(IEnumerable<SleeveData> newData)
{
var scatterSeries1 = new OxyPlot.Series.ScatterSeries
{
MarkerSize = 3,
Title = string.Format("Sleeve Data"),
};
int j = 0;
var zeroBuffer = new List<float>(new float[2000]);
var fDiaDataBuffer = zeroBuffer.Concat((newData.Select(x => x.fDiameter).ToList())).ToList();
var iDiaDataBuffer = zeroBuffer.Concat((newData.Select(x => x.IDiaMax).ToList())).ToList();
for (int i = fDiaDataBuffer.Count - 2000; i <= fDiaDataBuffer.Count - 1; i++)
{
scatterSeries1.Points.Add(new ScatterPoint(j, fDiaDataBuffer[i]));
j++;
}
PlotModel.Series.Clear();
PlotModel.Series.Add(scatterSeries1);
}
我正在做一些时髦的事情来获得最新的 2000 分并将它们添加到图表中。
也许我需要考虑一个后台工作人员?
我可能会做错这一切,如果我是,我很想知道我应该往哪个方向走!我对这么大的项目和必须实时运行的项目编码相当陌生。请对我放轻松:)并在此先感谢。