<%@ WebHandler Language="C#" Class="QualityData_XR" %>

using System;
using System.Web;
using System.Data;
using System.Text;
using ASPNet_Drawing;
using MES_SPC;

public class QualityData_XR : IHttpHandler
{
    double[] X;
    double[] R;
    double[] X1;
    double[] SUM;
    double[] Xbar;
    string UCL_X = "";
    string LCL_X = "";
    string CL_X = "";
    string UCL_R = "";
    string LCL_R = "";
    string CL_R = "";
    double usl = 0;
    string USL = "";
    double sl = 0;
    string SL = "";
    double lsl = 0;
    string LSL = "";
    string Cpk = "";
    string CPL = "";
    string CPU = "";
    double YvalueMaxRk = 0;
    double YvalueMinRk = 0;
    double YvalueMaxXk = 0;
    double YvalueMinXk = 0;
    string YvalueXk = "";
    string YvalueRk = "";
    string XvalueZT = "";
    string YvalueZT = "";

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string type = HttpContext.Current.Request["type"];
        string responseText = "";
        switch (type)
        {
            case "SaveImg":
                responseText = SaveImg();
                break;
            //绑定机型
            case "BindMachineType":
                responseText = BindMachineType();
                break;
            //绑定测量内容
            case "BindMeasurePartContent":
                responseText = BindMeasurePartContent();
                break;
            //绑定工位名称 
            case "BindOpName":
                responseText = BindOpName();
                break;
            //绑定测量位置
            case "BindMeasurePart":
                responseText = BindMeasurePart();
                break;
            // 页面表格显示
            case "GetDataTableByConditent":
                responseText = GetDataTableByConditent();
                break;
            //页面数据图数据显示均值图&极差图&正态分布图
            case "GetBaseTrendData":
                responseText = GetBaseTrendData();
                break;
            //数据图分析线及X/Y轴显示
            case "GetMarkLineByConditent":
                responseText = GetMarkLineByConditent();
                break;
            //页面数据图显示极差图
            //case "GetBaseTrendDataR":
            //    responseText = GetBaseTrendDataR();
            //    break;
            ////页面显示正态分布图
            //case "GetBaseTrendDataZT":
            //    responseText = GetBaseTrendDataZT();
            //    break;
            //页面数据显示排列图
            case "GetChart":
                responseText = GetChart();
                break;
            //下载图片
            case "OutPutPic":
                responseText = OutPutPic();
                break;
        }
        context.Response.Write(responseText);
    }
    ///// <summary>
    ///// 正态分布图
    ///// </summary>
    ///// <returns></returns>
    //public string GetBaseTrendDataZT()
    //{
    //    DataTable dt = null;
    //    //System.Text.StringBuilder result = new System.Text.StringBuilder();
    //    string result = "";
    //    bool EnginIDCheck = false;
    //    string EnginID = "";
    //    bool OPNameCheck = Convert.ToBoolean(HttpContext.Current.Request["OPNameCheck"]);
    //    string OPName = HttpContext.Current.Request["OPName"];
    //    bool shaftNameCheck = Convert.ToBoolean(HttpContext.Current.Request["shaftNameCheck"]);
    //    string shaftName = HttpContext.Current.Request["shaftName"];
    //    bool itemNameCheck = Convert.ToBoolean(HttpContext.Current.Request["itemNameCheck"]);
    //    string itemName = HttpContext.Current.Request["itemName"];
    //    string startTime = HttpContext.Current.Request["startTime"];
    //    string endTime = null;
    //    string EngineTypeID = HttpContext.Current.Request["EngineTypeID"]; ;
    //    bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
    //    int n = Convert.ToInt32(HttpContext.Current.Request["Yangben"]);
    //    int k = Convert.ToInt32(HttpContext.Current.Request["Num"]);

    //    QualityData.SelectGoodDataReport_DataGroup(EnginIDCheck, EnginID,
    //                              OPNameCheck, OPName,
    //                               EngineTypeID,
    //                                EngineTypeIDCheck,
    //                               shaftNameCheck, shaftName,
    //                              itemNameCheck, itemName,
    //                               startTime, endTime, n, k, out  dt);

    //    if (dt != null && dt.Rows.Count > 2)
    //    {
    //        X = new double[dt.Rows.Count];
    //        for (int i = 0; i < dt.Rows.Count; i++)
    //        {
    //            X[i] = Convert.ToDouble(dt.Rows[i]["测量值"].ToString());
    //        }
    //        double Usl;
    //        double Lsl;

    //        SpcCaculator.GetUslLsl(dt, out Usl, out Lsl);

    //        double x;
    //        double s;
    //        double QU;
    //        double QL;
    //        double cp;
    //        double cpk;
    //        double[] sx;
    //        double[] sy;

    //        double ValueUp = Max(X);
    //        double ValueDown = Min(X);
    //        //计算SPC参数
    //        SpcCaculator.SpcValue(X, ref Usl, ref Lsl, out  x, out  s, out  QU, out  QL, out  cp, out  cpk, out  sx, out  sy);
    //        //坐标轴
    //        double ValueUpX = Convert.ToDouble(Max(sx).ToString("F4"));
    //        double ValueDownX = Convert.ToDouble(Min(sx).ToString("F4"));
    //        double ValueUpY = Convert.ToDouble(Max(sy).ToString("F4"));

    //        ValueDownX = Math.Min(ValueDownX, Lsl);
    //        ValueDownX = Math.Min(ValueDownX, QL);

    //        ValueUpX = Math.Max(ValueUpX, Usl);
    //        ValueUpX = Math.Max(ValueUpX, QU);
    //        double X1 = (ValueUpX - ValueDownX) / 10;
    //        double minValue = Math.Round(ValueDownX - X1, 4);
    //        double maxValue = Math.Round(ValueUpX + X1, 4);
    //        double[] value = new double[dt.Rows.Count];
    //        for (int i = 0; i < sy.Length; i++)
    //        {
    //            result += sy[i].ToString("0.00000000000000");
    //            if (i != sy.Length - 1)
    //            {
    //                result += ",";
    //            }
    //        }
    //        result += "|";
    //        result += Usl + "," + QU + "," + sx[5].ToString("F4") + "," + QL + "," + Lsl + "," + cp + "," + cpk + "|";
    //        for (int i = 0; i < 6; i++)
    //        {
    //            result += i * (double)Math.Round(ValueUpY + ValueUpY / 10, 4) / 5;
    //            if (i != 5)
    //            {
    //                result += ",";
    //            }
    //        }
    //        double XmaxValue = 0;
    //        if (QL > XmaxValue)
    //        {
    //            XmaxValue = QL;
    //        }
    //        if (Usl > XmaxValue)
    //        {
    //            XmaxValue = Usl;
    //        }
    //        if (QU > XmaxValue)
    //        {
    //            XmaxValue = QU;
    //        }
    //        if (Lsl > XmaxValue)
    //        {
    //            XmaxValue = Lsl;
    //        }
    //        if (Convert.ToDouble(sx[5].ToString("F4")) > XmaxValue)
    //        {
    //            XmaxValue = Convert.ToDouble(sx[5].ToString("F4"));
    //        }
    //        result += "|";
    //        for (int i = 0; i <= 12; i++)
    //        {
    //            result += i * (double)XmaxValue / 11;
    //            if (i != 12)
    //            {
    //                result += ",";
    //            }
    //        }
    //        //添加显示对应标线在X轴的实际位置
    //        result += "|";
    //        result += (double)Usl * 11 / XmaxValue + "," + (double)QU * 11 / XmaxValue + "," + (double)(Convert.ToDouble(sx[5].ToString("F4"))) * 11 / XmaxValue + "," + (double)QL * 11 / XmaxValue + "," + (double)Lsl * 11 / XmaxValue;
    //    }
    //    return result;
    //}

    /// <summary>
    /// 排列直方图
    /// </summary>
    /// <returns></returns>
    public string GetChart()
    {
        DataTable dt = null;
        bool EnginIDCheck = false;
        string EnginID = "";
        string startTime = HttpContext.Current.Request["startTime"];//开始时间
        string endTime = HttpContext.Current.Request["endTime"];//结束时间
        bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
        string EngineTypeID = HttpContext.Current.Request["EngineTypeID"];//机型
        bool OPNameCheck = Convert.ToBoolean(HttpContext.Current.Request["OPNameCheck"]);
        string OPName = HttpContext.Current.Request["OPName"];//工位号
        bool shaftNameCheck = Convert.ToBoolean(HttpContext.Current.Request["shaftNameCheck"]);
        string shaftName = HttpContext.Current.Request["shaftName"];//测量位置
        bool itemNameCheck = Convert.ToBoolean(HttpContext.Current.Request["itemNameCheck"]);
        string itemName = HttpContext.Current.Request["itemName"];//测量项目
        int n = Convert.ToInt32(HttpContext.Current.Request["Yangben"]);//样本容量
        int k = Convert.ToInt32(HttpContext.Current.Request["Num"]);//样本个数
        QualityData.SelectGoodDataReport_DataGroup(EnginIDCheck, EnginID,
            OPNameCheck, OPName,
             EngineTypeID, EngineTypeIDCheck,
             shaftNameCheck, shaftName,
            itemNameCheck, itemName,
             startTime, endTime, n, k, out dt);

        int len = dt.Rows.Count;
        double[] str = new double[len];
        try
        {
            for (int i = 0; i < len; i++)
            {
                str[i] = Convert.ToDouble(dt.Rows[i]["测量值"]);
            }

            double minV = Min(str);
            double maxV = Max(str);
            double Dv = (maxV - minV) / n;
            Dv = RoundInterval(Dv);
            minV = Math.Floor(minV / Dv) * Dv;
            maxV = Math.Ceiling(maxV / Dv) * Dv;

            string textY1 = "";
            string textY2 = "";
            double acv = 0.0;
            object average;
            double bottom = minV;
            StringBuilder textadd = new StringBuilder();

            for (bottom = minV; bottom < maxV; bottom += Dv)
            {
                int number = 0;
                double top = bottom + Dv;
                foreach (double p in str)
                {
                    if (top >= maxV)
                    {
                        if (p >= bottom && p <= top)
                            ++number;
                    }
                    else
                    {
                        if (p >= bottom && p < top)
                            ++number;
                    }
                }
                acv += Convert.ToDouble(number);
                average = ((acv / (n * k)) * 100).ToString("0.00");
                textY1 += number;
                textY2 += average;
                if (top < maxV)
                {
                    textY1 += ",";
                    textY2 += ",";
                }
            }
            textadd.Append(textY1);
            textadd.Append("|");
            textadd.Append(textY2);
            return Convert.ToString(textadd);
        }
        catch (Exception)
        {

             return "";
        }
       
       
    }


    /// <summary>
    /// 观测值最大值
    /// </summary>
    /// <param name="Xn">子组观测值</param>
    /// <returns></returns>
    static private double Max(double[] Xn)
    {
        double temp;
        temp = Xn[0];
        for (int i = 0; i < Xn.Length; i++)
        {
            temp = (Xn[i] > temp) ? Xn[i] : temp;
        }
        return temp;
    }

    /// <summary>
    /// 观测值最小值
    /// </summary>
    /// <param name="Xn">子组观测值</param>
    /// <returns></returns>
    static private double Min(double[] Xn)
    {
        double temp;
        temp = Xn[0];
        for (int i = 0; i < Xn.Length; i++)
        {
            temp = (Xn[i] < temp) ? Xn[i] : temp;
        }
        return temp;
    }
    /// <summary>
    /// Helper method which rounds specified axsi interval.
    /// </summary>
    /// <param name="interval">Calculated axis interval.</param>
    /// <returns>Rounded axis interval.</returns>
    public double RoundInterval(double interval)
    {
        // If the interval is zero return error
        if (interval == 0.0)
        {
            throw (new ArgumentOutOfRangeException("interval", "Interval can not be zero."));
        }

        // If the real interval is > 1.0
        double step = -1;
        double tempValue = interval;
        while (tempValue > 1.0)
        {
            step++;
            tempValue = tempValue / 10.0;
            if (step > 1000)
            {
                throw (new InvalidOperationException("Auto interval error due to invalid point values or axis minimum/maximum."));
            }
        }

        // If the real interval is < 1.0
        tempValue = interval;
        if (tempValue < 1.0)
        {
            step = 0;
        }

        while (tempValue < 1.0)
        {
            step--;
            tempValue = tempValue * 10.0;
            if (step < -1000)
            {
                throw (new InvalidOperationException("Auto interval error due to invalid point values or axis minimum/maximum."));
            }
        }

        double tempDiff = interval / Math.Pow(10.0, step);
        if (tempDiff < 3.0)
        {
            tempDiff = 2.0;
        }
        else if (tempDiff < 7.0)
        {
            tempDiff = 5.0;
        }
        else
        {
            tempDiff = 10.0;
        }

        // Make a correction of the real interval
        return tempDiff * Math.Pow(10.0, step);
    }





    ///<summary>
    ///数据图上分析线&X/Y轴
    ///</summary>
    ///<returns></returns>
    public string GetMarkLineByConditent()
    {
        {

            DataTable dt = null;
            bool EnginIDCheck = false;
            string EnginID = "";
            string startTime = HttpContext.Current.Request["startTime"];//开始时间
            string endTime = HttpContext.Current.Request["endTime"];//结束时间
            bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
            string EngineTypeID = HttpContext.Current.Request["EngineTypeID"];//机型
            bool OPNameCheck = Convert.ToBoolean(HttpContext.Current.Request["OPNameCheck"]);
            string OPName = HttpContext.Current.Request["OPName"];//工位号
            bool shaftNameCheck = Convert.ToBoolean(HttpContext.Current.Request["shaftNameCheck"]);
            string shaftName = HttpContext.Current.Request["shaftName"];//测量位置
            bool itemNameCheck = Convert.ToBoolean(HttpContext.Current.Request["itemNameCheck"]);
            string itemName = HttpContext.Current.Request["itemName"];//测量项目
            int n = Convert.ToInt32(HttpContext.Current.Request["Yangben"]);//样本容量
            int k = Convert.ToInt32(HttpContext.Current.Request["Num"]);//样本个数
            MES_SPC.QualityData.SelectGoodDataReport_DataGroup(EnginIDCheck, EnginID,
                OPNameCheck, OPName,
                 EngineTypeID, EngineTypeIDCheck,
                 shaftNameCheck, shaftName,
                itemNameCheck, itemName,
                 startTime, endTime, n, k, out dt);


            if (dt.Rows.Count == n * k)
            {
                X = new double[n * k];
                for (int i = 0; i < n * k; i++)
                {
                    X[i] = Convert.ToDouble(dt.Rows[i]["测量值"].ToString());
                }
                //绘图数据
                double ValueUp = Max(X);
                double ValueDown = Min(X);
                CSpc_Data_Cpk Spc_Data_Cpk = Spc_Data.Cpk(X, n, ValueUp, ValueDown);
                //坐标轴
                double ValueUpX = Convert.ToDouble(Max(Spc_Data_Cpk.NormalDistributionX).ToString("F2"));
                double ValueDownX = Convert.ToDouble(Min(Spc_Data_Cpk.NormalDistributionX).ToString("F2"));
                double ValueUpY = Convert.ToDouble(Max(Spc_Data_Cpk.NormalDistributionY).ToString("F2"));
                double X1 = (ValueUpX - ValueDownX) / 10;
                double AxisXMinimum = Math.Round(ValueDownX - X1, 2);
                double AxisXMaximum = Math.Round(ValueUpX + X1, 2);
                double AxisYMinimum = 0;
                double AxisYMaximum = Math.Round(ValueUpY + ValueUpY / 10, 2);
                //int len =Convert.ToInt32(Spc_Data_Cpk.NormalDistributionX); 
                double[] XValue = new double[Spc_Data_Cpk.NormalDistributionX.Length];
                double[] YValue = new double[Spc_Data_Cpk.NormalDistributionX.Length];
                XvalueZT = AxisXMinimum + ",";
                for (int i = 0; i < Spc_Data_Cpk.NormalDistributionX.Length; i++)
                {
                    //正态分布图X轴坐标
                    XValue[i] = Math.Round(Spc_Data_Cpk.NormalDistributionX[i], 2);
                    XvalueZT += XValue[i] + ",";
                }
                XvalueZT += AxisXMaximum;
                //YvalueZT = AxisYMinimum + ",";
                for (int i = 0; i < Spc_Data_Cpk.NormalDistributionX.Length; i++)
                {
                    //正态分布图Y轴坐标
                    YValue[i] = Math.Round(Spc_Data_Cpk.NormalDistributionY[i], 2);
                    YvalueZT += YValue[i] + ",";
                }
                YvalueZT += AxisYMaximum;
                //计算XR控制图的数据
                double T = (AxisXMaximum - AxisXMinimum) * 2;
                CSpc_Data_XR Spc_Data_XR = Spc_Data.XR(X, n);
                sl = Spc_Data_XR.CL_X;
                SL = (sl * 10 / T).ToString("0.00");
                usl = Spc_Data_XR.UCL_X;
                USL = (usl * 10 / T).ToString("0.00");
                lsl = Spc_Data_XR.LCL_X;
                LSL = (lsl * 10 / T).ToString("0.00");
                Cpk = Spc_Data_Cpk.Cpk.ToString("F2");
                CPL = Spc_Data_Cpk.CPL.ToString("F2");
                CPU = Spc_Data_Cpk.CPU.ToString("F2");
                UCL_X = Spc_Data_XR.UCL_X.ToString("F2");
                CL_X = Spc_Data_XR.CL_X.ToString("F2");
                LCL_X = Spc_Data_XR.LCL_X.ToString("F2");
                UCL_R = Spc_Data_XR.UCL_R.ToString("F2");
                CL_R = Spc_Data_XR.CL_R.ToString("F2");
                LCL_R = Spc_Data_XR.LCL_R.ToString("F2");
                //均值图Y轴范围
                double gapAxis_X = Spc_Data_XR.UCL_X - Spc_Data_XR.LCL_X;
                //均值的最大值
                double maxXk = Max(Spc_Data_XR.CL_Xk);
                //均值的最小值
                double minXk = Min(Spc_Data_XR.CL_Xk);
                if (maxXk > Spc_Data_XR.UCL_X)
                {
                    YvalueMaxXk = Convert.ToDouble((maxXk + gapAxis_X / 8).ToString("F2"));
                }
                else
                {
                    YvalueMaxXk = Convert.ToDouble((Spc_Data_XR.UCL_X + gapAxis_X / 8).ToString("F2"));
                }
                if (minXk < Spc_Data_XR.LCL_X)
                {
                    YvalueMinXk = Convert.ToDouble((minXk - gapAxis_X / 8).ToString("F2"));
                }
                else
                {
                    YvalueMinXk = Convert.ToDouble((Spc_Data_XR.LCL_X - gapAxis_X / 8).ToString("F2"));
                }
                YvalueXk += YvalueMinXk + ",";
                for (int i = 1; i < 5; i++)
                {
                    YvalueXk += (i * (double)(YvalueMaxXk - YvalueMinXk) / 5 + YvalueMinXk).ToString() + ",";
                }
                YvalueXk += YvalueMaxXk;

                //极差图Y轴范围
                double gapAxis_R = Spc_Data_XR.UCL_R - Spc_Data_XR.LCL_R;
                //极差最大值
                double maxRk = Max(Spc_Data_XR.CL_Rk);
                //极差最小值
                double minRk = Min(Spc_Data_XR.CL_Rk);
                if (maxRk > Spc_Data_XR.UCL_R)
                {
                    YvalueMaxRk = Convert.ToDouble((maxRk + gapAxis_R / 8).ToString("F2"));
                }
                else
                {
                    YvalueMaxRk = Convert.ToDouble((Spc_Data_XR.UCL_R + gapAxis_R / 8).ToString("F2"));
                }
                if (minRk < Spc_Data_XR.LCL_R)
                {
                    YvalueMinRk = Convert.ToDouble((minRk - gapAxis_R / 8).ToString("F2"));
                }
                else
                {
                    YvalueMinRk = Convert.ToDouble((Spc_Data_XR.LCL_R - gapAxis_R / 8).ToString("F2"));
                }
                YvalueRk += YvalueMinRk + ",";
                for (int i = 1; i < 5; i++)
                {
                    YvalueRk += (i * (double)(YvalueMaxRk - YvalueMinRk) / 5 + YvalueMinRk).ToString() + ",";
                }
                YvalueRk += YvalueMaxRk;

            }


        }
        return UCL_X + "," + CL_X + "," + LCL_X + "," + UCL_R + "," + CL_R + "," + LCL_R + "," + usl + "," + sl + "," + lsl + "," + Cpk + "," + CPL + "," + CPU + "|" + YvalueXk + "|" + YvalueRk + "|" + YvalueZT + "|" + XvalueZT + "|" + USL + "," + SL + "," + LSL;
    }




    ///<summary>
    ///GetReport
    ///</summary>
    ///<returns></returns>
    private DataTable GetReport(bool OPNameCheck, string OPName, bool shaftNameCheck, string shaftName, bool itemNameCheck, string itemName, string startTime, string endTime)
    {
        DataTable dt;
        bool EnginIDCheck;
        string EnginID;
        EnginIDCheck = false;
        EnginID = "";
        QualityData.SelectGoodDataReport_TrendPictureBasic(EnginIDCheck, EnginID,
        OPNameCheck, OPName,
         shaftNameCheck, shaftName,
        itemNameCheck, itemName,
         startTime, endTime, out dt);
        return dt;
    }



    ///<summary>
    ///页面数据图数据显示均值图&极差图
    ///</summary>
    ///<returns></returns>
    public string GetBaseTrendData()
    {

        string result = "";
        string result1 = "";
        string result3 = "";
        string text = "";
        DataTable dt = null;
        bool EnginIDCheck = false;
        string EnginID = "";
        string startTime = HttpContext.Current.Request["startTime"];//开始时间
        string endTime = HttpContext.Current.Request["endTime"];//结束时间
        bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
        string EngineTypeID = HttpContext.Current.Request["EngineTypeID"];//机型
        bool OPNameCheck = Convert.ToBoolean(HttpContext.Current.Request["OPNameCheck"]);
        string OPName = HttpContext.Current.Request["OPName"];//工位号
        bool shaftNameCheck = Convert.ToBoolean(HttpContext.Current.Request["shaftNameCheck"]);
        string shaftName = HttpContext.Current.Request["shaftName"];//测量位置
        bool itemNameCheck = Convert.ToBoolean(HttpContext.Current.Request["itemNameCheck"]);
        string itemName = HttpContext.Current.Request["itemName"];//测量项目
        int n = Convert.ToInt32(HttpContext.Current.Request["Yangben"]);//样本容量
        int k = Convert.ToInt32(HttpContext.Current.Request["Num"]);//样本个数
        QualityData.SelectGoodDataReport_DataGroup(EnginIDCheck, EnginID,
            OPNameCheck, OPName,
             EngineTypeID, EngineTypeIDCheck,
             shaftNameCheck, shaftName,
            itemNameCheck, itemName,
             startTime, endTime, n, k, out dt);

        X1 = new double[n * k];
        R = new double[n * k];
        if (dt.Rows.Count == n * k)
        {
            X = new double[n * k];
            for (int i = 0; i < n * k; i++)
            {
                X[i] = Convert.ToDouble(dt.Rows[i]["测量值"].ToString());
            }
            double ValueUp = Max(X);
            double ValueDown = Min(X);
            //绘图数据--正态分布图
            CSpc_Data_Cpk Spc_Data_Cpk = Spc_Data.Cpk(X, n, ValueUp, ValueDown);
            double[] Y = new double[Spc_Data_Cpk.NormalDistributionX.Length];
            for (int i = 0; i < Spc_Data_Cpk.NormalDistributionX.Length; i++)
            {
                Y[i] = Math.Round(Spc_Data_Cpk.NormalDistributionY[i], 2);
                result3 += Y[i] + ",";
            }
            //计算XR控制图的数据
            CSpc_Data_XR Spc_Data_XR = Spc_Data.XR(X, n);
            for (int i = 0; i < Spc_Data_XR.CL_Xk.Length; i++)
            {
                //X
                X1[i] = Spc_Data_XR.CL_Xk[i];
                result += X1[i] + ",";
                //R
                R[i] = Spc_Data_XR.CL_Rk[i];
                result1 += R[i] + ",";

            }
            text = result + "|" + result1 + "|" + result3;













        }


        return text;

    }



    ///<summary>
    ///页面表格显示
    ///</summary>
    ///<returns></returns>
    public string GetDataTableByConditent()
    {
        string text = "";//数据
        string text5 = "";//和
        string text1 = "";//平均值
        string text2 = "";//极差
        string text3 = "";//USL CPK数据
        string text4 = "";//组号
        string usl = "";
        string lsl = "";
        string cpl = "";
        string cpk = "";
        string cpu = "";
        string pp = "";//计算无偏过程性能指数Pp
        string ppl = "";
        string ppu = "";
        string ppk = "";
        string cr = "";
        string pr = "";
        string cp = "";
        string xx = "";
        string rx = "";
        int m = 0;

        DataTable dt = null;
        bool EnginIDCheck = false;
        string EnginID = "";
        string startTime = HttpContext.Current.Request["startTime"];//开始时间
        string endTime = HttpContext.Current.Request["endTime"];//结束时间
        bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
        string EngineTypeID = HttpContext.Current.Request["EngineTypeID"];//机型
        bool OPNameCheck = Convert.ToBoolean(HttpContext.Current.Request["OPNameCheck"]);
        string OPName = HttpContext.Current.Request["OPName"];//工位号
        bool shaftNameCheck = Convert.ToBoolean(HttpContext.Current.Request["shaftNameCheck"]);
        string shaftName = HttpContext.Current.Request["shaftName"];//测量位置
        bool itemNameCheck = Convert.ToBoolean(HttpContext.Current.Request["itemNameCheck"]);
        string itemName = HttpContext.Current.Request["itemName"];//测量项目
        int n = Convert.ToInt32(HttpContext.Current.Request["Yangben"]);//样本容量
        int k = Convert.ToInt32(HttpContext.Current.Request["Num"]);//样本个数
        QualityData.SelectGoodDataReport_DataGroup(EnginIDCheck, EnginID,
            OPNameCheck, OPName,
             EngineTypeID, EngineTypeIDCheck,
             shaftNameCheck, shaftName,
            itemNameCheck, itemName,
             startTime, endTime, n, k, out dt);
        int len = dt.Rows.Count;
        SUM = new double[k];
        if (dt != null && dt.Rows.Count > 0)
        {
            len = dt.Rows.Count;
            double[] str = new double[len];
            for (int i = 0; i < len; i++)
            {
                str[i] = Convert.ToDouble(dt.Rows[i]["测量值"]);
            }
            text4 = "<table table  style=\"width:1520px;table-layout:fixed;\"><tr><td style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;border: solid #ff0000;text-align:center;border-width:1px 1px 1px 1px; padding:10px 0px; \">组号</td>";
            for (int i = 1; i <= k; i++)
            {
                text4 += "<td style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;border: solid #ff0000;text-align:center;border-width:1px 1px 1px 0px; padding:10px 0px;\">" + i + "</td>";
            }
            text = "<tr>";
            for (int i = 0, t = 1; i < n; i++, t++)
            {
                text += "<td style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;border: solid #ff0000;text-align:center;border-width:0px 1px 1px 1px; padding:10px 0px;\">" + t + "</td>";

                for (m = 0; m < k; m++)
                {

                    text += "<td style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;border: solid #ff0000;text-align:center;border-width:0px 1px 1px 0px; padding:10px 0px;\">";
                    text += str[m * n + i] + "</td>";
                }
                text += "</tr>";
            }
        }
        X1 = new double[len];
        R = new double[len];
        X = new double[len];

        for (int i = 0; i < len; i++)
        {
            X[i] = Convert.ToDouble(dt.Rows[i]["测量值"].ToString());
        }
        Xbar = new double[X.Length / n];
        SUM = new double[X.Length / n];
        float a = 0;
        float b = 0;
        for (int j = 0; j < X.Length / n; j++)
        {
            SUM[j] = 0.0;
            Xbar[j] = 0.0;
            for (int i = 0; i < n; i++)
            {
                if (i == 0)
                {
                    a = (float)X[j * n + i];
                    b = (float)X[j * n + i];
                }
                if (i != 0)
                {
                    if (a > (float)X[j * n + i])
                    {
                        a = a;
                    }
                    else
                    {
                        a = (float)X[j * n + i];
                    }
                    if (b < (float)X[j * n + i])
                    {
                        b = b;
                    }
                    else
                    {
                        b = (float)X[j * n + i];
                    }
                }
                SUM[j] = X[j * n + i] + SUM[j];
                Xbar[j] = SUM[j] / n;

            }

        }
        text5 = "<tr><td style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;border: solid #ff0000;text-align:center;border-width:0px 1px 1px 1px; padding:10px 0px;\">求和</td>";
        for (int i = 0; i < X.Length / n; i++)
        {
            text5 += "<td style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;border: solid #ff0000;text-align:center;border-width:0px 1px 1px 0px; padding:10px 0px;\">" + SUM[i] + "</td>";
        }
        text5 += "</tr>";


        //计算XR控制图的数据
        double ValueUp = Max(X);
        double ValueDown = Min(X);
        //绘图数据
        CSpc_Data_Cpk Spc_Data_Cpk = Spc_Data.Cpk(X, n, ValueUp, ValueDown);
        CSpc_Data_XR Spc_Data_XR = Spc_Data.XR(X, n);
        usl = Spc_Data_XR.UCL_X.ToString("0.000");
        lsl = Spc_Data_XR.LCL_X.ToString("0.000");
        cpk = Spc_Data_Cpk.Cpk.ToString("0.000");
        cpl = Spc_Data_Cpk.CPL.ToString("0.000");
        cpu = Spc_Data_Cpk.CPU.ToString("0.000");
        cp = ASPNet_Drawing.Table.SPC_Table_Data.CalcCp(n, X, Spc_Data_XR.LCL_X, Spc_Data_XR.UCL_X).ToString("0.000");
        pp = ASPNet_Drawing.Table.SPC_Table_Data.CalcPp(n, X, Spc_Data_XR.LCL_X, Spc_Data_XR.UCL_X).ToString("0.000");
        ppu = ASPNet_Drawing.Table.SPC_Table_Data.CalcPpu(X, n, Spc_Data_XR.UCL_X).ToString("0.000");
        ppl = ASPNet_Drawing.Table.SPC_Table_Data.CalcPpl(X, n, Spc_Data_XR.LCL_X).ToString("0.000");
        ppk = ASPNet_Drawing.Table.SPC_Table_Data.CalcPpk(ASPNet_Drawing.Table.SPC_Table_Data.CalcPpu(X, n, Spc_Data_XR.UCL_X), ASPNet_Drawing.Table.SPC_Table_Data.CalcPpl(X, n, Spc_Data_XR.LCL_X)).ToString("0.000");
        xx = ASPNet_Drawing.Table.SPC_Table_Data.CalcXbarbar(X, n).ToString("0.000");
        rx = ASPNet_Drawing.Table.SPC_Table_Data.CalcRbar(X, n).ToString("0.000");
        pr = (1 / ASPNet_Drawing.Table.SPC_Table_Data.CalcPp(n, X, Spc_Data_XR.LCL_X, Spc_Data_XR.UCL_X)).ToString("0.000");
        cr = (1 / ASPNet_Drawing.Table.SPC_Table_Data.CalcCp(n, X, Spc_Data_XR.LCL_X, Spc_Data_XR.UCL_X)).ToString("0.000");
        text1 = "<tr><td style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;border: solid #ff0000;text-align:center;border-width:0px 1px 1px 1px; padding:10px 0px;\">X</td>";
        text2 = "<tr><td style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;border: solid #ff0000;text-align:center;border-width:0px 1px 1px 1px; padding:10px 0px;\">R</td>";
        for (int i = 0; i < Spc_Data_XR.CL_Xk.Length; i++)
        {
            //X
            text1 += "<td style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;border: solid #ff0000;text-align:center;border-width:0px 1px 1px 0px; padding:10px 0px;\">";
            X1[i] = Spc_Data_XR.CL_Xk[i];
            text1 += X1[i].ToString("0.00") + "</td>";
            //R
            text2 += "<td style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;border: solid #ff0000;text-align:center;border-width:0px 1px 1px 0px; padding:10px 0px;\">";
            R[i] = Spc_Data_XR.CL_Rk[i];
            text2 += R[i].ToString("0.00") + "</td>";
        }
        text1 += "</tr>";
        text2 += "</tr>";
        text3 = "<tr>"
             // + "<td colspan=\"6\"  style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis; ;text-align:center;padding:10px 0px; \">USL=" + usl + "</td>"
             // + "<td  colspan=\"6\"style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;;text-align:center; padding:10px 0px; \">LSL=" + lsl + "</td><td colspan=\"3\">异常情况分析：</td></tr>"
             // + "<tr><td colspan=\"6\"style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis; ;text-align:center;padding:10px 0px; \">X平均值=" + xx + "</td>"
             // + "<td  colspan=\"6\"style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;text-align:center; padding:10px 0px; \" >R的平均值=" + rx + "</td><td colspan=\"3\">(1)出现超出控制限的点</td></tr>"
             //+ "<tr><td  colspan=\"3\"style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;text-align:center; padding:10px 0px; \" >CPL=" + cpl + "</td>"
             //+ "<td  colspan=\"3\"style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;text-align:center; padding:10px 0px; \" >Cp=" + cp + "</td>"
             //+ "<td  colspan=\"3\"style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;text-align:center; padding:10px 0px; \" >CPK=" + cpk + "</td>"
             //+ "<td  colspan=\"3\"style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;text-align:center; padding:10px 0px; \" >CPU=" + cpu + "</td><td colspan=\"3\">(2)连续七个点全在控制限之上或之下</td></tr>"
             //+ "<tr><td  colspan=\"3\"style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;text-align:center; padding:10px 0px; \"  >Pp=" + pp + "</td>"
             //+ "<td  colspan=\"3\"style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;text-align:center; padding:10px 0px; \"  >Ppu=" + ppu + "</td>"
             //+ "<td  colspan=\"3\"style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;text-align:center; padding:10px 0px; \" >Ppl=" + ppl + "</td>"
             //+ "<td  colspan=\"3\"style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;text-align:center; padding:10px 0px; \"  >Ppk=" + ppk + "</td><td colspan=\"3\">(3)任何其他明显非随机的图形</td></tr>"
             //+ "<tr><td  colspan=\"3\"style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;text-align:center; padding:10px 0px; \"  >Cr=" + cr + "</td>"
             //+ "<td  colspan=\"3\"style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis;text-align:center; padding:10px 0px; \"  >Pr=" + pr + "</td>"

             + "<td colspan=\"5\" style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis; ;text-align:center;padding:0px 0px; border:1px solid red;\">X平均值=" + xx + "</td>"
              + "<td colspan=\"5\" style=\"white-space:nowrap;overflow:hidden;word-break:keep-all;text-overflow:ellipsis; ;text-align:center;padding:0px 0px; border:1px solid red;\">Cp=" + cp + "</td>"
             +"</tr>";
        return text4 + text + text5 + text1 + text2 + text3 + "</table>";

    }

    /// <summary>
    /// 绑定机型
    /// </summary>
    /// <returns></returns>
    public string BindMachineType()
    {
        string text = "";
        DataTable dt;
        MES_SPC.CraftMana_step.enginetypeSelect(out dt);
        if (dt != null && dt.Rows.Count > 0)
        {
            text = JsonHelper.DataTableToJson(dt);
        }
        return text;
    }
    /// <summary>
    /// 绑定测量内容
    /// </summary>
    /// <returns></returns>
    public string BindMeasurePartContent()
    {
        string text = "";
        string opName;
        string shaftName;
        opName = HttpContext.Current.Request["opName"];
        shaftName = HttpContext.Current.Request["shaftName"];
        DataTable dt;
        QualityData.GetItemName(opName, shaftName, out dt);
        if (dt != null && dt.Rows.Count > 0)
        {
            text = JsonHelper.DataTableToJson(dt);
        }
        return text;
    }

    /// <summary>
    /// 绑定工位名称
    /// </summary>
    /// <returns></returns>
    public string BindOpName()
    {
        string text = "";
        System.Data.DataTable dt;
        MES_SPC.QualityData.GetOpName(out dt);
        if (dt != null && dt.Rows.Count > 0)
        {
            text = JsonHelper.DataTableToJson(dt);
        }
        return text;
    }
    /// <summary>
    /// 绑定测量名称
    /// </summary>
    /// <returns></returns>
    public string BindMeasurePart()
    {
        string text = "";
        DataTable dt;
        string opName;
        opName = HttpContext.Current.Request["opName"];
        QualityData.GetShaftName(opName, out dt);
        if (dt != null && dt.Rows.Count > 0)
        {
            text = JsonHelper.DataTableToJson(dt);
        }
        return text;
    }

    /// <summary>
    /// 保存为图片
    /// </summary>
    /// <returns></returns>




    public string SaveImg()
    {
        //string path = "c:\\QualityData_XR.png";
        string path = HttpContext.Current.Server.MapPath("~/Chart_excel/QualityData_XR.png");
        string byteStr = HttpContext.Current.Request["ImageDate"].Replace("data:image/png;base64,", "");
        System.Drawing.Image returnImage = Base64StringToImage(byteStr);
        if (System.IO.File.Exists(path))
        {
            System.IO.File.Delete(path);
        }
        returnImage.Save(path);
        return "{\"result\":\"1\"}";

    }
    public string OutPutPic()
    {

        CommonFunction.ExcelTable_QualityData_tupian("QualityData_XR.png", "QualityData_XR");
        return "";
    }
    private System.Drawing.Image Base64StringToImage(string txt)
    {
        byte[] arr = Convert.FromBase64String(txt);
        System.IO.MemoryStream ms = new System.IO.MemoryStream(arr);
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
        return bmp;
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}