PgCurveModify.xaml.cs 12.8 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using FLY.Thick.Base.Common;
using System.Windows.Threading;
using FLY.Thick.Base.Client;
18 19
using Unity;
using FLY.Thick.Base.IService;
潘栩锋's avatar
潘栩锋 committed
20 21
using System.ComponentModel;
using GalaSoft.MvvmLight.Command;
潘栩锋's avatar
潘栩锋 committed
22

潘栩锋's avatar
潘栩锋 committed
23
namespace FLY.Thick.Base.UI
潘栩锋's avatar
潘栩锋 committed
24 25 26 27
{
    /// <summary>
    /// Page_Curve.xaml 的交互逻辑
    /// </summary>
潘栩锋's avatar
潘栩锋 committed
28
    public partial class PgCurveModify : Page
潘栩锋's avatar
潘栩锋 committed
29
    {
潘栩锋's avatar
潘栩锋 committed
30
        PgCurveModifyVm viewModel;
潘栩锋's avatar
潘栩锋 committed
31

潘栩锋's avatar
潘栩锋 committed
32
        public PgCurveModify()
潘栩锋's avatar
潘栩锋 committed
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
        {
            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 series_orgad = new System.Windows.Forms.DataVisualization.Charting.Series();

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

            chart1.Name = "chart1";
            chart1.BackColor = System.Drawing.Color.Transparent;
            // 
            // chart1
            // 
            chartArea1.Name = "Default";
            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.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)))));
            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.ShadowColor = System.Drawing.Color.Transparent;
            //对数坐标
            //chartArea1.AxisY.IsLogarithmic = true;
            //chartArea1.AxisY.LogarithmBase = Math.E;
            this.chart1.ChartAreas.Add(chartArea1);


            legend1.Name = "Default";
            legend1.BackColor = System.Drawing.Color.Transparent;
            legend1.Enabled = true;
            legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            legend1.IsTextAutoFit = false;

            this.chart1.Legends.Add(legend1);
            //this.chart1.Location = new System.Drawing.Point(16, 32);

            series_orgad.Name = "series_orgad";
            series_orgad.BorderColor = System.Drawing.Color.LightSkyBlue;
            series_orgad.BorderWidth = 3;
            series_orgad.ChartArea = chartArea1.Name;
            series_orgad.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            series_orgad.Color = System.Drawing.Color.Blue;
            series_orgad.Legend = legend1.Name;
            series_orgad.MarkerSize = 8;
            series_orgad.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;

            series_orgad.ShadowColor = System.Drawing.Color.Black;
            series_orgad.ShadowOffset = 2;
            series_orgad.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
            series_orgad.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;

            series_orgad.LegendText = "原始曲线";

            this.chart1.Series.Add(series_orgad);

            series_revisead.Name = "series_revisead";
            series_revisead.BorderColor = System.Drawing.Color.LightPink;
            series_revisead.BorderWidth = 3;
            series_revisead.ChartArea = chartArea1.Name;
            series_revisead.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            series_revisead.Color = System.Drawing.Color.Red;
            series_revisead.Legend = legend1.Name;
            series_revisead.MarkerSize = 8;
            series_revisead.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;

            series_revisead.ShadowColor = System.Drawing.Color.Black;
            series_revisead.ShadowOffset = 2;
            series_revisead.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
            series_revisead.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;

            series_revisead.LegendText = "样品修正后曲线";
            this.chart1.Series.Add(series_revisead);
        }

120 121
        [InjectionMethod]
        public void Init(ICurveService curveService)
潘栩锋's avatar
潘栩锋 committed
122
        {
潘栩锋's avatar
潘栩锋 committed
123 124 125 126 127 128 129 130 131
            viewModel = new PgCurveModifyVm();
            viewModel.Init(curveService, chart1, this);
            this.DataContext = viewModel;
        }
    }

    public class PgCurveModifyVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
潘栩锋's avatar
潘栩锋 committed
132

潘栩锋's avatar
潘栩锋 committed
133 134 135
        #region Command
        public RelayCommand ApplyCmd { get; }
        public RelayCommand ScaleCmd { get; }
潘栩锋's avatar
潘栩锋 committed
136

潘栩锋's avatar
潘栩锋 committed
137 138 139 140 141 142 143 144 145 146 147 148 149
        public RelayCommand SetRevisedCmd { get; }
        #endregion

        CurveCore curveCore = new CurveCore();
        ICurveService curveService;
        System.Windows.Forms.DataVisualization.Charting.Chart chart1;
        Page page;

        public PgCurveModifyVm() 
        {
            ApplyCmd = new RelayCommand(Apply);
            ScaleCmd = new RelayCommand(Scale);
            SetRevisedCmd = new RelayCommand(SetRevised);
潘栩锋's avatar
潘栩锋 committed
150
        }
