using FLY.FeedbackRenZiJia.Client;
using FLY.FeedbackRenZiJia.IService;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
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.Navigation;
using System.Windows.Shapes;
using ThickTcpUiInWindow;
using System.Globalization;
using FLY.FeedbackRenZiJia.Common;
using FLY.Thick.Blowing.IService;

namespace FLY.FeedbackRenZiJia.UI.Client
{
    /// <summary>
    /// Page_HeatList.xaml 的交互逻辑
    /// </summary>
    public partial class Page_HeatList : Page,INotifyPropertyChanged
    {
        private HeatList2 mHeat2 = new HeatList2();
        private SnapShotList mSnaps = new SnapShotList();
        private HeatListHelper mHelper = new HeatListHelper();

        private FeedbackHeatServiceClient mFeedback;
        private HeatBufServiceClient mHeatBuf;
        private HeatCellServiceClient mHeatCell;
        private FLY.Thick.Blowing.Client.BlowingServiceClient mBlowing;
        private FLY.Thick.Blowing.Client.BlowingDetectServiceClient mBDetect;
        public Page_HeatList()
        {
            InitializeComponent();
            InitializeChart();
            Init();
        }

        public void Init()
        {
            mFeedback = TDGage.Current.mFeedback;
            mHeatBuf = TDGage.Current.mHeatBuf;
            mHeatCell = TDGage.Current.mHeatCell;
            mBlowing = TDGage.Current.mBlowing;
            mBDetect = TDGage.Current.mBDetect;

            mHeat2.Init();
            mSnaps.Init(mHeat2);
            mHelper.Init(mSnaps);

            mHeat2.PropertyChanged += MHeat2_PropertyChanged;
            mHelper.PropertyChanged += MHelper_PropertyChanged;

            UpdateAxisY();

            this.DataContext = mHelper;
            this.tabitem_heat2.DataContext = mHeat2;
            this.stackpanel_flydata1.DataContext = mHeat2.flydata1;
            this.stackpanel_flydata2.DataContext = mHeat2.flydata2;

            this.stackpanel_bmRange.DataContext = mHeatBuf;
            this.items_snapshot.ItemsSource = mSnaps.mList;
            this.stackpanel_snap_opt.DataContext = mSnaps;
            mSnaps.Refresh();
            
            Misc.BindingOperations.SetBinding(mBlowing, "OrgBoltNo", mHelper, "OrgBoltNo");
            Misc.BindingOperations.SetBinding(mBDetect, "RAngle", mHelper, "RAngle");
            
            InitMap();
            Init_BoltHeat();
        }


        private void MHeat2_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Data1")
            {
                UpdateData1();
                UpdateAxisY();
            }
            if (e.PropertyName == "Data2")
            {
                UpdateData2();
                UpdateAxisY();
            }
        }

