using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Input; using Aitex.UI.Charting.ViewModel; using System.Windows; using Aitex.UI.Charting.Model; using Abt.Controls.SciChart; using System.Windows.Media; using Abt.Controls.SciChart.Model.DataSeries; using Aitex.UI.Charting.View; using DataAnalysisControl.Core; namespace Aitex.UI.Charting.Command { public class AddDataSeriesCommand : ICommand { public AddDataSeriesCommand(string dataSourceName, string dataSeriesName, string displayName, Color lineColor, float factor, float offset, int lineWidth) { _dataSourceName = dataSourceName; _dataSeriesName = dataSeriesName; _displayName = displayName; _lineColor = lineColor; _factor = factor; _offset = offset; if (lineWidth < 1) lineWidth = 1; _lineWidth = lineWidth; } string _dataSourceName; string _dataSeriesName; string _displayName; Color _lineColor; float _factor; float _offset; int _lineWidth; public bool CanExecute(object parameter) { return true; } #pragma warning disable 0067 public event EventHandler CanExecuteChanged; #pragma warning restore 0067 private void AsyncShowWarningMessageBox(string message, string title) { Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Warning))); } public void Execute(object parameter) { string reason = string.Empty; try { var commonViewModel = (CommonViewModel)parameter; //判断新加入数据是否合理 if (!commonViewModel.DataSources.Keys.Contains(_dataSourceName)) { reason = string.Format("数据源{0}不存在", _dataSourceName); AsyncShowWarningMessageBox(reason, "警告"); return; } if (!(commonViewModel.DataSources[_dataSourceName].Datas.Keys.Contains(_dataSeriesName))) { reason = string.Format("在数据源{0}中,\n不存在数据变量{1}", _dataSourceName, _dataSeriesName); AsyncShowWarningMessageBox(reason, "警告"); return; } foreach (MyLineSeries item in commonViewModel.RenderableSeries) { if (item.DataSourceName == commonViewModel.DataSources[_dataSourceName].Title && item.SeriesName == _dataSeriesName) { reason = string.Format("变量{0}已添加", item.SeriesDisplayName); AsyncShowWarningMessageBox(reason, "警告"); return; } } if (commonViewModel.RenderableSeries.Count == 0)//修复第一次加载界面无法实时更新"RenderableSeries"问题 { commonViewModel.RenderableSeries/*xaml binging object*/.Add(new MyLineSeries("firstLoad", null)); foreach (MyLineSeries line in commonViewModel.RenderableSeries) { if (line.SeriesName.Equals("firstLoad")) { commonViewModel.RenderableSeries.Remove(line); break; } } } //创建DataSeries对象 var newLineSeries = new MyLineSeries(_dataSeriesName, commonViewModel.DataSources[_dataSourceName]); //如果已指定显示名称,则使用指定的显示名称 //if (!string.IsNullOrEmpty(_displayName)) newLineSeries.DisplayName = _displayName; //如果已指定颜色,则使用指定的颜色 if (_lineColor != Colors.White) newLineSeries.SeriesColor = _lineColor; //如果已指定缩放因子,则使用指定的缩放因子 newLineSeries.Factor = _factor; //指定offset newLineSeries.Offset = _offset; //如果已指定线宽,则使用指定的线宽 newLineSeries.LineThickness = _lineWidth; //更新RenderableSeries commonViewModel.RenderableSeries/*xaml binging object*/.Add(newLineSeries); //通知Property修改 commonViewModel.InvokePropertyChanged("RenderableSeries"); } catch (Exception ex) { reason = ex.Message; CONTEXT.WriteLog(ex, string.Format("添加数据源{0}数据{1}发生失败", _dataSourceName, _dataSeriesName)); AsyncShowWarningMessageBox(reason, "警告"); } } } }