using System;
using System.Drawing;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using ZedGraph;
using CRVM.CIDExcuter;
using CRVM.Entity;

namespace CRVM
{
    public partial class Plotting : DockContent
    {
        private MasterPane master;
        private Machine mac = null;
        GraphPane gpt;
        private double[] xAxisEnergy;

        public Plotting(Machine mac)
        {
            this.mac = mac;
            InitializeComponent();
        }

        private void Plotting_Load(object sender, EventArgs e)
        {
            GraRefresh();
        }
        public void GraRefresh()
        {
            if (mac != null)
            {
                zedGraphControl1.IsShowPointValues = false;
                master = new MasterPane();
                master.PaneList.Clear();
                xAxisEnergy = mac.xAxisEnergy.ToArray();
                if (SysParam.Instance.orderByOther)
                {
                    if (SysParam.Instance.orderByChannel)
                    {
                        //for (int i = 0; i < SysParam.Instance.channelCount; i++)
                        for (int i = SysParam.Instance.channelCount - 1; i >= 0; i--)
                        {
                            if (SysParam.Instance.channel[i].ChChecked)
                            {
                                if (SysParam.Instance.checkBoxTime)
                                    OrderByChannel_Time(mac.Stand[i], i);
                                if (SysParam.Instance.checkBoxEnergy)
                                    OrderByChannel_Energy(mac.Stand[i], i);
                            }
                        }
                    }
                    else
                    {
                        if (SysParam.Instance.checkBoxTime)
                            OrderByKind_Time();
                        if (SysParam.Instance.checkBoxEnergy)
                            OrderByKind_Energy();
                    }
                }
                else if (SysParam.Instance.orderByStandnoR2L)
                {
                    if (SysParam.Instance.checkBoxTime)
                        OrderByKind_Time();
                    if (SysParam.Instance.checkBoxEnergy)
                        OrderByKind_Energy();
                }


                else if (SysParam.Instance.orderByStandnoL2R)
                {
                    if (SysParam.Instance.checkBoxTime)
                        OrderByKind_Time2();
                    if (SysParam.Instance.checkBoxEnergy)
                        OrderByKind_Energy2();
                }

                master.Border.IsVisible = false;
                master.Margin.All = 5f;
                master.InnerPaneGap = 5f;
                master.Fill = new Fill(SysParam.Instance.colorBackgroud);
                master.AxisChange();
                zedGraphControl1.MasterPane = master;

                if (SysParam.Instance.orderByStandnoR2L|| SysParam.Instance.orderByStandnoL2R)
                {
                    int colNumTemp = 0;//显示的列数，按机架排列
                    for (int i = 0; i < SysParam.Instance.SpeedChannelCount; i++)
                    {
                        if (SysParam.Instance.channel[i].ChChecked)
                        {
                            colNumTemp++;
                        }
                    }

                    int picNumber = 0;
                    if (SysParam.Instance.checkBoxTime)
                    {
                        if (SysParam.Instance.checkBoxTimeAll)
                        {
                            picNumber++;
                        }
                        if (SysParam.Instance.checkBoxTimeL)
                        {
                            picNumber++;
                        }
                        if (SysParam.Instance.checkBoxTimeM)
                        {
                            picNumber++;
                        }
                        if (SysParam.Instance.checkBoxTimeH)
                        {
                            picNumber++;
                        }
                        if (SysParam.Instance.checkBoxTimeFre)
                        {
                            picNumber++;
                        }
                        if (SysParam.Instance.checkBoxTimeFilt)
                        {
                            picNumber++;
                        }
                    }
                    if (SysParam.Instance.checkBoxEnergy)
                    {
                        if (SysParam.Instance.checkBoxEnergyAll)
                        {
                            picNumber++;
                        }
                        if (SysParam.Instance.checkBoxEnergyL)
                        {
                            picNumber++;
                        }
                        if (SysParam.Instance.checkBoxEnergyM)
                        {
                            picNumber++;
                        }
                        if (SysParam.Instance.checkBoxEnergyH)
                        {
                            picNumber++;
                        }
                        if (SysParam.Instance.checkBoxEnergySpeed)
                        {
                            picNumber++;
                        }
                    }
                    if (picNumber == 0)
                    {
                        picNumber = 1;
                    }
                    zedGraphControl1.MasterPane.RowNum = picNumber;

                    if (colNumTemp == 0 || colNumTemp > SysParam.Instance.SpeedChannelCount)
                    {

                    }
                    else
                    {
                        zedGraphControl1.MasterPane.ColNum = colNumTemp;
                    }

                    zedGraphControl1.MasterPane.SetLayout(zedGraphControl1.CreateGraphics(), PaneLayout.UserFormat);
                }
                else if (SysParam.Instance.orderByOther)
                {
                    if (SysParam.Instance.orderBySingleColumn)
                    {
                        zedGraphControl1.MasterPane.SetLayout(zedGraphControl1.CreateGraphics(), PaneLayout.SingleColumn);
                    }
                    else if (SysParam.Instance.orderByChannel)
                    {
                        zedGraphControl1.MasterPane.SetLayout(zedGraphControl1.CreateGraphics(), PaneLayout.SquareRowPreferred);
                    }
                }
                zedGraphControl1.Height = DockPanel.Height;
                zedGraphControl1.Invalidate();
            }
        }

