ChangeFactorOffsetCommand.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Input;
  6. using Aitex.UI.Charting.ViewModel;
  7. using System.Windows;
  8. using Aitex.UI.Charting.Model;
  9. using Abt.Controls.SciChart;
  10. using Abt.Controls.SciChart.Model.DataSeries;
  11. using DataAnalysisControl.Core;
  12. namespace Aitex.UI.Charting.Command
  13. {
  14. public class ChangeFactorOffsetCommand : ICommand
  15. {
  16. public ChangeFactorOffsetCommand(string uniqueDataId, double newFactor, double newOffset)
  17. {
  18. _newFactor = (float)newFactor;
  19. _newOffset = (float)newOffset;
  20. _uniqueDataId = uniqueDataId;
  21. }
  22. string _uniqueDataId = "";
  23. float _newFactor = 1;
  24. float _newOffset = 0;
  25. public bool CanExecute(object parameter)
  26. {
  27. return true;
  28. }
  29. #pragma warning disable 0067
  30. public event EventHandler CanExecuteChanged;
  31. #pragma warning restore 0067
  32. private void AsyncShowWarningMessageBox(string message, string title)
  33. {
  34. Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Warning)));
  35. }
  36. public void Execute(object parameter)
  37. {
  38. string reason = string.Empty;
  39. try
  40. {
  41. var commonViewModel = (CommonViewModel)parameter;
  42. var myLine = commonViewModel.GetDataLineSeries(_uniqueDataId);
  43. if (myLine == null)
  44. {
  45. reason = string.Format("数据名称{0}不存在", _uniqueDataId);
  46. AsyncShowWarningMessageBox(reason, "警告");
  47. }
  48. else
  49. {
  50. var dataList = new List<float>();
  51. var timeList = new List<DateTime>();
  52. for (int i = 0; i < myLine.Points.RawData.Count; i++)
  53. {
  54. dataList.Add(myLine.Points.RawData[i] * _newFactor + _newOffset);
  55. timeList.Add(myLine.Points.TimeStamp[i] + myLine.DataSource.TimeMove);
  56. }
  57. var dataSet = myLine.DataSeries as XyDataSeries<DateTime, float>;
  58. if (dataSet != null && dataList.Count > 0)
  59. {
  60. dataSet.Clear();
  61. dataSet.Append(timeList, dataList);
  62. }
  63. commonViewModel.InvokePropertyChanged();
  64. }
  65. }
  66. catch (Exception ex)
  67. {
  68. reason = ex.Message;
  69. CONTEXT.WriteLog(ex, string.Format("改变数据{0}的缩放因子及偏差值发生异常", _uniqueDataId));
  70. AsyncShowWarningMessageBox(reason, "警告");
  71. }
  72. }
  73. }
  74. }