ningshuxia
2023-02-24 1e4b358de58e36bfbf692ab2538ff9e7d60fd025
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using DevExpress.XtraCharts;
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IStation.Untity;
using DevExpress.Utils;
using System.Runtime;
using DevExpress.XtraSplashScreen;
 
namespace IStation.WinFormUI.MonitorDataSet
{
    public partial class CurveCompareChartMgr : DocumentPage
    {
        public CurveCompareChartMgr()
        {
            InitializeComponent();
            this.multiSelectMonitorPointTreeListCtrl1.SelectedEvent += MultiSelectMonitorPointTreeListCtrl1_SelectedEvent;
            this.multiSelectMonitorPointTreeListCtrl1.UncheckEvent += MultiSelectMonitorPointTreeListCtrl1_UncheckEvent;
            this.PageTitle.Caption = "曲线比较";
        }
        
        #region 当前视图类
        public class CurrentViewModel
        {
            public CurrentViewModel() { }
            public CurrentViewModel(Model.SignalRecord rhs)
            {
                this.DataTime = rhs.Time;
                this.DataValue = rhs.Value;
            }
 
            public DateTime DataTime { get; set; }
            public double DataValue { get; set; }
        }
        #endregion
          
        private long _projectId;
        private long _sceneId;
        private Model.LogicTree _logic;
 
        private SwiftPlotDiagram _diagram
        {
            get { return this.chartControl1.Diagram as SwiftPlotDiagram; }
        }
 
        /// <summary>
        /// 初始化数据
        /// </summary>
        public void InitialDataSource(long projectId,long sceneId, Model.LogicTree logic)
        {
            _projectId = projectId;
            _sceneId = sceneId;
            SetBindingData(logic);
        }
 
        private void SetBindingData(Model.LogicTree obj)
        {
            _logic = obj;
            if (_logic == null)
            { 
                this.multiSelectMonitorPointTreeListCtrl1.SetBindingData();
            }
            else
            {
                this.multiSelectMonitorPointTreeListCtrl1.SetBindingData(_projectId, _logic.LogicType, _logic.LogicId);
            }
        }
 
        private void MultiSelectMonitorPointTreeListCtrl1_SelectedEvent(Model.MonitorPointExSignalList obj, Color color)
        {
            var signal = obj.SignalList.First();
            var lays = this.chartControl1.ShowOverlay();
 
            var packet = new BLL.MonitorDataSet().GetSignalRecordPacket(_projectId, _sceneId, obj.Id, signal.Id);
            if (packet==null|| packet.RecordList==null)
            {
                XtraMessageBox.Show("无数据!");
                lays.Close();
                return;
            } 
            CreateSeries(signal, packet.RecordList);
            lays.Close();
        }
 
        //添加系列
        private void CreateSeries(Model.Signal signal, IEnumerable<Model.SignalRecord> records)
        {
            try
            {
                var count = this.chartControl1.Series.Where(x => Convert.ToInt64(x.Tag) == signal.Id).Count();
                if (count > 0)
                    return;
                var unitValue = UnitValueHelper.Get(_projectId,signal);
                var view = new SwiftPlotSeriesView();
                view.AxisX = _diagram.AxisX;
                if (this.chartControl1.Series.Count < 1)
                {
                    _diagram.AxisY.Title.Text = unitValue;
                    view.AxisY = _diagram.AxisY;
                }
                else
                {
                    if (_diagram.AxisY.Title.Text == unitValue)
                    {
                        view.AxisY = _diagram.AxisY;
                    }
                    else
                    {
                        var axisy = _diagram.SecondaryAxesY.GetAxisByName(unitValue);
                        if (axisy == null)
                        {
                            axisy = new SwiftPlotDiagramSecondaryAxisY();
                            axisy.Name = unitValue;
                            axisy.Alignment = AxisAlignment.Far;
                            axisy.Title.Text = unitValue;
                            axisy.Title.Alignment = StringAlignment.Far;
                            axisy.Title.Visibility = DefaultBoolean.True;
                            _diagram.SecondaryAxesY.Add(axisy);
                        }
                        view.AxisY = axisy;
                    }
                }
                Series series = new Series();
                series.Tag = signal.Id;
                series.Name = signal.Name;
                series.View = view;
 
                var list = records.Select(x => new { Time = x.Time, Value = x.Value });
                series.BindToData(list, "Time", "Value");
                this.chartControl1.Series.Add(series);
            }
            catch (OutOfMemoryException)
            {
            }
            finally
            {
                Gc();
            }
        }
         
        private void MultiSelectMonitorPointTreeListCtrl1_UncheckEvent(Model.MonitorPointExSignalList obj)
        {
            var signal = obj.SignalList.First();
            var lays = this.chartControl1.ShowOverlay();
            List<Series> seriesList = new List<Series>(this.chartControl1.Series.ToArray());
            var index = seriesList.FindIndex(x => Convert.ToInt64(x.Tag) == signal.Id);
            if (index != -1)
            {
                this.chartControl1.BeginInit();
                var unitValue = UnitValueHelper.Get(_projectId, signal);
                var axisy = _diagram.SecondaryAxesY.GetAxisByName(unitValue);
                if (axisy != null)
                {
                    _diagram.SecondaryAxesY.Remove(axisy);
                }
                this.chartControl1.Series.RemoveAt(index);
                this.chartControl1.EndInit();
            }
            Gc();
            lays.Close();
        }
 
        private void Gc()
        {
            GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
            GC.Collect(2, GCCollectionMode.Forced);
            GC.WaitForFullGCComplete();
        }
         
    }
}