123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Drawing;
- using System.Runtime.Serialization;
- using SciChart.Charting.Model.DataSeries;
- using SciChart.Charting.Visuals.RenderableSeries;
- using Aitex.Core.UI.MVVM;
- using Aitex.Core.UI.View.Smart;
- using SciChart.Charting.Visuals.Annotations;
- using SciChart.Data.Model;
- namespace Aitex.Core.UI.ControlDataContext
- {
- [DataContract]
- [Serializable]
- public class HistoryDataItem
- {
- [DataMember]
- public DateTime dateTime
- {
- get;
- set;
- }
- [DataMember]
- public string dbName
- {
- get;
- set;
- }
- [DataMember]
- public double value
- {
- get;
- set;
- }
- }
- public class ProcessDataChartDataItem : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- public void InvokePropertyChanged(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- public DelegateCommand<object> SeriesSelectAllCommand
- {
- get;
- private set;
- }
- public DelegateCommand<object> SeriesSelectNoneCommand
- {
- get;
- private set;
- }
- public DelegateCommand<object> SeriesSelectDefaultCommand
- {
- get;
- private set;
- }
- public ObservableCollection<IRenderableSeries> RenderableSeries
- {
- get;
- set;
- }
- public string ProcessInfo
- {
- get;
- set;
- }
- public int Count
- {
- get
- {
- return RenderableSeries.Count;
- }
- }
- private int _capacity = 10000;
- private AnnotationCollection _annotations;
- public AnnotationCollection Annotations
- {
- get => _annotations;
- set
- {
- _annotations = value;
- InvokePropertyChanged(nameof(Annotations));
- }
- }
- public ProcessDataChartDataItem(int capacity = 60 * 5)
- {
- SeriesSelectAllCommand = new DelegateCommand<object>(new Action<object>(OnSeriesSelectAll), null);
- SeriesSelectNoneCommand = new DelegateCommand<object>(new Action<object>(OnSeriesSelectNone), null);
- SeriesSelectDefaultCommand = new DelegateCommand<object>(new Action<object>(OnSeriesSelectDefault), null);
- RenderableSeries = new ObservableCollection<IRenderableSeries>();
- _capacity = capacity;
- }
- public void SetInfo(string info)
- {
- ProcessInfo = info;
- InvokePropertyChanged("ProcessInfo");
- }
- public void AppendData(Dictionary<string, object> data)
- {
- if (data == null)
- return;
- foreach (var item in RenderableSeries)
- {
- var series = item as SmartDataLine;
- if (series == null)
- continue;
- if (!data.ContainsKey(series.DbDataName))
- continue;
- var dataSeries = series.DataSeries as XyDataSeries<DateTime, float>;
- while (dataSeries.Count > _capacity)
- dataSeries.RemoveAt(0);
- dataSeries.Append(DateTime.Now, (float)(Convert.ToDouble(data[series.DbDataName])));
- }
- }
- public void UpdateCultureString(Dictionary<string, string> data)
- {
- if (data == null)
- return;
- foreach (var item in RenderableSeries)
- {
- var series = item as SmartDataLine;
- if (series == null)
- continue;
- if (!data.ContainsKey(series.DbDataName))
- continue;
- series.DisplayName = data[series.DbDataName];
- }
- }
- public void UpdateData(List<HistoryDataItem> data)
- {
- ClearData();
- if (data == null || data.Count == 0)
- return;
- foreach (HistoryDataItem dataItem in data)
- {
- foreach (var item in RenderableSeries)
- {
- var series = item as SmartDataLine;
- if (series == null)
- continue;
- var dataSeries = series.DataSeries as XyDataSeries<DateTime, float>;
- while (dataSeries.Count > _capacity)
- dataSeries.RemoveAt(0);
- if (series.DbDataName != dataItem.dbName)
- continue;
- dataSeries.Append(dataItem.dateTime, (float)(dataItem.value));
- }
- }
- }
- public void ClearData()
- {
- foreach (var item in RenderableSeries)
- {
- var series = item as SmartDataLine;
- if (series == null)
- continue;
- var dataSeries = series.DataSeries as XyDataSeries<DateTime, float>;
- dataSeries.Clear();
- }
- }
- /// <summary>
- /// 如果已经有了key,更新名字和做标注
- /// 如果不存在,创建新的
- /// </summary>
- /// <param name="items"></param>
- public void UpdateColumns(Dictionary<string/*key value=database column name*/, Tuple<string, string, bool>> dicItems)
- {
- List<IRenderableSeries> lstRemovedSeries = new List<IRenderableSeries>();
- foreach (var seriesItem in RenderableSeries)
- {
- var series = seriesItem as SmartDataLine;
- if (series == null)
- continue;
- if (!dicItems.ContainsKey(series.DbDataName))
- {
- lstRemovedSeries.Add(seriesItem);
- continue;
- }
- UpdateColumn(dicItems[series.DbDataName]);
- dicItems.Remove(series.DbDataName);
- }
- foreach (var newItem in dicItems)
- {
- RenderableSeries.Add(new SmartDataLine(newItem.Value.Item2, newItem.Value.Item3 ? System.Windows.Media.Colors.Blue : System.Windows.Media.Colors.Red,
- newItem.Value.Item1, true)
- {
- YAxisId = newItem.Value.Item3 ? "PressureYAxisId" : "GeneralYAxisId"
- });
- }
- foreach (var removeItem in lstRemovedSeries)
- {
- RenderableSeries.Remove(removeItem);
- }
- }
- public void UpdateColumn(Tuple<string, string, bool> columnInfo)
- {
- foreach (var item in RenderableSeries)
- {
- var series = item as SmartDataLine;
- if (series != null && series.DbDataName == columnInfo.Item1)
- {
- series.DisplayName = columnInfo.Item2;
- series.YAxisId = columnInfo.Item3 ? "PressureYAxisId" : "GeneralYAxisId";
- break;
- }
- }
- }
- public void UpdateColumn(string dbName, string displayName, bool isPressure)
- {
- foreach (var item in RenderableSeries)
- {
- var series = item as SmartDataLine;
- if (series != null && series.DbDataName == dbName)
- {
- series.DisplayName = displayName;
- break;
- }
- }
- }
- public void UpdateColumnDisplayName(string dbName, string displayName)
- {
- foreach (var item in RenderableSeries)
- {
- var series = item as SmartDataLine;
- if (series != null && series.DbDataName == dbName)
- {
- series.DisplayName = displayName;
- break;
- }
- }
- }
- public void RemoveColumn(string dbName)
- {
- foreach (var item in RenderableSeries)
- {
- var series = item as SmartDataLine;
- if (series != null && series.DbDataName == dbName)
- {
- RenderableSeries.Remove(series);
- break;
- }
- }
- }
- public void RemoveAllColumn()
- {
- ClearData();
- RenderableSeries.Clear();
- }
- public void InitColumns(Dictionary<string, string> items)
- {
- ClearData();
- RenderableSeries.Clear();
- foreach (KeyValuePair<string, string> item in items)
- {
- RenderableSeries.Add(new SmartDataLine(item.Key, System.Windows.Media.Colors.Blue,
- item.Value, true)
- {
- YAxisId = item.Key.Contains("Pressure") ? "PressureYAxisId" : "GeneralYAxisId"
- });
- }
- }
- public void SetColor(string key, System.Windows.Media.Color color)
- {
- foreach (var item in RenderableSeries)
- {
- var series = item as SmartDataLine;
- if (series == null)
- continue;
- if (series.DisplayName == key)
- series.Stroke = color;
- }
- }
- public void OnSeriesSelectAll(object param)
- {
- foreach (var item in RenderableSeries)
- {
- var series = item as SmartDataLine;
- if (series == null)
- continue;
- series.IsVisible = true;
- }
- }
- public void OnSeriesSelectNone(object param)
- {
- foreach (var item in RenderableSeries)
- {
- var series = item as SmartDataLine;
- if (series == null)
- continue;
- series.IsVisible = false;
- }
- }
- public void OnSeriesSelectDefault(object param)
- {
- foreach (var item in RenderableSeries)
- {
- var series = item as SmartDataLine;
- if (series == null)
- continue;
- series.IsVisible = series.IsDefaultVisable;
- series.Stroke = series.DefaultSeriesColor;
- series.LineThickness = 1;
- }
- }
- }
- }
|