潘栩锋's avatar
潘栩锋 committed
151 152 153 154

        public void Init(ICurveService curveService,
            System.Windows.Forms.DataVisualization.Charting.Chart chart1,
            Page page)
潘栩锋's avatar
潘栩锋 committed
155
        {
潘栩锋's avatar
潘栩锋 committed
156 157 158
            this.curveService = curveService;
            this.chart1 = chart1;
            this.page = page;
潘栩锋's avatar
潘栩锋 committed
159

潘栩锋's avatar
潘栩锋 committed
160 161
            update_curves();
            curveService.PropertyChanged += mCurveService_PropertyChanged;
潘栩锋's avatar
潘栩锋 committed
162 163
        }

潘栩锋's avatar
潘栩锋 committed
164
        void update_curves()
潘栩锋's avatar
潘栩锋 committed
165
        {
潘栩锋's avatar
潘栩锋 committed
166 167 168 169
            curveCore.Flag = curveService.Flag;
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(curveService.Curves);
            curveCore.Curves = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CurveCell>>(json);
            DataBindAll();
潘栩锋's avatar
潘栩锋 committed
170 171 172 173 174
        }
        void mCurveService_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Flag")
            {
潘栩锋's avatar
潘栩锋 committed
175 176 177 178 179 180 181
                curveCore.Flag = curveService.Flag;
                DataBindAll();
            }
            else if (e.PropertyName == "Curves")
            {
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(curveService.Curves);
                curveCore.Curves = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CurveCell>>(json);
潘栩锋's avatar
潘栩锋 committed
182 183 184 185 186
                DataBindAll();
            }
        }

        #region DataBindAll
潘栩锋's avatar
潘栩锋 committed
187

潘栩锋's avatar
潘栩锋 committed
188 189 190 191 192
        void DataBindAll()
        {
            DataBindAll_OrgAD();
            DataBindAll_RevisedAD();
        }
潘栩锋's avatar
潘栩锋 committed
193

潘栩锋's avatar
潘栩锋 committed
194 195
        void DataBindAll_OrgAD()
        {
潘栩锋's avatar
潘栩锋 committed
196
            var curves = curveCore.Curves;
潘栩锋's avatar
潘栩锋 committed
197
            chart1.Series["series_orgad"].Points.Clear();
潘栩锋's avatar
潘栩锋 committed
198
            for (int i = 0; i < curves.Count(); i++)
潘栩锋's avatar
潘栩锋 committed
199
            {
潘栩锋's avatar
潘栩锋 committed
200 201
                int ad = curves[i].AD;
                int thick = curves[i].Value;
潘栩锋's avatar
潘栩锋 committed
202 203 204

                chart1.Series["series_orgad"].Points.AddXY(thick / 100.0, ad);

潘栩锋's avatar
潘栩锋 committed
205
                if ((i + 1) <= (curves.Count() - 1))
潘栩锋's avatar
潘栩锋 committed
206 207 208 209
                {
                    //补齐点, 以thick=100 步进
                    int step = 100;
                    bool isDescending = true;//降序排列
潘栩锋's avatar
潘栩锋 committed
210
                    if (curves[i].AD < curves[i + 1].AD)
潘栩锋's avatar
潘栩锋 committed
211 212 213 214 215 216 217
                        isDescending = false;

                    while (true)
                    {
                        if (isDescending)
                        {
                            ad -= step;
潘栩锋's avatar
潘栩锋 committed
218
                            if (ad <= curves[i + 1].AD)
潘栩锋's avatar
潘栩锋 committed
219 220 221 222 223
                                break;
                        }
                        else
                        {
                            ad += step;
潘栩锋's avatar
潘栩锋 committed
224
                            if (ad >= curves[i + 1].AD)
潘栩锋's avatar
潘栩锋 committed
225 226 227
                                break;
                        }

潘栩锋's avatar
潘栩锋 committed
228
                        thick = curveCore.AD2Value(ad, AD2ValueFlag.NoRevised);
潘栩锋's avatar
潘栩锋 committed
229 230 231 232 233 234 235 236 237
                        chart1.Series["series_orgad"].Points.AddXY(thick / 100.0, ad);
                        chart1.Series["series_orgad"].Points.Last().MarkerStyle =
                            System.Windows.Forms.DataVisualization.Charting.MarkerStyle.None;
                    }
                }
            }
        }
        void DataBindAll_RevisedAD()
        {
潘栩锋's avatar
潘栩锋 committed
238
            var curves = curveCore.Curves;
潘栩锋's avatar
潘栩锋 committed
239
            chart1.Series["series_revisead"].Points.Clear();
潘栩锋's avatar
潘栩锋 committed
240
            for (int i = 0; i < curves.Count(); i++)
潘栩锋's avatar
潘栩锋 committed
241
            {
潘栩锋's avatar
潘栩锋 committed
242 243
                int ad = curves[i].RevisedAD;
                int thick = curves[i].Value;
潘栩锋's avatar
潘栩锋 committed
244 245 246

                chart1.Series["series_revisead"].Points.AddXY(thick / 100.0, ad);

潘栩锋's avatar
潘栩锋 committed
247
                if ((i + 1) <= (curves.Count() - 1))
潘栩锋's avatar
潘栩锋 committed
248 249 250 251
                {
                    //补齐点, 以thick=100 步进
                    int step = 100;
                    bool isDescending = true;//降序排列
潘栩锋's avatar
潘栩锋 committed
252
                    if (curves[i].AD < curves[i + 1].AD)
潘栩锋's avatar
潘栩锋 committed
253 254 255 256 257 258 259
                        isDescending = false;

                    while (true)
                    {
                        if (isDescending)
                        {
                            ad -= step;
潘栩锋's avatar
潘栩锋 committed
260
                            if (ad <= curves[i + 1].AD)
潘栩锋's avatar
潘栩锋 committed
261 262 263 264 265
                                break;
                        }
                        else
                        {
                            ad += step;
潘栩锋's avatar
潘栩锋 committed
266
                            if (ad >= curves[i + 1].AD)
潘栩锋's avatar
潘栩锋 committed
267 268 269
                                break;
                        }

潘栩锋's avatar
潘栩锋 committed
270
                        thick = curveCore.AD2Value(ad, AD2ValueFlag.Revised);
潘栩锋's avatar
潘栩锋 committed
271 272 273 274 275 276 277 278 279
                        chart1.Series["series_revisead"].Points.AddXY(thick / 100.0, ad);
                        chart1.Series["series_revisead"].Points.Last().MarkerStyle =
                            System.Windows.Forms.DataVisualization.Charting.MarkerStyle.None;
                    }
                }
            }
        }
        #endregion