        private void OrderByChannel_Time(Stand stand, int standNo)
        {
            if (standNo >= SysParam.Instance.SpeedChannelCount)
            {
                return;
            }
            if (SysParam.Instance.checkBoxTimeAll)
            {
                SetTimeGra_All(stand.tVib, SysParam.Instance.comboBoxTimeAll, (standNo + 1) + "号机架时域波形", standNo);
            }
            if (SysParam.Instance.checkBoxTimeL)
            {
                SetTimeGra_LMH(stand.tVib, stand.band_Fre_L1, stand.band_Fre_L2, SysParam.Instance.comboBoxTimeL, (standNo + 1) + "号机架低频时域", standNo);
            }
            if (SysParam.Instance.checkBoxTimeM)
            {
                SetTimeGra_LMH(stand.tVib, stand.band_Fre_M1, stand.band_Fre_M2, SysParam.Instance.comboBoxTimeM, (standNo + 1) + "号机架中频时域", standNo);
            }
            if (SysParam.Instance.checkBoxTimeH)
            {
                SetTimeGra_LMH(stand.tVib, stand.band_Fre_H1, stand.band_Fre_H2, SysParam.Instance.comboBoxTimeH, (standNo + 1) + "号机架高频时域", standNo);
            }
            if (SysParam.Instance.checkBoxTimeFre)
            {
                SetTimeGra_Fre(stand.tVib, SysParam.Instance.comboBoxTimeFre, (standNo + 1) + "号机架频谱");
            }
            if (SysParam.Instance.checkBoxTimeFilt)
            {
                SetTimeGra_LMH(stand.tVib, Convert.ToInt32(SysParam.Instance.textBoxf1), Convert.ToInt32(SysParam.Instance.textBoxf2), SysParam.Instance.ComboBoxTimeFilt, (standNo + 1) + "号机架带通滤波", standNo);
            }
        }

        private void OrderByChannel_Energy(Stand stand, int standNo)
        {
            if (standNo >= SysParam.Instance.SpeedChannelCount)
            {
                return;
            }
            if (SysParam.Instance.checkBoxEnergyAll)
            {
                energy_All(stand.list_Energy_All.ToArray(), SysParam.Instance.comboBoxEnergyAll, (standNo + 1) + "号机架全局能量");
            }
            if (SysParam.Instance.checkBoxEnergyL)
            {
                energy_LMH(stand.list_Energy_L.ToArray(), stand.list_Alarm_L.ToArray(), stand.list_Defect_L.ToArray(), SysParam.Instance.comboBoxEnergyL, (standNo + 1) + "号机架低频能量");
            }
            if (SysParam.Instance.checkBoxEnergyM)
            {
                energy_LMH(stand.list_Energy_M.ToArray(), stand.list_Alarm_M.ToArray(), stand.list_Defect_M.ToArray(), SysParam.Instance.comboBoxEnergyM, (standNo + 1) + "号机架中频能量");
            }
            if (SysParam.Instance.checkBoxEnergyH)
            {
                energy_LMH(stand.list_Energy_H.ToArray(), stand.list_Alarm_H.ToArray(), stand.list_Defect_H.ToArray(), SysParam.Instance.comboBoxEnergyH, (standNo + 1) + "号机架高频能量");
            }
            if (SysParam.Instance.checkBoxEnergySpeed && (standNo < SysParam.Instance.SpeedChannelCount))
            {
                energy_Speed(stand.list_Energy_Speed.ToArray(), SysParam.Instance.comboBoxEnergySpeed, (standNo + 1) + "号机架速度");
            }
        }

