ScanGraphVm.cs 14 KB
Newer Older
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
using FLY.Thick.FilmCasting.Common;
using FLY.Thick.FilmCasting.IService;
using FLY.Thick.FilmCasting.Server.Model;
using FObjBase;
using LiveCharts;
using LiveCharts.Configurations;
using LiveCharts.Wpf;
using Misc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;

namespace FLY.Thick.FilmCasting.UI.UiModule
{
    public class ScanGraphVm : INotifyPropertyChanged
    {
        const int MARKNO_TEXTUP = 1;
        const int MARKNO_TARGET_DELAY = 2;
        #region 图表控制
        public double YMax { get; set; } = double.NaN;
        public double YMin { get; set; } = double.NaN;
        public double Target { get; set; } = double.NaN;
        public double ToleranceYMax { get; set; } = double.NaN;
        public double ToleranceYMin { get; set; } = double.NaN;
        public double Tolerance2YMax { get; set; } = double.NaN;
        public double Tolerance2YMin { get; set; } = double.NaN;
        public double XMax { get; set; } = double.NaN;
        public double XMin { get; set; } = double.NaN;

        public int XStep { get; set; } = 10;

        public Func<double, string> YFormatter { get; set; }
        public Func<double, string> XFormatter { get; set; }

        public SeriesCollection Series { get; set; }

        #endregion



        #region 界面统计值
        /// <summary>
        /// 平均值
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
50
        public double Average { get; protected set; } = double.NaN;
51

潘栩锋's avatar
潘栩锋 committed
52
        public double Sigma2 { get; protected set; } = double.NaN;
53 54 55
        /// <summary>
        /// 最大值
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
56
        public double Max { get; protected set; } = double.NaN;
57 58 59
        /// <summary>
        /// 最小值
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
60
        public double Min { get; protected set; } = double.NaN;
61

潘栩锋's avatar
潘栩锋 committed
62 63 64
        public string Sigma2Text { get; protected set; }
        public string MaxText { get; protected set; }
        public string MinText { get; protected set; }
65 66 67 68

        /// <summary>
        /// 混合数
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
69
        public int Mix { get; protected set; } = 2;
70 71 72 73

        /// <summary>
        /// 测量时间
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
74
        public DateTime Time { get; protected set; }
75 76 77 78

        /// <summary>
        /// 测量时间
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
79
        public DateTime EndTime { get; protected set; }
80 81 82
        /// <summary>
        /// 数据库中的序号
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
83
        public long Id { get; protected set; }
84

潘栩锋's avatar
潘栩锋 committed
85 86
        public double Velocity { get; protected set; }
        public double Position { get; protected set; }
87 88 89 90

        /// <summary>
        /// 膜宽 mm
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
91
        public double FilmWidth { get; protected set; }
92 93 94

        #endregion

潘栩锋's avatar
潘栩锋 committed
95
        protected List<Brush> AreaColors;
96 97

