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

using System;
using System.Web;
using System.Data;
using System.Text;
using ASPNet_Drawing;
using System.Drawing;
using System.Web.UI.DataVisualization.Charting;
using MES_SPC;

public class QualityData_XS : IHttpHandler
{

    double[] X;
    double[] S;
    double[] X1;
    double[] SUM;
    double[] Xbar;
    string UCL_X = "";
    string LCL_X = "";
    string CL_X = "";
    string UCL_S = "";
    string LCL_S = "";
    string CL_S = "";
    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 "GetChart":
                responseText = GetChart();
                break;
            //下载图片
            case "OutPutPic":
                responseText = OutPutPic();
                break;



        }
        context.Response.Write(responseText);
    }
    /// <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];
        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);
    }

    /// <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"]);//样本个数
        QualityData.SelectGoodDataReport_DataGroup(EnginIDCheck, EnginID,
            OPNameCheck, OPName,
             EngineTypeID, EngineTypeIDCheck,
             shaftNameCheck, shaftName,
            itemNameCheck, itemName,
             startTime, endTime, n, k, out dt);

        try
        {
            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");
                //计算XS控制图的数据
                CSpc_Data_XS Spc_Data_XS = Spc_Data.XS(X, n);
                UCL_X = Spc_Data_XS.UCL_X.ToString("F2");
                CL_X = Spc_Data_XS.CL_X.ToString("F2");
                LCL_X = Spc_Data_XS.LCL_X.ToString("F2");
                UCL_S = Spc_Data_XS.UCL_S.ToString("F2");
                CL_S = Spc_Data_XS.CL_S.ToString("F2");
                LCL_S = Spc_Data_XS.LCL_S.ToString("F2");
                //均值图Y轴范围
                double gapAxis_X = Spc_Data_XS.UCL_X - Spc_Data_XS.LCL_X;
                //均值的最大值
                double maxXk = Max(Spc_Data_XS.CL_Xk);
                //均值的最小值
                double minXk = Min(Spc_Data_XS.CL_Xk);
                if (maxXk > Spc_Data_XS.UCL_X)
                {
                    YvalueMaxXk = Convert.ToDouble((maxXk + gapAxis_X / 8).ToString("F2"));
                }
                else
                {
                    YvalueMaxXk = Convert.ToDouble((Spc_Data_XS.UCL_X + gapAxis_X / 8).ToString("F2"));
                }
                if (minXk < Spc_Data_XS.LCL_X)
                {
                    YvalueMinXk = Convert.ToDouble((minXk - gapAxis_X / 8).ToString("F2"));
                }
                else
                {
                    YvalueMinXk = Convert.ToDouble((Spc_Data_XS.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_XS.UCL_S - Spc_Data_XS.LCL_S;
                //标准差最大值
                double maxRk = Max(Spc_Data_XS.CL_Sk);
                //标准差最小值
                double minRk = Min(Spc_Data_XS.CL_Sk);
                if (maxRk > Spc_Data_XS.UCL_S)
                {
                    YvalueMaxRk = Convert.ToDouble((maxRk + gapAxis_R / 8).ToString("F2"));
                }
                else
                {
                    YvalueMaxRk = Convert.ToDouble((Spc_Data_XS.UCL_S + gapAxis_R / 8).ToString("F2"));
                }
                if (minRk < Spc_Data_XS.LCL_S)
                {
                    YvalueMinRk = Convert.ToDouble((minRk - gapAxis_R / 8).ToString("F2"));
                }
                else
                {
                    YvalueMinRk = Convert.ToDouble((Spc_Data_XS.LCL_S - gapAxis_R / 8).ToString("F2"));
                }
                YvalueRk += YvalueMinRk + ",";
                for (int i = 1; i < 5; i++)
                {
                    YvalueRk += (i * (double)(YvalueMaxRk - YvalueMinRk) / 5 + YvalueMinRk).ToString() + ",";
                }
                YvalueRk += YvalueMaxRk;
            }
        }
        catch (Exception)
        {

            throw;
        }
       
        return UCL_X + "," + CL_X + "," + LCL_X + "," + UCL_S + "," + CL_S + "," + LCL_S + ","+ usl + ","+ sl +","+ lsl +","+ Cpk +","+ CPL +","+ CPU +"|" + YvalueXk + "|" + YvalueRk+"|"+ YvalueZT + "|"+ XvalueZT +"|"+ USL +","+ SL +","+ LSL;
    }
    /// <summary>
    /// 页面表格数据填充
    /// </summary>
    /// <returns></returns>
    public string GetDataTableByConditent()
    {
        string text = "";//数据
        string text1 = "";//平均值
        string text2 = "";//标准差
        string text3 = "";
        string text4 = "";
        string text5 = "";
        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 sx = "";
        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;
        if (dt != null && dt.Rows.Count > 0)
        {

            double[] str = new double[len];
            for (int i = 0; i < len; i++)
            {
                str[i] = Convert.ToDouble(dt.Rows[i]["测量值"]);
            }
            text4 = "<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];
        S = 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>";
        double ValueUp = Max(X);
        double ValueDown = Min(X);
        //绘图数据
        CSpc_Data_Cpk Spc_Data_Cpk = Spc_Data.Cpk(X, n, ValueUp, ValueDown);
        //计算XS控制图的数据
        CSpc_Data_XS Spc_Data_XS = Spc_Data.XS(X, n);
        usl = Spc_Data_XS.UCL_X.ToString("0.000");
        lsl = Spc_Data_XS.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_XS.LCL_X,  Spc_Data_XS.UCL_X).ToString("0.000");
        pp =ASPNet_Drawing.Table.SPC_Table_Data.CalcPp( n,  X, Spc_Data_XS.LCL_X, Spc_Data_XS.UCL_X).ToString("0.000");
        ppu = ASPNet_Drawing.Table.SPC_Table_Data.CalcPpu(X,n,Spc_Data_XS.UCL_X).ToString("0.000");
        ppl = ASPNet_Drawing.Table.SPC_Table_Data.CalcPpl(X,  n,  Spc_Data_XS.LCL_X).ToString("0.000");
        ppk = ASPNet_Drawing.Table.SPC_Table_Data.CalcPpk(ASPNet_Drawing.Table.SPC_Table_Data.CalcPpu(X,n,Spc_Data_XS.UCL_X),ASPNet_Drawing.Table.SPC_Table_Data.CalcPpl(X,  n,  Spc_Data_XS.LCL_X)).ToString("0.000");
        xx = ASPNet_Drawing.Table.SPC_Table_Data.CalcXbarbar( X, n).ToString("0.000");
        sx = ASPNet_Drawing.Table.SPC_Table_Data.CalcSbar( X, n).ToString("0.000");
        pr = (1 / ASPNet_Drawing.Table.SPC_Table_Data.CalcPp( n,  X, Spc_Data_XS.LCL_X, Spc_Data_XS.UCL_X)).ToString("0.000");
        cr = (1 / ASPNet_Drawing.Table.SPC_Table_Data.CalcCp(n,  X, Spc_Data_XS.LCL_X,  Spc_Data_XS.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; \">S</td>";
        for (int i = 0; i < Spc_Data_XS.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_XS.CL_Xk[i];
            text1 += X1[i].ToString("0.00") + "</td>";
            //S
            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; \">";
            S[i] = Spc_Data_XS.CL_Sk[i];
            text2 += S[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; \" >S的平均值=" + sx + "</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></tr>";

        return text4+text+text5+text1+text2+text3+"</table>";
    }

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

        string result = "";
        string text = "";
        string result1 = "";
        string result3 = "";
        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];
        S = 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_XS Spc_Data_XS = Spc_Data.XS(X, n);
            for (int i = 0; i < Spc_Data_XS.CL_Xk.Length; i++)
            {
                //X
                X1[i] = Spc_Data_XS.CL_Xk[i];
                result += X1[i] + ",";
                //S
                S[i] = Spc_Data_XS.CL_Sk[i];
                result1 += S[i] + ",";


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


        return text;


    }
    /// <summary>
    /// 页面数据图数据标准差图
    /// </summary>
    /// <returns></returns>
    public string GetBaseTrendDataS()
    {

        string result = "";
        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];
        S = 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());
            }
            //计算XS控制图的数据
            ASPNet_Drawing.CSpc_Data_XS Spc_Data_XS = ASPNet_Drawing.Spc_Data.XS(X, n);
            for (int i = 0; i < Spc_Data_XS.CL_Xk.Length; i++)
            {

                //S
                S[i] = Spc_Data_XS.CL_Sk[i];
                result += S[i] + ",";

            }

        }

        return result;


    }
    /// <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 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 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 SaveImg()
    {
        //string path = "c:\\QualityData_XR.png";
        string path = HttpContext.Current.Server.MapPath("~/Chart_excel/QualityData_XS.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_XS.png","QualityData_XS");
        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;
        }
    }

}