DataSeriesExportingAll.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Threading;
  5. using Aitex.UI.Charting.Model;
  6. using Aitex.UI.Charting.ViewModel;
  7. using DataAnalysisControl.Core;
  8. namespace Aitex.UI.Charting.View
  9. {
  10. /// <summary>
  11. /// Interaction logic for DataSourceExportingDialog.xaml
  12. /// </summary>
  13. public partial class DataSeriesExportingAll : Window
  14. {
  15. public DataSeriesExportingAll(string fileName)
  16. {
  17. InitializeComponent();
  18. _viewModel = CommonViewModel.Instance;
  19. _lines = _viewModel.GetAllDataLineSeries();
  20. if (_lines == null)
  21. {
  22. MessageBox.Show(string.Format(DataAnalysisControl.Properties.Resource.DataSeriesExportingAll_DataSeriesExportingAll_No_data));
  23. }
  24. else
  25. {
  26. txt_Info.Content = string.Format(DataAnalysisControl.Properties.Resource.DataSeriesExportingAll_DataSeriesExportingAll_exporting);
  27. txt_Completed.Content = "50%";
  28. progressBar1.Value = 50;
  29. _archieveFilePath = fileName;
  30. _thread = new Thread(new ThreadStart(Task_Export));
  31. _thread.Name = "data analysis export all thread";
  32. _thread.IsBackground = true;
  33. _thread.Start();
  34. }
  35. }
  36. CommonViewModel _viewModel;
  37. string _archieveFilePath;
  38. Thread _thread;
  39. List<MyLineSeries> _lines;
  40. private void Task_Export()
  41. {
  42. try
  43. {
  44. List<DataItem> datas;
  45. List<string> dataNames = new List<string>();
  46. foreach (MyLineSeries _line in _lines)
  47. {
  48. dataNames.Add(_line.SeriesName);
  49. }
  50. var source = _lines[0].DataSource;
  51. var ret = source.GetData(dataNames, source.BeginTime, source.EndTime, out datas);
  52. if (ret)
  53. {
  54. using (System.IO.StreamWriter fs = new System.IO.StreamWriter(_archieveFilePath))
  55. {
  56. fs.Write("Time");
  57. foreach (DataItem data in datas)
  58. {
  59. fs.Write(",");
  60. fs.Write(data.DataName);
  61. }
  62. fs.WriteLine("");
  63. for (int i = 0; i < datas[0].RawData.Count; i++)
  64. {
  65. fs.Write(datas[0].TimeStamp[i].ToString("yyyy/MM/dd HH:mm:ss"));
  66. foreach (DataItem data in datas)
  67. {
  68. fs.Write(",");
  69. fs.Write(data.RawData[i].ToString());
  70. }
  71. fs.WriteLine("");
  72. }
  73. }
  74. MessageBox.Show(string.Format(DataAnalysisControl.Properties.Resource.DataSeriesExportingAll_Task_Export_exportto, _archieveFilePath), DataAnalysisControl.Properties.Resource.DataSeriesExportingAll_Task_Export_data_export,
  75. MessageBoxButton.OK, MessageBoxImage.Information);
  76. }
  77. else
  78. {
  79. MessageBox.Show(string.Format(DataAnalysisControl.Properties.Resource.DataSeriesExportingAll_Task_Export_export_error), DataAnalysisControl.Properties.Resource.DataSeriesExportingAll_Task_Export_data_export, MessageBoxButton.OK, MessageBoxImage.Error);
  80. }
  81. }
  82. catch (Exception ex)
  83. {
  84. CONTEXT.WriteLog(ex, string.Format("导出数据发生异常"));
  85. MessageBox.Show(string.Format(DataAnalysisControl.Properties.Resource.DataSeriesExportingDialog_Task_Export_export_error, ex.Message), DataAnalysisControl.Properties.Resource.DataSeriesExportingAll_Task_Export_data_export, MessageBoxButton.OK, MessageBoxImage.Error);
  86. }
  87. finally
  88. {
  89. this.Dispatcher.BeginInvoke(new Action(() => { Close(); }));
  90. }
  91. }
  92. private void btnAbort_Click(object sender, RoutedEventArgs e)
  93. {
  94. if(_thread != null)
  95. _thread.Abort();
  96. Close();
  97. }
  98. }
  99. }