        //最大只能显示100个数据,大于100, 都会被/2
潘栩锋's avatar
潘栩锋 committed
98
        protected ChartValues<double> Values = new ChartValues<double>();
99

潘栩锋's avatar
潘栩锋 committed
100 101
        //double 转 XY
        protected object Mapper;
102

潘栩锋's avatar
潘栩锋 committed
103 104
        protected FilmCastingProfileParam profileParam;
        protected ScanGraphItemParam graphparam;
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
        IBulkDbService bulkDb;
        Lc_ScanData scanData;
        ParamDictionary paramDictionary;
        public ScanGraphVm()
        {
            #region 与数据无关界面参数

            AreaColors = FLY.Thick.FilmCasting.UI.Themes.Colors.AreaColors;

            YFormatter = (y) =>
            {
                //只显示 Target,ToleranceYMax,ToleranceYMin,Tolerance2YMax,Tolerance2YMin
                string text = "";
                if (graphparam.IsPercent)
                {
                    double target = Target;
                    if (target > 0)
                    {
                        if (y == target)
                        {
                            text = y.ToString("F1");
                        }
                        else
                        {
                            double percent = 100.0 * (y - target) / target;
                            if (percent > 0)
                            {
                                text = $"+{Math.Abs(percent):F1}%";
                            }
                            else
                            {
                                text = $"-{Math.Abs(percent):F1}%";
                            }
                        }
                    }
                    else
                    {
                        text = y.ToString("F1");
                    }
                }
                else
                {
                    text = y.ToString("F1");
                }
                return $"{text,6}";
            };

            XFormatter = (x) =>
            {
                return $"{x + 1}";
            };

            Mapper = Mappers.Xy<double>()
                .X((value, index) =>
                {
                    return index;
                })
                .Y(value => value)
                .Fill((value) =>
                {
                    if ((value > Tolerance2YMax) || (value < Tolerance2YMin))
                    {
                        return AreaColors[2];
                    }
                    else if ((value > ToleranceYMax) || (value < ToleranceYMin))
                    {
                        return AreaColors[1];
                    }
                    else
                    {
                        return AreaColors[0];
                    }
                });
            #endregion

            Series = new SeriesCollection
            {
                new Column2Series
                {
                    Values = Values,
                    Stroke = new SolidColorBrush(Colors.Black),
                    StrokeThickness = 1,
                    PointGeometry = null,
                    Configuration = Mapper
                }
            };
            ((Series)Series[0]).SetBinding(Column2Series.YAxisCrossingProperty, new Binding(nameof(Target)) { Source = this });
        }

        public void Init(
            ScanGraphItemParam graphparam,
            IBulkDbService bulkDb,
            ParamDictionary paramDictionary,
            FilmCastingProfileParam profileParam
            )
        {
            this.graphparam = graphparam;
            this.bulkDb = bulkDb;
            this.paramDictionary = paramDictionary;
            this.profileParam = profileParam;
            Init();
        }
        void Init()
        {

            paramDictionary.SetBinding(this, nameof(XStep), XStep);

            UpdateY();
            UpdateX();

            Values.Clear();

            Id = -1;
            UpdateId();

            bulkDb.PropertyChanged += BulkDb_PropertyChanged;
            graphparam.PropertyChanged += Graphparam_PropertyChanged;
            profileParam.PropertyChanged += ProfileParam_PropertyChanged;
            this.PropertyChanged += ScanGraphVm_PropertyChanged;
        }

        static string[] propertyname_updateMaxMinText = new string[] {
            nameof(Average),nameof(Max),nameof(Min),nameof(Sigma2)};

        private void ScanGraphVm_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {

            if (e.PropertyName == nameof(Target))//nameof(Average)
            {
                if (graphparam.IsPercent)
                {
                    NotifyPropertyChanged(nameof(YFormatter));
                }
            }

            if (propertyname_updateMaxMinText.Contains(e.PropertyName))
                UpdateMaxMinText();

        }

        private void ProfileParam_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(profileParam.Target))
            {
                UpdateY();
            }
            else if (e.PropertyName == nameof(profileParam.TolerancePercent))
            {
                UpdateY();
            }
        }

        private void BulkDb_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(bulkDb.LastScanDataId))
            {
                UpdateId();
            }
            else if (e.PropertyName == nameof(FObjBase.FObjServiceClient.IsConnected))
            {
                if ((bulkDb as FObjBase.FObjServiceClient).IsConnected)
                {
                    UpdateId();
                }
            }
        }

        private void Graphparam_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(graphparam.Number))
            {
                UpdateId();
            }
            else if (e.PropertyName == nameof(graphparam.Mix))
            {
                UpdateId();
            }
            else if (e.PropertyName == nameof(graphparam.YRangePercent))
            {
                UpdateY();
            }
            else if (e.PropertyName == nameof(graphparam.IsPercent))
            {
                NotifyPropertyChanged(nameof(YFormatter));
                UpdateY();
                UpdateMaxMinText();

            }
            else if (e.PropertyName == nameof(graphparam.IsAutoTarget))
            {
                UpdateY();
            }
        }



        void UpdateY()
        {
            //使用 profileParam 的参数显示
            UpdateY_profileParam();
        }
        void UpdateY_profileParam()
        {
            double target = this.profileParam.Target;

            if (graphparam.IsAutoTarget)
            {
                if (!double.IsNaN(Average))
                    target = Average;
            }

            double tolerance = this.profileParam.TolerancePercent * target;

            Target = target;
            ToleranceYMax = target + tolerance;
            ToleranceYMin = target - tolerance;
            Tolerance2YMax = target + tolerance * 2;
            Tolerance2YMin = target - tolerance * 2;

            YMax = target + tolerance * graphparam.YRangePercent;
            YMin = target - tolerance * graphparam.YRangePercent;
        }

