WindowSavePic.xaml.cs 15.9 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
using FLY.Thick.Blowing.Server.Model;
using Misc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FLY.Thick.Blowing.UI.Fix.Server
{
    /// <summary>
    /// WindowSavePic.xaml 的交互逻辑
    /// </summary>
    public partial class WindowSavePic : Window
    {

潘栩锋's avatar
潘栩锋 committed
26
        Lc_ScanData mScanData;
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
        /// <summary>
        /// Y轴倍数
        /// </summary>
        int YRangePercent;
        bool IsAutoTarget;
        double Target;
        double TolerancePercent;
        CHART_TYPE ChartType;

        public WindowSavePic()
        {
            InitializeComponent();
            InitializeComponent2();
        }

        void InitializeComponent2()
        {
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
            System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.Title title1 = new System.Windows.Forms.DataVisualization.Charting.Title();

            // 
            // chart1
            // 

            chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisX.IsMarksNextToAxis = false;
            chartArea1.AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
            chartArea1.AxisX.IsLabelAutoFit = true;
            chartArea1.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont;

            chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));


            chart1.BackColor = System.Drawing.Color.White;// System.Drawing.Color.FromArgb(((int)(((byte)(211)))), ((int)(((byte)(223)))), ((int)(((byte)(240)))));
                                                          //chart1.BackSecondaryColor = System.Drawing.Color.Blue;
                                                          //chart1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.None;
                                                          //chart1.BorderlineColor = System.Drawing.Color.FromArgb(((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
                                                          //chart1.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
                                                          //chart1.BorderlineWidth = 2;
                                                          //chart1.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;

            //chartArea1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(165)))), ((int)(((byte)(191)))), ((int)(((byte)(228)))));
            //chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            //chartArea1.BackSecondaryColor = System.Drawing.Color.Transparent;
            //chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            //chartArea1.ShadowColor = System.Drawing.Color.Transparent;

            //chartArea1.BackColor = System.Drawing.Color.Transparent;
            //chartArea1.BackColor = System.Drawing.Color.OldLace;
            //chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            //chartArea1.BackSecondaryColor = System.Drawing.Color.White;

            chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            chartArea1.Name = "Default";


            //chartArea1.Position.Height = 100;// 42F;
            //chartArea1.Position.Width = 100;// 88F;
            //chartArea1.Position.X = 0;// 3F;
            //chartArea1.Position.Y = 0;// 10F;

            //chartArea1.ShadowColor = System.Drawing.Color.Transparent;
            this.chart1.ChartAreas.Add(chartArea1);


            //legend1.BackColor = System.Drawing.Color.Transparent;
            //legend1.Enabled = false;
            //legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            //legend1.IsTextAutoFit = false;
            //legend1.Name = "Default";
            //this.chart1.Legends.Add(legend1);
            //this.chart1.Location = new System.Drawing.Point(16, 32);
            this.chart1.Name = "chart1";


            series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
            series1.BorderWidth = 2;

            series1.ChartArea = chartArea1.Name;
            //TODO
            series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;

            series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));

            series1.MarkerSize = 6;
            //series1.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
            series1.Name = "Series 1";
            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);

            //this.chart1.BackColor = System.Drawing.Color.Transparent;

            //this.chart1.PrePaint += new System.EventHandler<System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs>(Chart1_PrePaint);

            title1.Font = new System.Drawing.Font("Trebuchet MS", 14.25F, System.Drawing.FontStyle.Bold);
            title1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
            title1.Name = "Title1";
            title1.ShadowColor = System.Drawing.Color.FromArgb(((int)(((byte)(32)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            title1.ShadowOffset = 3;
            title1.Text = "";
            title1.Alignment = System.Drawing.ContentAlignment.TopCenter;
            this.chart1.Titles.Add(title1);
        }

        /// <summary>
        /// 画图,输出jpg文件
        /// </summary>
        /// <param name="scanData"></param>
        /// <param name="title">标题,里面应该包含有时间</param>
        /// <param name="isAutoTarget">自动目标值指示</para
        /// <param name="target">目标值</param>
        /// <param name="tolerancePercent">公差%</param>
        /// <param name="path">输出路径</param>
        /// <param name="yrangepercent">Y轴显示范围是公差的倍数</param>
        /// <param name="charttype">曲线类型</param>
潘栩锋's avatar
潘栩锋 committed
154
        public bool Paint(Lc_ScanData scanData, string title,bool isAutoTarget, double target, double tolerancePercent, string path, int yrangepercent, CHART_TYPE charttype)
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 328 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
        {
            mScanData = scanData;
            Title = title;
            IsAutoTarget = isAutoTarget;
            Target = target;
            TolerancePercent = tolerancePercent;
            if (TolerancePercent < 0.01)
                TolerancePercent = 0.01;
            YRangePercent = yrangepercent;
            ChartType = charttype;

            //数据检查
            if (IsAutoTarget)
            {
                Target = mScanData.Thicks.AverageNoNull();
                if (Target == double.NaN)
                {
                    return false;
                }
            }


            UpdateTitle();
            UpdateAxisY();
            UpdateStripLineY();
            UpdateAxisX();
            UpdateXInterval();
            UpdateCalState();
            UpdateChartType();
            DataChanged(mScanData.Thicks);

            chart1.SaveImage(path, ChartImageFormat.Jpeg);
            return true;
        }


        void DataChanged(double[] data)
        {
            DataPointCollection points = chart1.Series[0].Points;
            points.Clear();

            for (int i = 0; i < data.Count(); i++)
            {
                double d = data.ElementAt(i);
                DataPoint dp = new DataPoint();
                dp.YValues = new double[] { d };
                if(double.IsNaN(d))
                    dp.IsEmpty = true;

                dp.AxisLabel = $"{i + 1}";

                UpdateDataColor(dp);

                points.Add(dp);
            }
        }

        string GetCalState()
        {
            double[] data = mScanData.Thicks;
            string s = "最大值=null 最小值=null 平均值=null 2σ=null";
            if (data.Count() == 0)
                return s;
            

            var list2 = data.Where((d) =>
            {
                if (double.IsNaN(d))
                    return false;
                else
                    return true;
            });
            if (list2.Count() == 0)
                return s;

            double avg = list2.Average();
            double max = list2.Max();
            double min = list2.Min();
            double sigma = list2.Sigma();


            s = $"最大值={100*max/avg:F1}% 最小值={100 * min / avg:F1}% 平均值={avg:F1} 2σ={200 * sigma / avg:F1}%";
            return s;
        }
        void UpdateCalState()
        {
            System.Windows.Forms.DataVisualization.Charting.Axis axisX = chart1.ChartAreas[0].AxisX;
            axisX.Title = GetCalState();
        }
        void UpdateXInterval()
        {
            Axis axisX = chart1.ChartAreas[0].AxisX;
            axisX.Interval = 5;
        }
        void UpdateChartType()
        {
            if (ChartType == CHART_TYPE.Line)
                chart1.Series[0].ChartType = SeriesChartType.Line;
            else
                chart1.Series[0].ChartType = SeriesChartType.Column;
        }
        void UpdateStripLineY()
        {
            
            double target = Target;
            double warning = TolerancePercent * Target;

            if (warning < 0.1)
                warning = 0.1;
            Axis axisY = chart1.ChartAreas[0].AxisY;
            axisY.StripLines.Clear();


            axisY.StripLines.Add(new StripLine()
            {
                Interval = 0,
                IntervalOffset = target + warning,
                BorderColor = System.Drawing.Color.DarkGoldenrod,
                BackColor = System.Drawing.Color.DarkGoldenrod,
                StripWidth = 0.1
            });
            axisY.StripLines.Add(new StripLine()
            {
                Interval = 0,
                IntervalOffset = target - warning,
                BorderColor = System.Drawing.Color.DarkGoldenrod,
                BackColor = System.Drawing.Color.DarkGoldenrod,
                StripWidth = 0.1
            });
        }
        void UpdateAxisY()
        {
            bool ispercent = true;
            System.Windows.Forms.DataVisualization.Charting.Axis axisY = chart1.ChartAreas[0].AxisY;

            //TODO
            axisY.Crossing = Target;

            int yrangepercent = YRangePercent;
            if (yrangepercent < 2)
                yrangepercent = 2;

            double tolerance = Target * TolerancePercent;
            // Set manual minimum and maximum values.
            axisY.Minimum = Target - tolerance * yrangepercent;
            axisY.Maximum = Target + tolerance * yrangepercent;

            axisY.CustomLabels.Clear();

            SetCustomLabel(axisY, Target, GridTickTypes.All, System.Drawing.Color.Green, ispercent, null);

            SetCustomLabel(axisY, Target + tolerance, GridTickTypes.All, System.Drawing.Color.DarkGoldenrod, ispercent, null);
            SetCustomLabel(axisY, Target - tolerance, GridTickTypes.All, System.Drawing.Color.DarkGoldenrod, ispercent, null);

            SetCustomLabel(axisY, Target + tolerance * 2, GridTickTypes.All, System.Drawing.Color.Red, ispercent, null);
            SetCustomLabel(axisY, Target - tolerance * 2, GridTickTypes.All, System.Drawing.Color.Red, ispercent, null);

            for (int i = 3; i < yrangepercent + 1; i++)
            {
                SetCustomLabel(axisY, Target + tolerance * i, GridTickTypes.All, System.Drawing.Color.Black, ispercent, null);
                SetCustomLabel(axisY, Target - tolerance * i, GridTickTypes.All, System.Drawing.Color.Black, ispercent, null);
            }
        }
        void UpdateDataColor(DataPoint dp)
        {
            if (!dp.IsEmpty)
            {
                double yvalue = dp.YValues[0];
                double d = Math.Abs(yvalue - Target);
                double tolerance = Target * TolerancePercent;
                if (d <= tolerance)
                {
                    dp.Color = System.Drawing.Color.Green;
                }
                else if (d <= tolerance * 2)
                {
                    dp.Color = System.Drawing.Color.Orange;
                }
                else
                {
                    dp.Color = System.Drawing.Color.Red;
                }
                dp.BorderColor = dp.Color;
            }
        }
        void SetCustomLabel(Axis axisY, double p, GridTickTypes gt, System.Drawing.Color c, bool ispercent, string message)
        {
            double r = axisY.Maximum - axisY.Minimum;
            double f, t;
            f = p + r / 2;
            t = p - r / 2;
            double target = (axisY.Maximum + axisY.Minimum) / 2;

            CustomLabel customLabel;
            string str;
            if (!ispercent)
            {
                str = p.ToString("N1");
            }
            else
            {
                //百分比模式!!!!!!!
                if (target == p)
                {
                    str = p.ToString("N1");
                }
                else
                {
                    double percent;

                    percent = ((p - target) / target) * 100;
                    str = percent.ToString("N1") + "%";
                }
            }
            if (!string.IsNullOrEmpty(message))
                str = message + " " + str;

            customLabel = axisY.CustomLabels.Add(f, t, str);

            customLabel.GridTicks = gt;
            customLabel.ForeColor = c;
        }


        void UpdateAxisX()
        {
            System.Windows.Forms.DataVisualization.Charting.Axis axisX = chart1.ChartAreas[0].AxisX;
            axisX.Maximum = mScanData.Thicks.Count();
            axisX.Minimum = 1;
        }


        void UpdateTitle()
        {
            chart1.Titles[0].Text = $"{Title}";
        }
    }

    public enum CHART_TYPE
    {
        Line,
        Column
    }
}