AddDataSeriesCommand.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 System.Windows.Media;
  11. using Abt.Controls.SciChart.Model.DataSeries;
  12. using Aitex.UI.Charting.View;
  13. using DataAnalysisControl.Core;
  14. namespace Aitex.UI.Charting.Command
  15. {
  16. public class AddDataSeriesCommand : ICommand
  17. {
  18. public AddDataSeriesCommand(string dataSourceName, string dataSeriesName, string displayName, Color lineColor, float factor, float offset, int lineWidth)
  19. {
  20. _dataSourceName = dataSourceName;
  21. _dataSeriesName = dataSeriesName;
  22. _displayName = displayName;
  23. _lineColor = lineColor;
  24. _factor = factor;
  25. _offset = offset;
  26. if (lineWidth < 1) lineWidth = 1;
  27. _lineWidth = lineWidth;
  28. }
  29. string _dataSourceName;
  30. string _dataSeriesName;
  31. string _displayName;
  32. Color _lineColor;
  33. float _factor;
  34. float _offset;
  35. int _lineWidth;
  36. public bool CanExecute(object parameter)
  37. {
  38. return true;
  39. }
  40. #pragma warning disable 0067
  41. public event EventHandler CanExecuteChanged;
  42. #pragma warning restore 0067
  43. private void AsyncShowWarningMessageBox(string message, string title)
  44. {
  45. Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Warning)));
  46. }
  47. public void Execute(object parameter)
  48. {
  49. string reason = string.Empty;
  50. try
  51. {
  52. var commonViewModel = (CommonViewModel)parameter;
  53. //判断新加入数据是否合理
  54. if (!commonViewModel.DataSources.Keys.Contains(_dataSourceName))
  55. {
  56. reason = string.Format("数据源{0}不存在", _dataSourceName);
  57. AsyncShowWarningMessageBox(reason, "警告");
  58. return;
  59. }
  60. if (!(commonViewModel.DataSources[_dataSourceName].Datas.Keys.Contains(_dataSeriesName)))
  61. {
  62. reason = string.Format("在数据源{0}中,\n不存在数据变量{1}", _dataSourceName, _dataSeriesName);
  63. AsyncShowWarningMessageBox(reason, "警告");
  64. return;
  65. }
  66. foreach (MyLineSeries item in commonViewModel.RenderableSeries)
  67. {
  68. if (item.DataSourceName == commonViewModel.DataSources[_dataSourceName].Title && item.SeriesName == _dataSeriesName)
  69. {
  70. reason = string.Format("变量{0}已添加", item.SeriesDisplayName);
  71. AsyncShowWarningMessageBox(reason, "警告");
  72. return;
  73. }
  74. }
  75. if (commonViewModel.RenderableSeries.Count == 0)//修复第一次加载界面无法实时更新"RenderableSeries"问题
  76. {
  77. commonViewModel.RenderableSeries/*xaml binging object*/.Add(new MyLineSeries("firstLoad", null));
  78. foreach (MyLineSeries line in commonViewModel.RenderableSeries)
  79. {
  80. if (line.SeriesName.Equals("firstLoad"))
  81. {
  82. commonViewModel.RenderableSeries.Remove(line);
  83. break;
  84. }
  85. }
  86. }
  87. //创建DataSeries对象
  88. var newLineSeries = new MyLineSeries(_dataSeriesName, commonViewModel.DataSources[_dataSourceName]);
  89. //如果已指定显示名称,则使用指定的显示名称
  90. //if (!string.IsNullOrEmpty(_displayName)) newLineSeries.DisplayName = _displayName;
  91. //如果已指定颜色,则使用指定的颜色
  92. if (_lineColor != Colors.White) newLineSeries.SeriesColor = _lineColor;
  93. //如果已指定缩放因子,则使用指定的缩放因子
  94. newLineSeries.Factor = _factor;
  95. //指定offset
  96. newLineSeries.Offset = _offset;
  97. //如果已指定线宽,则使用指定的线宽
  98. newLineSeries.LineThickness = _lineWidth;
  99. //更新RenderableSeries
  100. commonViewModel.RenderableSeries/*xaml binging object*/.Add(newLineSeries);
  101. //通知Property修改
  102. commonViewModel.InvokePropertyChanged("RenderableSeries");
  103. }
  104. catch (Exception ex)
  105. {
  106. reason = ex.Message;
  107. CONTEXT.WriteLog(ex, string.Format("添加数据源{0}数据{1}发生失败", _dataSourceName, _dataSeriesName));
  108. AsyncShowWarningMessageBox(reason, "警告");
  109. }
  110. }
  111. }
  112. }