潘栩锋's avatar
潘栩锋 committed
280
        private void Scale()
潘栩锋's avatar
潘栩锋 committed
281
        {
潘栩锋's avatar
潘栩锋 committed
282 283
            var curves = curveCore.Curves;
            if (curves.Count > 1)
潘栩锋's avatar
潘栩锋 committed
284
            {
潘栩锋's avatar
潘栩锋 committed
285 286 287 288
                WdThickScale w = new WdThickScale();
                w.Old = curves[1].Value / 100.0;
                w.New = curves[1].Value / 100.0;
                w.Owner = FLY.ControlLibrary.COMMON.GetWindow(page);
潘栩锋's avatar
潘栩锋 committed
289 290 291 292 293
                if (w.ShowDialog() == true)
                {
                    if ((w.Old != w.New) && (w.New != 0) && (w.Old != 0))
                    {
                        double scale = (double)w.New / w.Old;
潘栩锋's avatar
潘栩锋 committed
294
                        for (int i = 0; i < curves.Count(); i++)
潘栩锋's avatar
潘栩锋 committed
295
                        {
潘栩锋's avatar
潘栩锋 committed
296
                            curves[i].Value = (int)(scale * curves[i].Value);
潘栩锋's avatar
潘栩锋 committed
297 298 299 300
                        }
                    }
                }
            }
潘栩锋's avatar
潘栩锋 committed
301
            DataBindAll();
潘栩锋's avatar
潘栩锋 committed
302 303
        }

潘栩锋's avatar
潘栩锋 committed
304
        private void SetRevised()
潘栩锋's avatar
潘栩锋 committed
305
        {
潘栩锋's avatar
潘栩锋 committed
306 307 308 309 310 311
            var curves = curveCore.Curves;
            for (int i = 0; i < curves.Count(); i++)
            {
                curves[i].AD = curves[i].RevisedAD;
            }
            DataBindAll();
潘栩锋's avatar
潘栩锋 committed
312 313
        }

潘栩锋's avatar
潘栩锋 committed
314
        private void Apply()
潘栩锋's avatar
潘栩锋 committed
315
        {
潘栩锋's avatar
潘栩锋 committed
316 317 318 319 320 321 322 323 324
            if (!WdPassword.Authorize("Curve"))
                return;

            curveService.Curves = curveCore.Curves;
            curveService.Apply();
            FLY.ControlLibrary.Window_Tip.Show("应用成功",
                null,
                TimeSpan.FromSeconds(2));
            
潘栩锋's avatar
潘栩锋 committed
325 326 327
        }
    }
}