潘栩锋's avatar
潘栩锋 committed
328
        protected void UpdateX()
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
        {
            if (profileParam.ScanRange.IsValid) {
                XMin = profileParam.ScanRange.Begin - 1;
                XMax = profileParam.ScanRange.End + 1;
            }
        }

        void UpdateId()
        {
            Update4IdChanged();
        }

        void UpdateMaxMinText()
        {
            PollModule.Current.Poll_JustOnce(() =>
            {
                if (graphparam.IsPercent && (Average > 0))
                {
                    MaxText = $"+{((Max - Average) / Average) * 100.0:F1}%";
                    MinText = $"-{((Average - Min) / Average) * 100.0:F1}%";
                    Sigma2Text = $"{(Sigma2 / Average) * 100.0:F1}%";
                }
                else
                {
                    MaxText = $"{Max:F1}";
                    MinText = $"{Min:F1}";
                    Sigma2Text = $"{Sigma2:F1}";
                }
            }, this, MARKNO_TEXTUP);
        }

        void UpdateAverage()
        {
            if (this.scanData == null)
            {
                Time = DateTime.MinValue;
                Id = -1;
                Position = -1;
                Velocity = -1;
                FilmWidth = -1;
                return;
            }
            else
            {
                Time = this.scanData.Time;
                EndTime = this.scanData.EndTime;
                Id = this.scanData.ID;
                Position = this.scanData.FilmPosition;
                Velocity = this.scanData.FilmVelocity;
                FilmWidth = this.scanData.FilmWidth;
            }


            List<double> list = new List<double>();
            double[] values = this.scanData.Thicks;
            if (this.profileParam.DataRange.IsValid) {

                int beginIndex = this.profileParam.DataRange.Begin;
                int endIndex = this.profileParam.DataRange.End;

                for (int i = beginIndex; i <= endIndex; i++)
                {
                    if (i < 0)
                        i = 0;
                    if (i >= values.Count())
                        break;
                    double v = values[i];
                    if (!double.IsNaN(v))
                        list.Add(values[i]);
                }
            }

            if (list.Count() > 0)
            {
                Average = list.Average();
                Max = list.Max();
                Min = list.Min();
                Sigma2 = list.Sigma() * 2;
            }
            else
            {
                Average = double.NaN;
                Max = double.NaN;
                Min = double.NaN;
                Sigma2 = double.NaN;
            }
        }
        
        void Update4IdChanged()
        {
            if (graphparam.Number > 0
                && bulkDb.LastScanDataId == Id
                && Mix == graphparam.Mix)
            {
                //不需要更新!!!!!!!
                return;
            }
            bulkDb.GetFrame(
                new Pack_GetFrameRequest() { Id = graphparam.Number, Mix = graphparam.Mix },
                (object AsyncContext, object retData) =>
                {
                    Pack_GetFrameReponse reponse = retData as Pack_GetFrameReponse;
                    
                    this.scanData = reponse.scanData;
                    Mix = reponse.Request.Mix;

                    //清除全部数据
                    Values.Clear();

                    //更新平均值,它会引起 YFormatter 刷新
                    UpdateAverage();

                    //更新Target
                    UpdateY();

                    UpdateX();

                    //必须先更新Y轴, X轴资料,最后再画线!!!!!!!
                    //不然 YAxisCrossing 会有机会无效

                    if (scanData != null && scanData.Thicks != null)
                    {
                        Values.AddRange(scanData.Thicks);
                    }

                }, this);
        }

潘栩锋's avatar
潘栩锋 committed
457
        protected void NotifyPropertyChanged(string propertyName)
458 459 460 461 462 463
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}