ScanGraph2VmUt.cs 7.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
using LiveCharts;
using LiveCharts.Configurations;
using LiveCharts.Wpf;
using Misc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Data;
using System.Windows.Media;

12
namespace FLY.Thick.Blowing.UI.UiModule
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 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
{
    public class ScanGraph2VmUt : INotifyPropertyChanged
    {
        #region 图表控制
        public double YMax { get; set; }
        public double YMin { get; set; }
        public double Target { get; set; }
        public double Tolerance2YMax { get; set; }
        public double Tolerance2YMin { get; set; }
        public double ToleranceYMax { get; set; }
        public double ToleranceYMin { get; set; }
        public double XMax { get; set; }
        public double XMin { get; set; }

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

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

        public SeriesCollection Series { get; set; }

        #endregion

        #region 颜色
        List<Brush> AreaColors;
        #endregion


        #region 界面统计值

        public double Average { get; set; }


        public string Sigma2Text { get; set; }
        public string MaxText { get; set; }
        public string MinText { get; set; }

        /// <summary>
        /// 混合数
        /// </summary>
        public int Mix { get; set; }

        /// <summary>
        /// 测量时间
        /// </summary>
        public DateTime Time { get; set; }

        /// <summary>
        /// 旋转方向 是反向
        /// </summary>
        public bool IsBackw { get; private set; }

        /// <summary>
        /// 旋转1周的时间
        /// </summary>
        public TimeSpan RPeriod { get; private set; }


        public DateTime Time2 { get; set; }
        /// <summary>
        /// 数据库中的ID标示
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 复位区号
        /// </summary>
        public int OrgBoltNo { get; set; }

        public bool IsPercent { get; set; }
        public bool IsAutoTarget { get; set; }


        #endregion



        ChartValues<double> Values { get; } = new ChartValues<double>();
        ChartValues<double> Values2 { get; } = new ChartValues<double>();
        object Mapper { get; set; }
        object Mapper2 { get; set; }


        public ScanGraph2VmUt()
        {

            #region 与数据无关界面参数
            AreaColors = new List<Brush>();
            for (int i = 0; i < 5; i++)
                AreaColors.Add(FLY.ControlLibrary.Themes.Styles.AreaColors[i]);

            
            YFormatter = (y) =>
            {
                if (IsPercent)
                {
                    if (y == Average)
                    {
                        return y.ToString("F1");
                    }
                    else
                    {
                        double percent = 100.0 * (y - Average) / Average;
                        if (percent > 0)
                        {
                            return $"+{Math.Abs(percent):F1}%";
                        }
                        else {
                            return $"-{Math.Abs(percent):F1}%";
                        }
                        
                    }
                }
                else {
                    return y.ToString("F1");
                }
            };

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

            #endregion
            #region 数据
            double target = 150;
            double tolerance = target * 0.02;
            double yrangepercent = 3;
            int nbolts = 80;
            double[] datas = new double[nbolts];
            Random random = new Random();
            for (int i = 0; i < nbolts; i++)
            {
                datas[i] = (Math.Sin(i * Math.PI / nbolts) * 3) * tolerance + target + (random.NextDouble() - 0.5) * 1 - 6;
            }

            double[] datas2 = new double[nbolts];
            for (int i = 0; i < nbolts; i++)
            {
                datas2[i] = (Math.Sin(i * Math.PI / nbolts) * 3) * tolerance + target + (random.NextDouble() - 0.5) * 1 - 6;
            }

            #endregion
            #region 界面数据



            XMin = 0;
            XMax = nbolts + 1;
            XStep = 5;
            Mix = 2;
            Time = DateTime.Now;
            Time2 = DateTime.Now - TimeSpan.FromMinutes(10);

            IsBackw = true;
            RPeriod = TimeSpan.FromMinutes(10.1);
            Id = 312312;

            OrgBoltNo = 31;
            IsPercent = true;
            IsAutoTarget = true;
            Values.AddRange(datas);
            Values2.AddRange(datas2);
            Average = datas.AverageNoNull();



            double Max = datas.Max();
            double Min = datas.Min();
            double Sigma2 = datas.Sigma() * 2;


            if (IsPercent)
            {
                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}";
            }

            if (IsPercent || IsAutoTarget)
            {
                Target = Average;
            }
            else {
                Target = target;
            }

            ToleranceYMax = Target + tolerance;
            ToleranceYMin = Target - tolerance;
            Tolerance2YMax = Target + tolerance * 2;
            Tolerance2YMin = Target - tolerance * 2;
            YMax = Target + tolerance * yrangepercent;
            YMin = Target - tolerance * yrangepercent;

            #endregion

            Series = new SeriesCollection
            {
                new Column2Series
                {
                    Title = "扫描图",
                    Values = Values,
                    StrokeThickness = 1,
                    Stroke = new SolidColorBrush(Colors.Black),
                    PointGeometry = null,
                    Configuration = Mapper
                },
                new LineSeries
                {
                    Title = "曲线图",
                    Values = Values2,
                    StrokeThickness = 3,
                    Stroke = new SolidColorBrush(Colors.DarkBlue),
                    Fill = new SolidColorBrush(Colors.Transparent),
                    PointGeometry = null,
                    Configuration = Mapper2
                }
            };
            ((Series)Series[0]).SetBinding(Column2Series.YAxisCrossingProperty, new Binding("Target") { Source = this });
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}