GraphScan2.cs 6.67 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;


namespace FLY.ControlLibrary
{
    public class GraphScan2:GraphScan
    {
        public GraphScan2():base()
        {
            mRefresh = new Refresh(this);
            
        }

        #region 刷新率控制
        class Refresh
        {
            /// <summary>
            /// 控制刷新率
            /// </summary>
            System.Windows.Threading.DispatcherTimer timer;
            bool updatedata_flag = false;

            /// <summary>
            /// 最快0.1s 刷新一次
            /// </summary>
            TimeSpan REFRESH_RATE = TimeSpan.FromSeconds(0.1);

            public Refresh(GraphScan2 graph)
            {
                timer = new System.Windows.Threading.DispatcherTimer();
                timer.Tick += new EventHandler(delegate(object sender, EventArgs e)
                {
                    if (UpdateData)
                    {
                        UpdateData = false;
                        graph.DataBindAll2();
                    }
                    timer.Stop();
                });

                timer.Interval = REFRESH_RATE;
                timer.Start();
            }

            public bool UpdateData
            {
                get { return updatedata_flag; }
                set
                {
                    updatedata_flag = value;
                    if (value)
                    {
                        if (!timer.IsEnabled)
                            timer.Start();
                    }
                }
            }
        }
        Refresh mRefresh;
        #endregion



        protected override void Test()
        {
            base.Test();

            ObservableCollection<int> datasource = new ObservableCollection<int>();

            // Populate series data
            Random random = new Random();

            for (int i = 0; i < 40; i++)
            {
                datasource.Add(random.Next(3000) - 1500 + Target);
            }

            DataSource2 = datasource;
        
        }
        protected override void InitializeComponent2()
        {
            base.InitializeComponent2();

            System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();

            series1.BorderColor = System.Drawing.Color.DarkBlue;
            series1.BorderWidth = 2;

            series1.ChartArea = "Default";
            //TODO
            series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            //series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;

            series1.Color = System.Drawing.Color.Blue;

            series1.Legend = "Default";
            series1.Name = "Series 2";
            series1.ShadowColor = System.Drawing.Color.Black;
            series1.ShadowOffset = 2;
            series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
            series1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;


            this.chart1.Series.Add(series1);
        }

        /// <summary>
        /// 数据源
        /// </summary>
        private ObservableCollection<int> _datasource;
        public virtual ObservableCollection<int> DataSource2
        {
            get
            {
                return _datasource;
            }
            set
            {
                _datasource = value;
                _datasource.CollectionChanged += dataSource_CollectionChanged;
                mRefresh.UpdateData = true;
            }
        }
        /// <summary>
        /// DataSource 里面的数据都是 EmptyValue 组成, 当修改数据时,应该clear ,再添加
        /// </summary>
        private bool DataSourceIsEmpty = false;
        /// <summary>
        /// 当一个数据都没有时,需要添加空数据
        /// </summary>
        public void CheckDataSourceEmpty2()
        {
            if ((DataSource2 == null) || (DataSource2.Count() == 0))
            {
                chart1.Series["Series 2"].Points.Clear();
                chart1.Series["Series 2"].Points.AddXY(FirstBoltNo, EmptyValue);
                chart1.Series["Series 2"].Points[0].IsEmpty = true;
                DataSourceIsEmpty = true;
            }
            else
            {
                DataSourceIsEmpty = false;
            }
        }
        void dataSource_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            mRefresh.UpdateData = true;
        }
        public void DataBindAll2()
        {
            double multi = Multi;
            int emptyvalue = EmptyValue;

            chart1.Series["Series 2"].Points.Clear();

            if (DataSource2 != null)
            {
                for (int i = 0; i < DataSource2.Count(); i++)
                {
                    UpdateOnePoint2(i);
                }
            }

            CheckDataSourceEmpty2();
        }
        public virtual void UpdateOnePoint2(int index)
        {
            double multi = Multi;

            int emptyvalue = EmptyValue;
            System.Windows.Forms.DataVisualization.Charting.DataPoint point;
            int x = FirstBoltNo + index;
            double y = (double)DataSource2.ElementAt(index);
            y = y / multi;
            if (index >= chart1.Series["Series 2"].Points.Count)
            {
                chart1.Series["Series 2"].Points.AddXY(x, y);
                point = chart1.Series["Series 2"].Points[index];
            }
            else
            {

                point = chart1.Series["Series 2"].Points[index];
                point.XValue = x;
                point.YValues[0] = y;

            }

            //设定空数据

            if (DataSource2[index] == emptyvalue)
            {
                point.IsEmpty = true;
            }
            else
            {
                point.IsEmpty = false;
            }
        }


        void GraphScan2_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "AutoTarget")
            {
                if (IsAutoTarget)
                {
                    mRefresh.UpdateData = true;
                }
            }
            if ((e.PropertyName == "Target") || (e.PropertyName == "Alarm") || (e.PropertyName == "YRange") || (e.PropertyName == "YRangePercent") || (e.PropertyName == "IsAutoTarget") || (e.PropertyName == "IsPercent") || (e.PropertyName == "AutoTarget"))
            {
                mRefresh.UpdateData = true;
            }
        }
    }
}