        private void OrderByKind_Time()
        {
            if (SysParam.Instance.checkBoxTimeAll)
            {
                //for (int i = 0; i < SysParam.Instance.channelCount; i++)
                for (int i = SysParam.Instance.SpeedChannelCount - 1; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        SetTimeGra_All(mac.Stand[i].tVib, SysParam.Instance.comboBoxTimeAll, (i + 1) + "号机架时域波形", i);
                    }
                }
            }
            if (SysParam.Instance.checkBoxTimeL)
            {
                //for (int i = 0; i < SysParam.Instance.channelCount; i++)
                for (int i = SysParam.Instance.SpeedChannelCount - 1; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        SetTimeGra_LMH(mac.Stand[i].tVib, mac.Stand[i].band_Fre_L1, mac.Stand[i].band_Fre_L2, SysParam.Instance.comboBoxTimeL, (i + 1) + "号机架低频时域", i);
                    }
                }
            }
            if (SysParam.Instance.checkBoxTimeM)
            {
                //for (int i = 0; i < SysParam.Instance.channelCount; i++)
                for (int i = SysParam.Instance.SpeedChannelCount - 1; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        SetTimeGra_LMH(mac.Stand[i].tVib, mac.Stand[i].band_Fre_M1, mac.Stand[i].band_Fre_M2, SysParam.Instance.comboBoxTimeM, (i + 1) + "号机架中频时域", i);
                    }
                }
            }
            if (SysParam.Instance.checkBoxTimeH)
            {
                //for (int i = 0; i < SysParam.Instance.channelCount; i++)
                for (int i = SysParam.Instance.SpeedChannelCount - 1; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        SetTimeGra_LMH(mac.Stand[i].tVib, mac.Stand[i].band_Fre_H1, mac.Stand[i].band_Fre_H2, SysParam.Instance.comboBoxTimeH, (i + 1) + "号机架高频时域", i);
                    }
                }
            }
            if (SysParam.Instance.checkBoxTimeFre)
            {
                //for (int i = 0; i < SysParam.Instance.channelCount; i++)
                for (int i = SysParam.Instance.SpeedChannelCount - 1; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        SetTimeGra_Fre(mac.Stand[i].tVib, SysParam.Instance.comboBoxTimeFre, (i + 1) + "号机架频谱");
                    }
                }
            }
            if (SysParam.Instance.checkBoxTimeFilt)
            {
                //for (int i = 0; i < SysParam.Instance.channelCount; i++)
                for (int i = SysParam.Instance.SpeedChannelCount - 1; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        SetTimeGra_LMH(mac.Stand[i].tVib, Convert.ToInt32(SysParam.Instance.textBoxf1), Convert.ToInt32(SysParam.Instance.textBoxf2), SysParam.Instance.ComboBoxTimeFilt, (i + 1) + "号机架带通滤波", i);
                    }
                }
            }
        }

        private void OrderByKind_Time2()
        {
            if (SysParam.Instance.checkBoxTimeAll)
            {
                for (int i = 0; i < SysParam.Instance.SpeedChannelCount; i++)
                //for (int i = 4; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        SetTimeGra_All(mac.Stand[i].tVib, SysParam.Instance.comboBoxTimeAll, (i + 1) + "号机架时域波形", i);
                    }
                }
            }
            if (SysParam.Instance.checkBoxTimeL)
            {
                for (int i = 0; i < SysParam.Instance.SpeedChannelCount; i++)
                //for (int i = 4; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        SetTimeGra_LMH(mac.Stand[i].tVib, mac.Stand[i].band_Fre_L1, mac.Stand[i].band_Fre_L2, SysParam.Instance.comboBoxTimeL, (i + 1) + "号机架低频时域", i);
                    }
                }
            }
            if (SysParam.Instance.checkBoxTimeM)
            {
                for (int i = 0; i < SysParam.Instance.SpeedChannelCount; i++)
                //for (int i = 4; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        SetTimeGra_LMH(mac.Stand[i].tVib, mac.Stand[i].band_Fre_M1, mac.Stand[i].band_Fre_M2, SysParam.Instance.comboBoxTimeM, (i + 1) + "号机架中频时域", i);
                    }
                }
            }
            if (SysParam.Instance.checkBoxTimeH)
            {
                for (int i = 0; i < SysParam.Instance.SpeedChannelCount; i++)
                //for (int i = 4; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        SetTimeGra_LMH(mac.Stand[i].tVib, mac.Stand[i].band_Fre_H1, mac.Stand[i].band_Fre_H2, SysParam.Instance.comboBoxTimeH, (i + 1) + "号机架高频时域", i);
                    }
                }
            }
            if (SysParam.Instance.checkBoxTimeFre)
            {
                for (int i = 0; i < SysParam.Instance.SpeedChannelCount; i++)
                //for (int i = 4; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        SetTimeGra_Fre(mac.Stand[i].tVib, SysParam.Instance.comboBoxTimeFre, (i + 1) + "号机架频谱");
                    }
                }
            }
            if (SysParam.Instance.checkBoxTimeFilt)
            {
                for (int i = 0; i < SysParam.Instance.SpeedChannelCount; i++)
                //for (int i = 4; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        SetTimeGra_LMH(mac.Stand[i].tVib, Convert.ToInt32(SysParam.Instance.textBoxf1), Convert.ToInt32(SysParam.Instance.textBoxf2), SysParam.Instance.ComboBoxTimeFilt, (i + 1) + "号机架带通滤波", i);
                    }
                }
            }
        }

        private void OrderByKind_Energy()
        {
            if (SysParam.Instance.checkBoxEnergyAll)
            {
                //for (int i = 0; i < SysParam.Instance.channelCount; i++)
                for (int i = SysParam.Instance.SpeedChannelCount - 1; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        energy_All(mac.Stand[i].list_Energy_All.ToArray(), SysParam.Instance.comboBoxEnergyAll, (i + 1) + "号机架全局能量");
                    }
                }
            }
            if (SysParam.Instance.checkBoxEnergyL)
            {
                //for (int i = 0; i < SysParam.Instance.channelCount; i++)
                for (int i = SysParam.Instance.SpeedChannelCount - 1; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        energy_LMH(mac.Stand[i].list_Energy_L.ToArray(), mac.Stand[i].list_Alarm_L.ToArray(), mac.Stand[i].list_Defect_L.ToArray(), SysParam.Instance.comboBoxEnergyL, (i + 1) + "号机架低频能量");
                    }
                }
            }
            if (SysParam.Instance.checkBoxEnergyM)
            {
                //for (int i = 0; i < SysParam.Instance.channelCount; i++)
                for (int i = SysParam.Instance.SpeedChannelCount - 1; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        energy_LMH(mac.Stand[i].list_Energy_M.ToArray(), mac.Stand[i].list_Alarm_M.ToArray(), mac.Stand[i].list_Defect_M.ToArray(), SysParam.Instance.comboBoxEnergyM, (i + 1) + "号机架中频能量");
                    }
                }
            }
            if (SysParam.Instance.checkBoxEnergyH)
            {
                //for (int i = 0; i < SysParam.Instance.channelCount; i++)
                for (int i = SysParam.Instance.SpeedChannelCount - 1; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        energy_LMH(mac.Stand[i].list_Energy_H.ToArray(), mac.Stand[i].list_Alarm_H.ToArray(), mac.Stand[i].list_Defect_H.ToArray(), SysParam.Instance.comboBoxEnergyH, (i + 1) + "号机架高频能量");
                    }
                }
            }
            if (SysParam.Instance.checkBoxEnergySpeed)
            {
                //for (int i = 0; i < SysParam.Instance.SpeedChannelCount; i++)
                for (int i = SysParam.Instance.SpeedChannelCount - 1; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        energy_Speed(mac.Stand[i].list_Energy_Speed.ToArray(), SysParam.Instance.comboBoxEnergySpeed, (i + 1) + "号机架速度");
                    }
                }
            }
        }

        private void OrderByKind_Energy2()
        {
            if (SysParam.Instance.checkBoxEnergyAll)
            {
                for (int i = 0; i < SysParam.Instance.SpeedChannelCount; i++)
                //for (int i = 4; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        energy_All(mac.Stand[i].list_Energy_All.ToArray(), SysParam.Instance.comboBoxEnergyAll, (i + 1) + "号机架全局能量");
                    }
                }
            }
            if (SysParam.Instance.checkBoxEnergyL)
            {
                for (int i = 0; i < SysParam.Instance.SpeedChannelCount; i++)
                //for (int i = 4; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        energy_LMH(mac.Stand[i].list_Energy_L.ToArray(), mac.Stand[i].list_Alarm_L.ToArray(), mac.Stand[i].list_Defect_L.ToArray(), SysParam.Instance.comboBoxEnergyL, (i + 1) + "号机架低频能量");
                    }
                }
            }
            if (SysParam.Instance.checkBoxEnergyM)
            {
                for (int i = 0; i < SysParam.Instance.SpeedChannelCount; i++)
                //for (int i = 4; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        energy_LMH(mac.Stand[i].list_Energy_M.ToArray(), mac.Stand[i].list_Alarm_M.ToArray(), mac.Stand[i].list_Defect_M.ToArray(), SysParam.Instance.comboBoxEnergyM, (i + 1) + "号机架中频能量");
                    }
                }
            }
            if (SysParam.Instance.checkBoxEnergyH)
            {
                for (int i = 0; i < SysParam.Instance.SpeedChannelCount; i++)
                //for (int i = 4; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        energy_LMH(mac.Stand[i].list_Energy_H.ToArray(), mac.Stand[i].list_Alarm_H.ToArray(), mac.Stand[i].list_Defect_H.ToArray(), SysParam.Instance.comboBoxEnergyH, (i + 1) + "号机架高频能量");
                    }
                }
            }
            if (SysParam.Instance.checkBoxEnergySpeed)
            {
                for (int i = 0; i < SysParam.Instance.SpeedChannelCount; i++)
                //for (int i = 4; i >= 0; i--)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        energy_Speed(mac.Stand[i].list_Energy_Speed.ToArray(), SysParam.Instance.comboBoxEnergySpeed, (i + 1) + "号机架速度");
                    }
                }
            }
        }

        private void SetTimeGra_All(double[] tx, string yAxisVal, string title, int standno)
        {
            gpt = new GraphPane();
            gpt.CurveList.Clear();
            //辊系故障判别线
            if (SysParam.Instance.rollLineVisible && mac.Stand[standno].speed > 5)
            {
                double rotatetimeB, rotatetimeM, rotatetimeW;
                rotatetimeB = (Math.PI * (mac.Stand[standno].rollDiameterBL + mac.Stand[standno].rollDiameterBU) / 2.0) / (mac.Stand[standno].speed / 60 * 1000);//支撑辊的旋转周期
                rotatetimeM = (Math.PI * (mac.Stand[standno].rollDiameterML + mac.Stand[standno].rollDiameterMU) / 2.0) / (mac.Stand[standno].speed / 60 * 1000);//中间辊的旋转周期
                rotatetimeW = (Math.PI * (mac.Stand[standno].rollDiameterWL + mac.Stand[standno].rollDiameterWU) / 2.0) / (mac.Stand[standno].speed / 60 * 1000);//工作辊的旋转周期

                if (rotatetimeB < mac.xAxisTime[mac.xAxisTime.Length - 1])
                {
                    double[] x = { rotatetimeB, rotatetimeB };
                    double[] y = { Convert.ToDouble(yAxisVal), -Convert.ToDouble(yAxisVal) };
                    gpt.AddStick("支撑辊(" + rotatetimeB.ToString("0.000") + "s)", x, y, SysParam.Instance.colorBackupRoll).Line.Width = 2f;
                }
                if (rotatetimeM < mac.xAxisTime[mac.xAxisTime.Length - 1])
                {
                    double[] x = { rotatetimeM, rotatetimeM };
                    double[] y = { Convert.ToDouble(yAxisVal), -Convert.ToDouble(yAxisVal) };
                    gpt.AddStick("中间辊(" + rotatetimeM.ToString("0.000") + "s)", x, y, SysParam.Instance.colorMidRoll).Line.Width = 2f;
                }
                if (rotatetimeW < mac.xAxisTime[mac.xAxisTime.Length - 1])
                {
                    double[] x = { rotatetimeW, rotatetimeW };
                    double[] y = { Convert.ToDouble(yAxisVal), -Convert.ToDouble(yAxisVal) };
                    gpt.AddStick("工作辊(" + rotatetimeW.ToString("0.000") + "s)", x, y, SysParam.Instance.colorWorkRoll).Line.Width = 2f;
                }
            }
            gpt.AddCurve("", mac.xAxisTime, tx, SysParam.Instance.colorTime, SymbolType.None);
            gpt.XAxis.Scale.Min = 0;
            gpt.XAxis.Scale.Max = mac.xAxisTime[mac.xAxisTime.Length - 1];
            gpt.YAxis.Scale.Min = -Convert.ToSingle(yAxisVal);
            gpt.YAxis.Scale.Max = Convert.ToSingle(yAxisVal);
            gpt.Title.Text = title;

            GraPaneSetting(gpt);
            master.Add(gpt);
        }

        private void SetTimeGra_LMH(double[] tx, int fre_1, int fre_2, string yAxisVal, string title, int standno)
        {
            gpt = new GraphPane();
            int length = tx.Length;
            double[] llf = new double[length];
            llf = SysParam.Instance.filterAlgor(tx, fre_1, fre_2, length, 100, Convert.ToInt32(SysParam.Instance.sampleFre));
            gpt.CurveList.Clear();

            //辊系故障判别线
            if (SysParam.Instance.rollLineVisible && mac.Stand[standno].speed > 5)
            {
                double rotatetimeB, rotatetimeM, rotatetimeW;
                rotatetimeB = (Math.PI * (mac.Stand[standno].rollDiameterBL + mac.Stand[standno].rollDiameterBU) / 2.0) / (mac.Stand[standno].speed / 60 * 1000);//支撑辊的旋转周期
                rotatetimeM = (Math.PI * (mac.Stand[standno].rollDiameterML + mac.Stand[standno].rollDiameterMU) / 2.0) / (mac.Stand[standno].speed / 60 * 1000);//中间辊的旋转周期
                rotatetimeW = (Math.PI * (mac.Stand[standno].rollDiameterWL + mac.Stand[standno].rollDiameterWU) / 2.0) / (mac.Stand[standno].speed / 60 * 1000);//工作辊的旋转周期

                if (rotatetimeB < mac.xAxisTime[mac.xAxisTime.Length - 1])
                {
                    double[] x = { rotatetimeB, rotatetimeB };
                    double[] y = { Convert.ToDouble(yAxisVal), -Convert.ToDouble(yAxisVal) };
                    gpt.AddStick("支撑辊(" + rotatetimeB.ToString("0.000") + "s)", x, y, SysParam.Instance.colorBackupRoll).Line.Width = 2f;
                }
                if (rotatetimeM < mac.xAxisTime[mac.xAxisTime.Length - 1])
                {
                    double[] x = { rotatetimeM, rotatetimeM };
                    double[] y = { Convert.ToDouble(yAxisVal), -Convert.ToDouble(yAxisVal) };
                    gpt.AddStick("中间辊(" + rotatetimeM.ToString("0.000") + "s)", x, y, SysParam.Instance.colorMidRoll).Line.Width = 2f;
                }
                if (rotatetimeW < mac.xAxisTime[mac.xAxisTime.Length - 1])
                {
                    double[] x = { rotatetimeW, rotatetimeW };
                    double[] y = { Convert.ToDouble(yAxisVal), -Convert.ToDouble(yAxisVal) };
                    gpt.AddStick("工作辊(" + rotatetimeW.ToString("0.000") + "s)", x, y, SysParam.Instance.colorWorkRoll).Line.Width = 2f;
                }
            }


            gpt.AddCurve("", mac.xAxisTime, llf, SysParam.Instance.colorTime, SymbolType.None);
            gpt.XAxis.Scale.Min = 0;
            gpt.XAxis.Scale.Max = mac.xAxisTime[mac.xAxisTime.Length - 1]; ;
            gpt.YAxis.Scale.Min = -Convert.ToSingle(yAxisVal);
            gpt.YAxis.Scale.Max = Convert.ToSingle(yAxisVal);
            gpt.Title.Text = title;
            GraPaneSetting(gpt);
            master.Add(gpt);
        }

        private void SetTimeGra_Fre(double[] tx, string yAxisVal, string title)
        {
            gpt = new GraphPane();
            gpt.CurveList.Clear();
            PointPairList list_f = new PointPairList();
            list_f.Clear();
            int myLength = tx.Length;
            int length = Convert.ToInt32(Math.Pow(2, SysParam.Instance.N2M(myLength)));
            double[] f = new double[length];
            double[] tt = new double[length];
            for (int i = 0; i < length; i++)
            {
                tt[i] = tx[myLength - length + i];
            }
            SysParam.Instance.FFT(tt, f, length, 1);
            for (int i = 0; i < length / 2; i++)
            {
                f[i] = 2 * Math.Sqrt(tt[i] * tt[i] + f[i] * f[i]) / length;
                list_f.Add(Convert.ToDouble(i) * SysParam.Instance.sampleFre / length, f[i]);
            }
            gpt.AddCurve("", list_f, SysParam.Instance.colorFre, SymbolType.None);
            gpt.XAxis.Scale.Max = SysParam.Instance.sampleFre / 2;
            gpt.XAxis.Scale.Min = 0;
            gpt.YAxis.Scale.Max = Convert.ToSingle(yAxisVal);
            gpt.YAxis.Scale.Min = 0;
            gpt.Title.Text = title;
            GraPaneSetting(gpt);
            master.Add(gpt);
        }

        private void energy_All(double[] energyList, string yAxisVal, string title)
        {
            gpt = new GraphPane();
            gpt.CurveList.Clear();
            if (mac.list_CutTime.Count > 0)
            {
                PointPairList templist = new PointPairList();
                for (int i = 0; i < mac.list_CutTime.Count; i++)
                {
                    templist.Add(mac.list_CutTime[i], Convert.ToDouble(yAxisVal));
                }
                gpt.AddStick("剪切线", templist, SysParam.Instance.colorCut).Line.Width = 2f;
            }
            gpt.AddCurve("", xAxisEnergy, energyList, SysParam.Instance.colorEnergy, SymbolType.None);
            gpt.XAxis.Scale.Min = xAxisEnergy[0];
            gpt.XAxis.Scale.Max = xAxisEnergy[xAxisEnergy.Length - 1];
            gpt.XAxis.Type = AxisType.Date;
            gpt.XAxis.Scale.Format = "HH:mm:ss";
            gpt.YAxis.Scale.Min = 0.0;
            gpt.YAxis.Scale.Max = Convert.ToDouble(yAxisVal);
            gpt.Title.Text = title;
            GraPaneSetting(gpt);
            this.master.Add(gpt);
        }

        private void energy_LMH(double[] lineList, double[] alarmList, double[] defectList, string yAxisVal, string title)
        {
            gpt = new GraphPane();
            gpt.CurveList.Clear();
            if (mac.list_CutTime.Count > 0)
            {
                PointPairList templist = new PointPairList();
                for (int i = 0; i < mac.list_CutTime.Count; i++)
                {
                    templist.Add(mac.list_CutTime[i], Convert.ToDouble(yAxisVal));
                }
                gpt.AddStick("剪切线", templist, SysParam.Instance.colorCut).Line.Width = 2f;
            }
            gpt.AddCurve("缺陷线", xAxisEnergy, defectList, SysParam.Instance.colorDefect, SymbolType.None).Line.Width = 2f;
            gpt.AddCurve("报警线", xAxisEnergy, alarmList, SysParam.Instance.colorAlarm, SymbolType.None).Line.Width = 2f;
            gpt.AddCurve("", xAxisEnergy, lineList, SysParam.Instance.colorEnergy, SymbolType.None);
            gpt.XAxis.Scale.Min = xAxisEnergy[0];
            gpt.XAxis.Scale.Max = xAxisEnergy[xAxisEnergy.Length - 1];
            gpt.XAxis.Type = AxisType.Date;
            gpt.XAxis.Scale.Format = "HH:mm:ss";
            gpt.YAxis.Scale.Min = 0.0;
            gpt.YAxis.Scale.Max = Convert.ToDouble(yAxisVal);
            gpt.Title.Text = title;
            GraPaneSetting(gpt);
            this.master.Add(gpt);
        }

        private void energy_Speed(double[] speedList, string yAxisVal, string title)
        {
            gpt = new GraphPane();
            gpt.CurveList.Clear();
            if (mac.list_CutTime.Count > 0)
            {
                PointPairList templist = new PointPairList();
                for (int i = 0; i < mac.list_CutTime.Count; i++)
                {
                    templist.Add(mac.list_CutTime[i], Convert.ToDouble(yAxisVal));
                }
                gpt.AddStick("剪切线", templist, SysParam.Instance.colorCut).Line.Width = 2f;
            }
            gpt.AddCurve("速度", xAxisEnergy, speedList, SysParam.Instance.colorSpeed, SymbolType.None).Line.Width = 2f;
            gpt.XAxis.Scale.Min = xAxisEnergy[0];
            gpt.XAxis.Scale.Max = xAxisEnergy[xAxisEnergy.Length - 1];
            gpt.XAxis.Type = AxisType.Date;
            gpt.XAxis.Scale.Format = "HH:mm:ss";
            gpt.YAxis.Scale.Min = 0.0;
            gpt.YAxis.Scale.Max = Convert.ToSingle(yAxisVal);
            gpt.Title.Text = title;
            GraPaneSetting(gpt);
            this.master.Add(gpt);
        }

        private static void GraPaneSetting(GraphPane tempGraph)
        {
            float fontsize = SysParam.Instance.fontSize;
            Color myFontColor = SysParam.Instance.colorFont;
            //tempGraph.Margin.All = 2f;
            tempGraph.Fill.IsVisible = false;
            tempGraph.IsFontsScaled = false;
            tempGraph.TitleGap = 0f;
            tempGraph.Margin.All = 3f;
            tempGraph.Border.Color = Color.FromArgb(127, myFontColor);

            tempGraph.Chart.Border.Color = myFontColor;
            tempGraph.Chart.Fill.IsVisible = false;

            tempGraph.Title.FontSpec.FontColor = myFontColor;
            tempGraph.Title.FontSpec.Family = "微软雅黑";
            tempGraph.Title.FontSpec.IsBold = false;
            tempGraph.Title.FontSpec.Size = 1.5f * fontsize;

            tempGraph.Legend.Border.IsVisible = false;
            tempGraph.Legend.Position = LegendPos.InsideTopLeft;
            tempGraph.Legend.IsHStack = false;
            tempGraph.Legend.IsShowLegendSymbols = false;
            tempGraph.Legend.FontSpec.Size = 0.8f * fontsize;
            tempGraph.Legend.Fill.IsVisible = false;
            tempGraph.Legend.FontSpec.FontColor = myFontColor;
            tempGraph.Legend.FontSpec.Family = "微软雅黑";

            tempGraph.XAxis.Title.FontSpec.Family = "微软雅黑";
            tempGraph.XAxis.Title.FontSpec.IsBold = false;
            tempGraph.XAxis.Scale.FontSpec.FontColor = myFontColor;
            tempGraph.XAxis.Scale.FontSpec.Size = fontsize;
            tempGraph.XAxis.Title.IsVisible = false;
            tempGraph.XAxis.MajorTic.IsOutside = false;
            tempGraph.XAxis.MajorTic.Color = Color.FromArgb(127, myFontColor);
            tempGraph.XAxis.MinorTic.IsAllTics = false;

            tempGraph.YAxis.Title.FontSpec.Family = "微软雅黑";
            tempGraph.YAxis.Title.FontSpec.IsBold = false;
            tempGraph.YAxis.Scale.FontSpec.FontColor = myFontColor;
            tempGraph.YAxis.Scale.FontSpec.Size = fontsize;
            tempGraph.YAxis.Title.IsVisible = false;
            tempGraph.YAxis.MajorTic.IsOutside = false;
            tempGraph.YAxis.MajorTic.Color = Color.FromArgb(127, myFontColor);
            tempGraph.YAxis.MinorTic.IsAllTics = false;
            tempGraph.YAxis.MajorGrid.IsZeroLine = false;

            if (SysParam.Instance.BackgroundGridOn)
            {
                tempGraph.XAxis.MajorGrid.IsVisible = true;
                tempGraph.XAxis.MajorGrid.Color = Color.FromArgb(127, myFontColor);
                tempGraph.YAxis.MajorGrid.IsVisible = true;
                tempGraph.YAxis.MajorGrid.Color = Color.FromArgb(127, myFontColor);
            }
            else
            {
                tempGraph.XAxis.MajorGrid.IsVisible = false;
                tempGraph.YAxis.MajorGrid.IsVisible = false;
            }
        }



        private string zedGraphControl1_PointValueEvent(ZedGraphControl sender, GraphPane pane, CurveItem curve, int iPt)
        {
            if (pane.Title.Text.Contains("能量"))
            {
                if (iPt < curve.Points.Count)
                {
                    PointPair pt = curve[iPt];
                    return (DateTime.FromOADate(pt.X).ToString("HH:mm:ss.ff") + "   " + pt.Y.ToString("0.00000") + "g   " + mac.distance_Pause[iPt].ToString("0.0") + "m");
                }
                else
                {
                    return "";
                }
            }
            else if (pane.Title.Text.Contains("速度"))
            {
                if (iPt < curve.Points.Count)
                {
                    PointPair pt = curve[iPt];
                    return (DateTime.FromOADate(pt.X).ToString("HH:mm:ss.ff") + "   " + pt.Y.ToString("0.0") + "m/min   " + mac.distance_Pause[iPt].ToString("0.0") + "m");
                }
                else
                {
                    return "";
                }
            }
            else if (pane.Title.Text.Contains("频谱"))
            {
                if (iPt < curve.Points.Count)
                {
                    PointPair pt = curve[iPt];
                    return (pt.X.ToString("0.0") + "Hz   " + pt.Y.ToString("0.00000") + "g");
                }
                else
                {
                    return "";
                }

            }
            else
            {
                if (iPt < curve.Points.Count)
                {
                    PointPair pt = curve[iPt];
                    return (pt.X.ToString("0.000") + "s   " + pt.Y.ToString("0.00000") + "g");
                }
                else
                {
                    return "";
                }
            }
        }

        private void zedGraphControl1_ContextMenuBuilder(ZedGraphControl sender, ContextMenuStrip menuStrip, Point mousePt, ZedGraphControl.ContextMenuObjectState objState)
        {
            for (int i = 0; i < menuStrip.Items.Count; i++)
            {
                switch (menuStrip.Items[i].Tag.ToString().Trim())
                {
                    case "copy":
                        menuStrip.Items[i].Text = "复制图片";
                        break;
                    case "show_val":
                        menuStrip.Items[i].Text = "查看坐标";
                        break;
                    case "save_as":
                        menuStrip.Items[i].Text = "图片保存至...";
                        break;
                    case "page_setup":
                        menuStrip.Items[i].Text = "页面设置";
                        break;
                    case "print":
                        menuStrip.Items[i].Text = "打印";
                        break;
                    case "unzoom":
                        menuStrip.Items[i].Text = "还原缩放";
                        break;
                    case "undo_all":
                        menuStrip.Items[i].Text = @"还原缩放\移动";
                        break;
                    case "set_default":
                        menuStrip.Items[i].Text = "恢复默认设置";
                        break;
                }
            }
        }

        public void IsShowMenuStrip(bool IsShow)
        {
            zedGraphControl1.IsShowContextMenu = IsShow;
        }
    }
}