        private void MHelper_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Factor")
            {
                UpdateDataDiff();
                UpdateAxisY();
            }
            else if (e.PropertyName == "DataDiff")
            {
                UpdateDataDiff();
                UpdateAxisY();
            }
        }

        void InitializeChart()
        {
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea3 = 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.Legend legend2 = new System.Windows.Forms.DataVisualization.Charting.Legend();
            System.Windows.Forms.DataVisualization.Charting.Legend legend3 = 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.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.Series series3 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.Series series4 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.Series series5 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.Series series6 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.Series series7 = new System.Windows.Forms.DataVisualization.Charting.Series();

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


            #region chartArea
            #region chartArea1
            chartArea1.Name = "chartArea1";

            chartArea1.AxisX.IsLabelAutoFit = false;
            chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisX.LabelStyle.IsEndLabelVisible = false;
            chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea1.AxisX.Interval = 5;


            chartArea1.AxisY.IsLabelAutoFit = true;
            chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea1.AxisY.IsStartedFromZero = true;
            chartArea1.AxisY.Title = "加热功率(%)";

            chartArea1.AxisY.Maximum = 100;
            chartArea1.AxisY.Minimum = 0;
            chartArea1.AxisY.Interval = 10;

            chartArea1.AxisY2.IsLabelAutoFit = true;
            chartArea1.AxisY2.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisY2.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea1.AxisY2.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea1.AxisY2.IsStartedFromZero = true;
            chartArea1.AxisY2.IntervalAutoMode = IntervalAutoMode.FixedCount;
            chartArea1.AxisY2.Title = "厚度(%)";

            chartArea1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(165)), ((System.Byte)(191)), ((System.Byte)(228)));
            chartArea1.BackSecondaryColor = System.Drawing.Color.White;
            chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            chartArea1.BorderColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea1.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;

            chartArea1.Position.Auto = false;// true;
            chartArea1.Position.Height = 100F;
            chartArea1.Position.Width = 100F;
            chartArea1.Position.X = 0;
            chartArea1.Position.Y = 0;
            chartArea1.ShadowColor = System.Drawing.Color.Transparent;


            #endregion

            #region chartArea2

            chartArea2.Name = "chartArea2";

            chartArea2.AxisX.IsLabelAutoFit = false;
            chartArea2.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea2.AxisX.LabelStyle.IsEndLabelVisible = false;
            chartArea2.AxisX.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea2.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea2.AxisX.Interval = 5;

            chartArea2.AxisY.IsLabelAutoFit = true;
            chartArea2.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea2.AxisY.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea2.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea2.AxisY.IsStartedFromZero = true;
            chartArea2.AxisY.Title = "加热功率(%)";

            chartArea2.AxisY.Maximum = 100;
            chartArea2.AxisY.Minimum = 0;
            chartArea2.AxisY.Interval = 10;

            chartArea2.AxisY2.IsLabelAutoFit = true;
            chartArea2.AxisY2.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea2.AxisY2.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea2.AxisY2.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea2.AxisY2.IsStartedFromZero = true;
            chartArea2.AxisY2.IntervalAutoMode = IntervalAutoMode.FixedCount;
            chartArea2.AxisY2.Title = "厚度(%)";

            chartArea2.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(165)), ((System.Byte)(191)), ((System.Byte)(228)));
            chartArea2.BackSecondaryColor = System.Drawing.Color.White;
            chartArea2.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            chartArea2.BorderColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea2.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;

            //chartArea2.AlignWithChartArea = chartArea1.Name;
            chartArea2.Position.Auto = false;
            chartArea2.Position.Height = 100F;
            chartArea2.Position.Width = 100F;
            chartArea2.Position.X = 0;
            chartArea2.Position.Y = 0;
            chartArea2.ShadowColor = System.Drawing.Color.Transparent;

            #endregion

            #region chartArea3

            chartArea3.Name = "chartArea3";

            chartArea3.AxisX.IsLabelAutoFit = false;
            chartArea3.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea3.AxisX.LabelStyle.IsEndLabelVisible = false;
            chartArea3.AxisX.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea3.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea3.AxisX.Interval = 5;

            chartArea3.AxisY.IsLabelAutoFit = true;
            chartArea3.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea3.AxisY.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea3.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea3.AxisY.IsStartedFromZero = true;
            chartArea3.AxisY.Title = "加热功率偏差(%)";

            chartArea3.AxisY.Maximum = 100;
            chartArea3.AxisY.Minimum = -100;
            chartArea3.AxisY.Interval = 20;

            chartArea3.AxisY2.IsLabelAutoFit = true;
            chartArea3.AxisY2.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea3.AxisY2.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea3.AxisY2.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea3.AxisY2.IsStartedFromZero = true;
            chartArea3.AxisY2.IntervalAutoMode = IntervalAutoMode.FixedCount;
            chartArea3.AxisY2.Title = "厚度偏差(%)";

            chartArea3.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(165)), ((System.Byte)(191)), ((System.Byte)(228)));
            chartArea3.BackSecondaryColor = System.Drawing.Color.White;
            chartArea3.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            chartArea3.BorderColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            chartArea3.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;

            chartArea3.Position.Auto = false;// true;
            chartArea3.Position.Height = 100F;
            chartArea3.Position.Width = 100F;
            chartArea3.Position.X = 0;
            chartArea3.Position.Y = 0;
            chartArea3.ShadowColor = System.Drawing.Color.Transparent;

            #endregion
            this.chart1.ChartAreas.Add(chartArea1);
            this.chart2.ChartAreas.Add(chartArea2);
            this.chart3.ChartAreas.Add(chartArea3);
            #endregion

            #region legend
            #region legend1
            legend1.Name = "legend1";
            legend1.Alignment = System.Drawing.StringAlignment.Near;
            legend1.IsTextAutoFit = false;
            legend1.BackColor = System.Drawing.Color.Transparent;
            legend1.Docking = Docking.Right;
            legend1.IsDockedInsideChartArea = true;
            legend1.DockedToChartArea = chartArea1.Name;
            legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            legend1.LegendStyle = LegendStyle.Column;

            legend1.Position.Auto = true;

            #endregion

            #region legend2
            legend2.Name = "legend2";
            legend2.Alignment = System.Drawing.StringAlignment.Near;
            legend2.IsTextAutoFit = false;
            legend2.BackColor = System.Drawing.Color.Transparent;
            legend2.Docking = Docking.Right;
            legend2.IsDockedInsideChartArea = true;
            legend2.DockedToChartArea = chartArea2.Name;
            legend2.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            legend2.LegendStyle = LegendStyle.Column;

            legend2.Position.Auto = true;

            #endregion

            #region legend3
            legend3.Name = "legend3";
            legend3.Alignment = System.Drawing.StringAlignment.Near;
            legend3.IsTextAutoFit = false;
            legend3.BackColor = System.Drawing.Color.Transparent;
            legend3.Docking = Docking.Right;
            legend3.IsDockedInsideChartArea = true;
            legend3.DockedToChartArea = chartArea3.Name;
            legend3.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            legend3.LegendStyle = LegendStyle.Column;

            legend3.Position.Auto = true;
            #endregion
            chart1.Legends.Add(legend1);
            chart2.Legends.Add(legend2);
            chart3.Legends.Add(legend3);
            #endregion

            #region series
            #region series1
            series1.Name = "series_heats1";
            series1.BorderColor = System.Drawing.Color.FromArgb(((System.Byte)(180)), ((System.Byte)(26)), ((System.Byte)(59)), ((System.Byte)(105)));
            series1.BorderWidth = 2;
            series1.ChartArea = chartArea1.Name;
            series1.ChartType = SeriesChartType.Column;
            series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));

            series1.XValueType = ChartValueType.Int32;
            series1.YValueType = ChartValueType.Int32;

            series1.ShadowColor = System.Drawing.Color.Black;
            series1.ShadowOffset = 2;

            series1.IsVisibleInLegend = true;
            series1.Legend = legend1.Name;
            series1.LegendText = "加热";
            #endregion

            #region series2
            series2.Name = "series_thicks1";
            series2.BorderColor = System.Drawing.Color.Red;
            series2.BorderWidth = 2;
            series2.ChartArea = chartArea1.Name;
            series2.ChartType = SeriesChartType.Line;
            series2.Color = System.Drawing.Color.HotPink;

            series2.XValueType = ChartValueType.Int32;
            series2.YValueType = ChartValueType.Double;
            series2.YAxisType = AxisType.Secondary;

            series2.ShadowColor = System.Drawing.Color.Black;
            series2.ShadowOffset = 2;

            series2.IsVisibleInLegend = true;
            series2.Legend = legend1.Name;
            series2.LegendText = "厚度";
            #endregion

            #region series3
            series3.Name = "series_heats2";
            series3.BorderColor = System.Drawing.Color.FromArgb(((System.Byte)(180)), ((System.Byte)(26)), ((System.Byte)(59)), ((System.Byte)(105)));
            series3.BorderWidth = 2;
            series3.ChartArea = chartArea2.Name;
            series3.ChartType = SeriesChartType.Column;
            series3.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));

            series3.XValueType = ChartValueType.Int32;
            series3.YValueType = ChartValueType.Int32;

            series3.ShadowColor = System.Drawing.Color.Black;
            series3.ShadowOffset = 2;

            series3.IsVisibleInLegend = true;
            series3.Legend = legend2.Name;
            series3.LegendText = "加热";
            #endregion

            #region series4
            series4.Name = "series_thicks2";
            series4.BorderColor = System.Drawing.Color.Red;
            series4.BorderWidth = 2;
            series4.ChartArea = chartArea2.Name;
            series4.ChartType = SeriesChartType.Line;
            series4.Color = System.Drawing.Color.HotPink;

            series4.XValueType = ChartValueType.Int32;
            series4.YValueType = ChartValueType.Double;
            series4.YAxisType = AxisType.Secondary;

            series4.ShadowColor = System.Drawing.Color.Black;
            series4.ShadowOffset = 2;

            series4.IsVisibleInLegend = true;
            series4.Legend = legend2.Name;
            series4.LegendText = "厚度";
            #endregion

            #region series5
            series5.Name = "series_heats3";
            series5.BorderColor = System.Drawing.Color.FromArgb(((System.Byte)(180)), ((System.Byte)(26)), ((System.Byte)(59)), ((System.Byte)(105)));
            series5.BorderWidth = 2;
            series5.ChartArea = chartArea3.Name;
            series5.ChartType = SeriesChartType.Column;
            series5.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));

            series5.XValueType = ChartValueType.Int32;
            series5.YValueType = ChartValueType.Int32;

            series5.ShadowColor = System.Drawing.Color.Black;
            series5.ShadowOffset = 2;

            series5.IsVisibleInLegend = true;
            series5.Legend = legend3.Name;
            series5.LegendText = "加热偏差";
            #endregion

            #region series6
            series6.Name = "series_thicks3";
            series6.BorderColor = System.Drawing.Color.Red;
            series6.BorderWidth = 2;
            series6.ChartArea = chartArea3.Name;
            series6.ChartType = SeriesChartType.Line;
            series6.Color = System.Drawing.Color.HotPink;

            series6.XValueType = ChartValueType.Int32;
            series6.YValueType = ChartValueType.Double;
            series6.YAxisType = AxisType.Secondary;

            series6.ShadowColor = System.Drawing.Color.Black;
            series6.ShadowOffset = 2;

            series6.IsVisibleInLegend = true;
            series6.Legend = legend3.Name;
            series6.LegendText = "厚度偏差";
            #endregion

            #region series7
            series7.Name = "series_heats3effect";
            series7.BorderColor = series5.BorderColor;
            series7.BorderWidth = 2;
            series7.ChartArea = chartArea3.Name;
            series7.ChartType = SeriesChartType.Line;
            series7.Color = series5.Color;

            series7.XValueType = ChartValueType.Int32;
            series7.YValueType = ChartValueType.Double;
            series7.YAxisType = AxisType.Secondary;

            series7.ShadowColor = System.Drawing.Color.Black;
            series7.ShadowOffset = 2;

            series7.IsVisibleInLegend = true;
            series7.Legend = legend3.Name;
            series7.LegendText = "加热效果";
            #endregion
            chart1.Series.Add(series1);
            chart1.Series.Add(series2);
            chart2.Series.Add(series3);
            chart2.Series.Add(series4);
            chart3.Series.Add(series5);
            chart3.Series.Add(series6);
            chart3.Series.Add(series7);
            #endregion

            chart1.Name = "chart1";
            chart1.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.BrightPastel;
            chart1.Location = new System.Drawing.Point(0, 0);

            chart2.Name = "chart2";
            chart2.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.BrightPastel;
            chart2.Location = new System.Drawing.Point(0, 0);

            chart3.Name = "chart3";
            chart3.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.BrightPastel;
            chart3.Location = new System.Drawing.Point(0, 0);

            //??????????????????????????????????????
            //((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();

        }


        void DataBindAll(Series series, IEnumerable<int> datas, int merge)
        {
            //Series series = chart1.Series[series_string];
            series.Points.Clear();

            for (int i = 0; i < datas.Count() / merge; i++)
            {
                int d = Misc.MyMath.Avg(datas, i * merge, (i + 1) * merge - 1);
                series.Points.AddXY(
                    1 + i, d);
                DataPoint point = series.Points[i];

                //设定空数据
                if (!Misc.MyBase.ISVALIDATA(d))
                    point.IsEmpty = true;
                else
                    point.IsEmpty = false;
            }
        }
        void DataBindAll(Series series, IEnumerable<int> datas)
        {
            //Series series = chart1.Series[series_string];
            series.Points.Clear();

            for (int i = 0; i < datas.Count(); i++)
            {
                int d = datas.ElementAt(i);
                series.Points.AddXY(
                    1 + i, d);
                DataPoint point = series.Points[i];

                //设定空数据
                if (!Misc.MyBase.ISVALIDATA(d))
                    point.IsEmpty = true;
                else
                    point.IsEmpty = false;
            }
        }
        void UpdateAxisX(int cnt)
        {
            Chart[] charts = new Chart[3] { chart1, chart2, chart3 };
            for (int i = 0; i < 3; i++)
            {
                ChartArea chartarea = charts[i].ChartAreas[0];
                chartarea.AxisX.Minimum = 0;
                chartarea.AxisX.Maximum = cnt + 1;
            }
        }
        void UpdateAxisY()
        {
            int range = (int)(100 / mHelper.Factor);

            {
                ChartArea chartarea = chart1.ChartAreas[0];

                chartarea.AxisY2.Minimum = -(range / 2);
                chartarea.AxisY2.Maximum = range / 2;
            }

            {
                ChartArea chartarea = chart2.ChartAreas[0];

                chartarea.AxisY2.Minimum = -(range / 2);
                chartarea.AxisY2.Maximum = range / 2;
            }

            {
                ChartArea chartarea = chart3.ChartAreas[0];

                chartarea.AxisY2.Minimum = -(range);
                chartarea.AxisY2.Maximum = range;
            }

            {
                ChartArea chartarea = chart3.ChartAreas[0];

                int avg = (int)mHelper.HeatDiff.Average();
                chartarea.AxisY.Minimum = avg - 100;
                chartarea.AxisY.Maximum = avg + 100;
            }
        }

        void Test()
        {
            int boltcnt = 96;


            int[] heat1 = new int[boltcnt];
            for (int i = 0; i < boltcnt; i++)
            {
                heat1[i] = (int)(Math.Sin(Math.PI * i / boltcnt) * 30 + 15);
            }

            int[] heat2 = new int[boltcnt];
            for (int i = 0; i < boltcnt; i++)
            {
                heat2[i] = (int)(Math.Cos(Math.PI * i / boltcnt) * 30 + 15);
            }

            int[] heat3 = new int[boltcnt];
            for (int i = 0; i < boltcnt; i++)
            {
                heat3[i] = heat1[i] - heat2[i];
            }



            int[] thick1 = new int[boltcnt];
            for (int i = 0; i < boltcnt; i++)
            {
                thick1[i] = (int)(Math.Sin(Math.PI * i / boltcnt) * 30 + 15);
            }
            int avg = (int)thick1.Average();
            for (int i = 0; i < boltcnt; i++)
            {
                thick1[i] = 100 * (thick1[i] - avg) / avg;
            }


            int[] thick2 = new int[boltcnt];
            for (int i = 0; i < boltcnt; i++)
            {
                thick2[i] = (int)(Math.Cos(Math.PI * i / boltcnt) * 30 + 15);
            }
            avg = (int)thick2.Average();
            for (int i = 0; i < boltcnt; i++)
            {
                thick2[i] = 100 * (thick2[i] - avg) / avg;
            }



            int[] thick3 = new int[boltcnt];
            for (int i = 0; i < boltcnt; i++)
            {
                thick3[i] = thick2[i] - thick1[i];
            }



            UpdateAxisX(boltcnt);
            DataBindAll(chart1.Series["series_heats1"], heat1, 1);
            DataBindAll(chart2.Series["series_heats2"], heat2, 1);
            DataBindAll(chart3.Series["series_heats3"], heat3, 1);

            DataBindAll(chart1.Series["series_thicks1"], thick1, 1);
            DataBindAll(chart2.Series["series_thicks2"], thick2, 1);
            DataBindAll(chart3.Series["series_thicks3"], thick3, 1);
        }
        void UpdateData1()
        {
            UpdateAxisX(mFeedback.ChannelCnt);

            DataBindAll(chart1.Series["series_heats1"], mHeat2.Heats[0]);

            int bpc = mFeedback.NBolts / mFeedback.ChannelCnt;
            DataBindAll(chart1.Series["series_thicks1"], mHeat2.ThickPercents[0], bpc);
        }
        void UpdateData2()
        {
            UpdateAxisX(mFeedback.ChannelCnt);

            DataBindAll(chart2.Series["series_heats2"], mHeat2.Heats[1]);

            int bpc = mFeedback.NBolts / mFeedback.ChannelCnt;
            DataBindAll(chart2.Series["series_thicks2"], mHeat2.ThickPercents[1], bpc);
        }
        void UpdateDataDiff()
        {
            UpdateAxisX(mFeedback.ChannelCnt);

            //mHelper.HeatDiff
            DataBindAll(chart3.Series["series_heats3"], mHelper.HeatDiff);

            int bpc = mFeedback.NBolts / mFeedback.ChannelCnt;



            
            if ((mBlowing.IsUsedMap != IsUsedMap) || //状态不一样,需要调整!!!!!
                ((mBlowing.IsUsedMap == true) && (!IsMapSame())))//修改了分区表,需要调整!!!!!
            {
                Dictionary<int, int> map = new Dictionary<int, int>();
                int[] thicks;
                //还原
                if ((mBlowing.IsUsedMap == false)||(mBlowing.Map.Count()==0))
                {
                    thicks = mHelper.ThickPercentDiff;
                }
                else
                {
                    IEnumerable<BoltMapCell> maps = mBlowing.Map;
                    for (int i = 0; i < maps.Count(); i++)
                        map.Add(maps.ElementAt(i).NewNo - 1, maps.ElementAt(i).OldNo - 1);
                    thicks = Misc.MyMath.Map(mHelper.ThickPercentDiff, map);
                }

                //再转换
                map.Clear();
                if ((IsUsedMap == false) || (BoltMap.Count() == 0))
                {
                    //什么都不用干
                }
                else
                {
                    IEnumerable<BoltMapCell> maps = BoltMap;
                    for (int i = 0; i < maps.Count(); i++)
                        map.Add(maps.ElementAt(i).OldNo - 1, maps.ElementAt(i).NewNo - 1);
                    thicks = Misc.MyMath.Map(thicks, map);
                }

                DataBindAll(chart3.Series["series_thicks3"], thicks, bpc);
            }
            else
            {
                DataBindAll(chart3.Series["series_thicks3"], mHelper.ThickPercentDiff, bpc);
            }

            int[] heatdiff = FLY.FeedbackRenZiJia.Common.MyMath.ZoomIn(mHelper.HeatDiff, bpc);
            heatdiff= FLY.FeedbackRenZiJia.Common.MyMath.OffsetAvgBe0(heatdiff);
            int[] heateffect = FLY.FeedbackRenZiJia.Common.MyMath.Filter(heatdiff, HeatEffectCurve.ToArray());

            double kp = mHelper.Factor;
            if (kp < 1)
                kp = 1;

            heateffect = FLY.FeedbackRenZiJia.Common.MyMath.ZoomY(heateffect, 1/kp);
            DataBindAll(chart3.Series["series_heats3effect"], heateffect, bpc);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {

            //button_set_Click(null, null);

            //Test();
            mHeat2.UpdateBookmark();
        }

        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {

        }

        private void button_back_Click(object sender, RoutedEventArgs e)
        {

            //if (w_set != null)
            //{
            //    w_set.Close();
            //}
            NavigationService.GoBack();
        }

        private void button_pre1_Click(object sender, RoutedEventArgs e)
        {
            if(mHeat2.Bookmark1>mHeatBuf.FirstBM)
                mHeat2.Bookmark1--;
        }

        private void button_next1_Click(object sender, RoutedEventArgs e)
        {
            if (mHeat2.Bookmark1 < mHeatBuf.LastBM)
                mHeat2.Bookmark1++;
        }

        private void button_pre2_Click(object sender, RoutedEventArgs e)
        {
            if (mHeat2.Bookmark2 > mHeatBuf.FirstBM)
                mHeat2.Bookmark2--;
        }

        private void button_next2_Click(object sender, RoutedEventArgs e)
        {
            if (mHeat2.Bookmark2< mHeatBuf.LastBM)
                mHeat2.Bookmark2++;
        }

        private void button_collapse_Click(object sender, RoutedEventArgs e)
        {
            if (mHelper.RAngle > 300)
                mHelper.RAngle -= 1;
        }

        private void button_expand_Click(object sender, RoutedEventArgs e)
        {
            if (mHelper.RAngle < 358)
                mHelper.RAngle += 1;
        }
        private void button_left_Click(object sender, RoutedEventArgs e)
        {
            if (mHelper.OrgBoltNo > 1)
                mHelper.OrgBoltNo--;
            else
                mHelper.OrgBoltNo = mFeedback.NBolts;
        }

        private void button_right_Click(object sender, RoutedEventArgs e)
        {
            if (mHelper.OrgBoltNo < mFeedback.NBolts)
                mHelper.OrgBoltNo++;
            else
                mHelper.OrgBoltNo = 1;

        }

        private void button_factor_expand_Click(object sender, RoutedEventArgs e)
        {
            if (mHelper.Factor > 0.2)
                mHelper.Factor -= 0.1;
        }

        private void button_factor_collapse_Click(object sender, RoutedEventArgs e)
        {
            if (mHelper.Factor < 4.9)
                mHelper.Factor += 0.1;
        }

        private void button_auto_Click(object sender, RoutedEventArgs e)
        {
            mHelper.Auto();
        }

        private void button_snap_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            SnapShot ss = b.Tag as SnapShot;
            if (ss.IsFocused)
                return;
            mSnaps.GetSnapShot(ss.Bookmark);
        }

        private void button_setsnap_Click(object sender, RoutedEventArgs e)
        {
            mSnaps.SetSnapShot();
        }

        private void button_delsnap_Click(object sender, RoutedEventArgs e)
        {
            if (Window_Password.Authorize(1))
            {
                mSnaps.DelSnapShot();
            }
        }

        private void button_refresh_Click(object sender, RoutedEventArgs e)
        {
            mSnaps.Refresh();
        }

        private void button_clearBoltHeat_Click(object sender, RoutedEventArgs e)
        {
            mHeatBuf.ClearBoltHeat();
            FLY.ControlLibrary.Window_Tip.Show("通知", "分区记录删除成功", TimeSpan.FromSeconds(1));
        }

        ObservableCollection<BoltMapCell> BoltMap = new ObservableCollection<BoltMapCell>();



        private List<int> heatEffectCurve = new List<int>();
        /// <summary>
        /// 加热生效曲线,允许为空,则不生效
        /// </summary>
        public List<int> HeatEffectCurve
        {
            get
            {
                return heatEffectCurve;
            }
            set
            {
                heatEffectCurve = value;
                NotifyPropertyChanged("HeatEffectCurve");
            }
        }

        private bool isUsedMap = false;
        public bool IsUsedMap
        {
            get { return isUsedMap; }
            set {
                if (isUsedMap != value)
                {
                    isUsedMap = value;
                    NotifyPropertyChanged("IsUsedMap");
                }
            }
        }

        #region 分区表 & 加热效果
        void InitMap()
        {
            BoltMap.Clear();
            
            for (int i = 0; i < mBlowing.Map.Count(); i++)
            {
                BoltMap.Add(mBlowing.Map[i].Clone());
            }
            
            datagrid.ItemsSource = BoltMap;
            IsUsedMap = mBlowing.IsUsedMap;
            HeatEffectCurve = mHeatBuf.HeatEffectCurve;
            stackpanel_isUsedBoltMap.DataContext = this;
            stackpanel_heateffectcurve.DataContext = this;

            this.PropertyChanged += Page_HeatList_PropertyChanged;
        }

        private void Page_HeatList_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "HeatEffectCurve")
            {
                if(CheckValid_HeatEffectCurve())
                    UpdateDataDiff();
            }
            else if (e.PropertyName == "IsUsedMap")
            {
                if(checkValid_map())
                    UpdateDataDiff();
            }
        }

        bool IsMapSame()
        {
            if (BoltMap.Count() != mBlowing.Map.Count())
                return false;
            for (int i = 0; i < mBlowing.Map.Count(); i++)
            {
                BoltMapCell modify = BoltMap[i];
                BoltMapCell used = mBlowing.Map[i];

                if (modify.NewNo != used.NewNo)
                    return false;

                if (modify.OldNo != used.OldNo)
                    return false;
            }
            return true;
        }
        private void button_delmap_Click(object sender, RoutedEventArgs e)
        {
            BoltMap.Clear();
        }

        private void button_testmap_Click(object sender, RoutedEventArgs e)
        {
            if (!checkValid_map())
            {
                FLY.ControlLibrary.Window_WarningTip.Show("异常", "分区表格式出错");
                return;
            }
            UpdateDataDiff();
        }

        bool checkValid_map()
        {
            bool map_err = false;
            bool g_o = false;
            bool g_n = false;
            //Map 检测
            //OldNo,NewNo不能 <1 >NBolts
            //Map OldNo 必须从小到大环形排列
            //Map NewNo 必须从小到大环形排列
            for (int i = 0; i < BoltMap.Count(); i++)
            {
                if (BoltMap[i].NewNo < 1 || BoltMap[i].NewNo > mFeedback.NBolts)
                {
                    map_err = true;
                    break;
                }
                if (BoltMap[i].OldNo < 1 || BoltMap[i].OldNo > mFeedback.NBolts)
                {
                    map_err = true;
                    break;
                }

                int i_next = i + 1;
                if (i_next >= BoltMap.Count())
                    i_next = 0;

                if (BoltMap[i].OldNo >= BoltMap[i_next].OldNo)
                {
                    if (!g_o)
                        g_o = true;
                    else
                    {
                        map_err = true;
                        break;
                    }
                    
                }

                if (BoltMap[i].NewNo >= BoltMap[i_next].NewNo)
                {
                    if (!g_n)
                        g_n = true;
                    else
                    {
                        map_err = true;
                        break;
                    }
                    
                }
            }
            return !map_err;
        }

        bool CheckValid_HeatEffectCurve()
        {
            List<int> list = HeatEffectCurve;

            if ((list == null) || (list.Count() == 0))
            {
                return false;
            }
            int cnt = list.Count();
            if ((int)(cnt / 2) * 2 == cnt)
            {
                return false;
            }
            for (int i = 0; i < list.Count() / 2; i++)
            {
                int idx0 = i;
                int idx_0 = list.Count() - 1 - idx0;
                if (list[idx0] != list[idx_0])
                {
                    return false;
                }
                int idx1 = idx0 + 1;
                if (list[idx0] > list[idx1])
                {
                    return false;
                }
            }
            return true;
        }
        private void button_applymap_Click(object sender, RoutedEventArgs e)
        {
            if (mHelper.IsReversed)
            {
                FLY.ControlLibrary.Window_WarningTip.Show("应用失败", "当前图是反向,需要先去调换旋转架的正反向信号线", TimeSpan.FromSeconds(2));
                return;
            }
            if (Window_Password.Authorize(1))
            {
                mBlowing.IsUsedMap = IsUsedMap;
                mBlowing.Map = BoltMap.ToList();
                mBlowing.OrgBoltNo = mHelper.OrgBoltNo;

                mBlowing.Apply();

                mBDetect.RAngle = mHelper.RAngle;
                mBDetect.Apply();


                if (CheckValid_HeatEffectCurve())
                {
                    mHeatBuf.HeatEffectCurve = HeatEffectCurve;
                }

                if (mHeatBuf.IsUsedLocalKp)
                {
                    mHeatBuf.LocalKp = mHelper.Factor;
                }
                mHeatBuf.Apply();


                if (!mHeatBuf.IsUsedLocalKp)
                {
                    mHeatCell.Kp = mHelper.Factor;
                    mHeatCell.Apply();
                }
                mHeatBuf.Apply();

                FLY.ControlLibrary.Window_Tip.Show("通知", "应用成功", TimeSpan.FromSeconds(2));
            }
        }

        #endregion
        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }


        #endregion

        #region 分区记录
        private bool isBoltHeatLoading=false;
        public bool IsBoltHeatLoading
        {
            get { return isBoltHeatLoading; }
            set {
                if(isBoltHeatLoading!=value)
                {
                    isBoltHeatLoading = value;
                    NotifyPropertyChanged("IsBoltHeatLoading");
                }
            }
        }

        private int getNo = 1;
        public int GetNo
        {
            get { return getNo; }
            set
            {
                if (getNo != value)
                {
                    getNo = value;
                    NotifyPropertyChanged("GetNo");
                }
            }
        }

        ObservableCollection<BoltHeatRecord> mStableList = new ObservableCollection<BoltHeatRecord>();
        ObservableCollection<BoltHeatRecord> mUnstableList = new ObservableCollection<BoltHeatRecord>();

        void Init_BoltHeat()
        {
            grid_stable.ItemsSource = mStableList;
            grid_unstable.ItemsSource = mUnstableList;
            this.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == "GetNo")
                {
                    LoadBoltHeat();
                }
            };

            stackpanel_boltheat.DataContext = this;
        }
        private void button_preno_Click(object sender, RoutedEventArgs e)
        {
            if (GetNo > 1)
                GetNo--;
        }

        private void button_nextno_Click(object sender, RoutedEventArgs e)
        {
            if (GetNo < mFeedback.NBolts)
                GetNo++;
        }
        void LoadBoltHeat()
        {
            if (GetNo < 0 || GetNo > mFeedback.NBolts)
                return;

            IsBoltHeatLoading = true;
            mHeatBuf.GetBoltHeatRecord(
                GetNo,
                (AsyncState, retData) =>
                {
                    FlyData_BoltHeatRecord reponse = retData as FlyData_BoltHeatRecord;

                    IsBoltHeatLoading = false;
                    mStableList.Clear();
                    mUnstableList.Clear();
                    if (reponse.StableList != null)
                    {
                        for (int i = 0; i < reponse.StableList.Count(); i++)
                            mStableList.Add(reponse.StableList[i]);
                    }

                    if (reponse.UnstableList != null)
                    {
                        for (int i = 0; i < reponse.UnstableList.Count(); i++)
                            mUnstableList.Add(reponse.UnstableList[i]);
                    }

                }, null);
        }
        private void button_boltheat_download_Click(object sender, RoutedEventArgs e)
        {
            LoadBoltHeat();
        }
        #endregion


    }

    public class Bookmark2StringValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int bookmark = (int)value;
            if (bookmark == 0)
            {
                return "当前";
            }
            else
            {
                return "记录" + bookmark.ToString();
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}