using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Data.SqlClient;


/// <summary>
///DrawChart 的摘要说明
/// </summary>
public partial class DrawChart
{

    public static void ChartQualityData_Pareto(DataTable dt,int n, Chart chart1)
    {
        chart1.Series["RawData"].Points.DataBindY(dt.Rows, "测量值");

        DrawPareto drawPareto = new DrawPareto();
        drawPareto.SegmentIntervalNumber = n;
        drawPareto.ShowPercentOnSecondaryYAxis = false;
        drawPareto.CreatePareto(chart1, "RawData", "Histogram");


        drawPareto.MakeParetoChart(chart1, "Histogram", "Pareto");

        // Set chart types for output data
        chart1.Series["Pareto"].ChartType = SeriesChartType.Line;

        // set the markers for each point of the Pareto Line
        chart1.Series["Pareto"].IsValueShownAsLabel = true;
        chart1.Series["Pareto"].MarkerColor = Color.Red;
        chart1.Series["Pareto"].MarkerBorderColor = Color.MidnightBlue;
        chart1.Series["Pareto"].MarkerStyle = MarkerStyle.Circle;
        chart1.Series["Pareto"].MarkerSize = 6;
        chart1.Series["Pareto"].LabelFormat = "0.#";  // format with one decimal and leading zero

        // Set Color of line Pareto chart
        chart1.Series["Pareto"].Color = Color.FromArgb(252, 180, 65);


    }
    /// <summary>
    /// SPC分析正态分布图
    /// </summary>
    /// <param name="X"></param>
    /// <param name="n"></param>
    /// <param name="chart1"></param>
    public static void ChartQualityData_NormalDistribution(double[] X, int n, Chart chart1)
    {
        double ValueUp = Max(X);
        double ValueDown = Min(X);
        //绘图数据
        ASPNet_Drawing.CSpc_Data_Cpk Spc_Data_Cpk = ASPNet_Drawing.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;
        chart1.ChartAreas["ChartArea1"].AxisX.Minimum = Math.Round(ValueDownX - X1, 2);
        chart1.ChartAreas["ChartArea1"].AxisX.Maximum = Math.Round(ValueUpX + X1, 2);
        chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
        chart1.ChartAreas["ChartArea1"].AxisY.Maximum = Math.Round(ValueUpY + ValueUpY / 10, 2);
        //绘图
        for (int i = 0; i < Spc_Data_Cpk.NormalDistributionX.Length; i++)
        {
            chart1.Series[0].Points.AddXY(Math.Round(Spc_Data_Cpk.NormalDistributionX[i], 2), Math.Round(Spc_Data_Cpk.NormalDistributionY[i], 2));
        }
        // Set Strip line item
        //chart1.ChartAreas["ChartArea1"].AxisX.StripLines[0].IntervalOffset = Spc_Data_Cpk.LSL;
        //chart1.ChartAreas["ChartArea1"].AxisX.StripLines[0].StripWidth = Spc_Data_Cpk.USL - Spc_Data_Cpk.LSL;

        // Set Strip line item
        //double SL = (ValueUpX + ValueDownX) / 2;
        //double USL = SL + 3 * Spc_Data_Cpk.Singma;
        //double LSL = SL - 3 * Spc_Data_Cpk.Singma;
        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[1].IntervalOffset = Spc_Data_Cpk.NormalDistributionX[4];
        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[1].Text = "SL=" + Spc_Data_Cpk.NormalDistributionX[4].ToString("F2");
        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[2].IntervalOffset = Spc_Data_Cpk.NormalDistributionX[1];
        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[2].Text = "USL=" + Spc_Data_Cpk.NormalDistributionX[1].ToString("F2");
        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[3].IntervalOffset = Spc_Data_Cpk.NormalDistributionX[7];
        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[3].Text = "LSL=" + Spc_Data_Cpk.NormalDistributionX[7].ToString("F2");

        //TextAnnotation annotation1 = new TextAnnotation();
        //annotation1.Text = "Cpk=" + Spc_Data_Cpk.Cpk.ToString("F2");
        //annotation1.ForeColor = Color.Black;
        //annotation1.Font = new Font("Arial", 11); ;
        //annotation1.X = 80;
        //annotation1.Y = 8;
        //chart1.Annotations.Add(annotation1);

        //TextAnnotation annotation2 = new TextAnnotation();
        //annotation2.Text = "CPL=" + Spc_Data_Cpk.CPL.ToString("F2");
        //annotation2.ForeColor = Color.Black;
        //annotation2.Font = new Font("Arial", 11); ;
        //annotation2.X = 80;
        //annotation2.Y = 13;
        //chart1.Annotations.Add(annotation2);

        //TextAnnotation annotation3 = new TextAnnotation();
        //annotation3.Text = "CPU=" + Spc_Data_Cpk.CPU.ToString("F2");
        //annotation3.ForeColor = Color.Black;
        //annotation3.Font = new Font("Arial", 11); ;
        //annotation3.X = 80;
        //annotation3.Y = 18;
        //chart1.Annotations.Add(annotation3);


    }

    /// <summary>
    /// SPC分析工序能力图
    /// 在均值级差图中
    /// </summary>
    /// <param name="X"></param>
    /// <param name="n"></param>
    /// <param name="chart1"></param>
    public static void ChartQualityData_Cpk_XR(double[] X, int n, Chart chart1)
    {
        double ValueUp = Max(X);
        double ValueDown = Min(X);
        //绘图数据
        ASPNet_Drawing.CSpc_Data_Cpk Spc_Data_Cpk = ASPNet_Drawing.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;
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.Minimum = Math.Round(ValueDownX - X1, 2);
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.Maximum = Math.Round(ValueUpX + X1, 2);
        chart1.ChartAreas["ChartArea_Cpk"].AxisY.Minimum = 0;
        chart1.ChartAreas["ChartArea_Cpk"].AxisY.Maximum = Math.Round(ValueUpY + ValueUpY / 10, 2);
        //绘图
        for (int i = 0; i < Spc_Data_Cpk.NormalDistributionX.Length; i++)
        {
            chart1.Series["Series_Cpk"].Points.AddXY(Math.Round(Spc_Data_Cpk.NormalDistributionX[i], 2), Math.Round(Spc_Data_Cpk.NormalDistributionY[i], 2));
        }

        ASPNet_Drawing.CSpc_Data_XR Spc_Data_XR = ASPNet_Drawing.Spc_Data.XR(X, n);

        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[1].IntervalOffset = Spc_Data_Cpk.NormalDistributionX[4];
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[1].Text = "SL=" + Spc_Data_XR.CL_X.ToString("F2");
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[2].IntervalOffset = Spc_Data_Cpk.NormalDistributionX[1];
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[2].Text = "USL=" + Spc_Data_XR.UCL_X.ToString("F2");
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[3].IntervalOffset = Spc_Data_Cpk.NormalDistributionX[7];
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[3].Text = "LSL=" + Spc_Data_XR.LCL_X.ToString("F2");

        TextAnnotation annotation1 = new TextAnnotation();
        annotation1.Text = "Cpk=" + Spc_Data_Cpk.Cpk.ToString("F2");
        annotation1.ForeColor = Color.Black;
        annotation1.Font = new Font("Arial", 6); ;
        annotation1.X = 27;
        annotation1.Y = 3.5;
        chart1.Annotations.Add(annotation1);

        TextAnnotation annotation2 = new TextAnnotation();
        annotation2.Text = "CPL=" + Spc_Data_Cpk.CPL.ToString("F2");
        annotation2.ForeColor = Color.Black;
        annotation2.Font = new Font("Arial", 6); ;
        annotation2.X = 27;
        annotation2.Y = 5;
        chart1.Annotations.Add(annotation2);

        TextAnnotation annotation3 = new TextAnnotation();
        annotation3.Text = "CPU=" + Spc_Data_Cpk.CPU.ToString("F2");
        annotation3.ForeColor = Color.Black;
        annotation3.Font = new Font("Arial", 6); ;
        annotation3.X = 27;
        annotation3.Y = 6.5;
        chart1.Annotations.Add(annotation3);


    }

    /// <summary>
    /// SPC分析工序能力图
    /// 在均值标准差图中
    /// </summary>
    /// <param name="X"></param>
    /// <param name="n"></param>
    /// <param name="chart1"></param>
    public static void ChartQualityData_Cpk_XS(double[] X, int n, Chart chart1)
    {
        double ValueUp = Max(X);
        double ValueDown = Min(X);
        //绘图数据
        ASPNet_Drawing.CSpc_Data_Cpk Spc_Data_Cpk = ASPNet_Drawing.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;
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.Minimum = Math.Round(ValueDownX - X1, 2);
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.Maximum = Math.Round(ValueUpX + X1, 2);
        chart1.ChartAreas["ChartArea_Cpk"].AxisY.Minimum = 0;
        chart1.ChartAreas["ChartArea_Cpk"].AxisY.Maximum = Math.Round(ValueUpY + ValueUpY / 10, 2);
        //绘图
        for (int i = 0; i < Spc_Data_Cpk.NormalDistributionX.Length; i++)
        {
            chart1.Series["Series_Cpk"].Points.AddXY(Math.Round(Spc_Data_Cpk.NormalDistributionX[i], 2), Math.Round(Spc_Data_Cpk.NormalDistributionY[i], 2));
        }

        ASPNet_Drawing.CSpc_Data_XR Spc_Data_XR = ASPNet_Drawing.Spc_Data.XR(X, n);

        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[1].IntervalOffset = Spc_Data_Cpk.NormalDistributionX[4];
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[1].Text = "SL=" + Spc_Data_XR.CL_X.ToString("F2");
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[2].IntervalOffset = Spc_Data_Cpk.NormalDistributionX[1];
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[2].Text = "USL=" + Spc_Data_XR.UCL_X.ToString("F2");
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[3].IntervalOffset = Spc_Data_Cpk.NormalDistributionX[7];
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[3].Text = "LSL=" + Spc_Data_XR.LCL_X.ToString("F2");

        TextAnnotation annotation1 = new TextAnnotation();
        annotation1.Text = "Cpk=" + Spc_Data_Cpk.Cpk.ToString("F2");
        annotation1.ForeColor = Color.Black;
        annotation1.Font = new Font("Arial", 6); ;
        annotation1.X = 27;
        annotation1.Y = 3.5;
        chart1.Annotations.Add(annotation1);

        TextAnnotation annotation2 = new TextAnnotation();
        annotation2.Text = "CPL=" + Spc_Data_Cpk.CPL.ToString("F2");
        annotation2.ForeColor = Color.Black;
        annotation2.Font = new Font("Arial", 6); ;
        annotation2.X = 27;
        annotation2.Y = 5;
        chart1.Annotations.Add(annotation2);

        TextAnnotation annotation3 = new TextAnnotation();
        annotation3.Text = "CPU=" + Spc_Data_Cpk.CPU.ToString("F2");
        annotation3.ForeColor = Color.Black;
        annotation3.Font = new Font("Arial", 6); ;
        annotation3.X = 27;
        annotation3.Y = 6.5;
        chart1.Annotations.Add(annotation3);


    }


    /// <summary>
    /// SPC分析工序能力图
    /// </summary>
    /// <param name="X"></param>
    /// <param name="n"></param>
    /// <param name="chart1"></param>
    public static void ChartQualityData_Cpk(double[] X, int n, Chart chart1)
    {
        double ValueUp = Max(X);
        double ValueDown = Min(X);
        //绘图数据
        ASPNet_Drawing.CSpc_Data_Cpk Spc_Data_Cpk = ASPNet_Drawing.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;
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.Minimum = Math.Round(ValueDownX - X1, 2);
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.Maximum = Math.Round(ValueUpX + X1, 2);
        chart1.ChartAreas["ChartArea_Cpk"].AxisY.Minimum = 0;
        chart1.ChartAreas["ChartArea_Cpk"].AxisY.Maximum = Math.Round(ValueUpY + ValueUpY / 10, 2);
        //绘图
        for (int i = 0; i < Spc_Data_Cpk.NormalDistributionX.Length; i++)
        {
            chart1.Series["Series_Cpk"].Points.AddXY(Math.Round(Spc_Data_Cpk.NormalDistributionX[i], 2), Math.Round(Spc_Data_Cpk.NormalDistributionY[i], 2));            
        }
        // Set Strip line item
        //chart1.ChartAreas["ChartArea1"].AxisX.StripLines[0].IntervalOffset = Spc_Data_Cpk.LSL;
        //chart1.ChartAreas["ChartArea1"].AxisX.StripLines[0].StripWidth = Spc_Data_Cpk.USL - Spc_Data_Cpk.LSL;

        // Set Strip line item
        //double SL = (ValueUpX + ValueDownX) / 2;
        //double USL = SL + 3 * Spc_Data_Cpk.Singma;
        //double LSL = SL - 3 * Spc_Data_Cpk.Singma;
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[1].IntervalOffset = Spc_Data_Cpk.NormalDistributionX[4];
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[1].Text = "SL=" + Spc_Data_Cpk.NormalDistributionX[4].ToString("F2");
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[2].IntervalOffset = Spc_Data_Cpk.NormalDistributionX[1];
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[2].Text = "USL=" + Spc_Data_Cpk.NormalDistributionX[1].ToString("F2");
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[3].IntervalOffset = Spc_Data_Cpk.NormalDistributionX[7];
        chart1.ChartAreas["ChartArea_Cpk"].AxisX.StripLines[3].Text = "LSL=" + Spc_Data_Cpk.NormalDistributionX[7].ToString("F2");

        TextAnnotation annotation1 = new TextAnnotation();
        annotation1.Text = "Cpk=" + Spc_Data_Cpk.Cpk.ToString("F2");
        annotation1.ForeColor = Color.Black;
        annotation1.Font = new Font("Arial", 11); ;
        annotation1.X = 80;
        annotation1.Y = 8;
        chart1.Annotations.Add(annotation1);

        TextAnnotation annotation2 = new TextAnnotation();
        annotation2.Text = "CPL=" + Spc_Data_Cpk.CPL.ToString("F2");
        annotation2.ForeColor = Color.Black;
        annotation2.Font = new Font("Arial", 11); ;
        annotation2.X = 80;
        annotation2.Y = 13;
        chart1.Annotations.Add(annotation2);

        TextAnnotation annotation3 = new TextAnnotation();
        annotation3.Text = "CPU=" + Spc_Data_Cpk.CPU.ToString("F2");
        annotation3.ForeColor = Color.Black;
        annotation3.Font = new Font("Arial", 11); ;
        annotation3.X = 80;
        annotation3.Y = 18;
        chart1.Annotations.Add(annotation3);


    }

    /// <summary>
    /// 机床报警频次分析
    /// </summary>
    /// <param name="dt"></param>
    /// <param name="chart1"></param>
    public static void ChartMachineAlarm(DataTable dt, Chart chart1)
    {
        if (dt.Rows.Count == 0)
            return;
        string seriesName = dt.Rows[0]["工位号"].ToString();
        chart1.Series.Add(seriesName);
        chart1.Series[seriesName].ChartType = SeriesChartType.Column;
        chart1.Series[seriesName].BorderWidth = 2;
        chart1.Series[seriesName].ToolTip = "报警文本：#VALX \n报警数量：#VALY";
        chart1.Series[seriesName].Points.DataBindXY(dt.Rows, "报警文本", dt.Rows, "报警数量");
        //for (int i = 0; i < 10; i++)
        //{
        //    chart1.Series[seriesName].Points.AddXY(dt.Rows[i]["报警编号"], dt.Rows[i]["报警数量"]);
        //}
        //chart1.DataBind();
    }

    ///// <summary>
    ///// 机床状态频次分析
    ///// </summary>
    ///// <param name="dt"></param>
    ///// <param name="chart1"></param>
    //public static void ChartMachineStatus(DataTable dt, Chart chart1)
    //{

    //    foreach (DataRow row in dt.Rows)
    //    {
    //        // For each Row add a new series
    //        string seriesName = row["工位号"].ToString();
    //        chart1.Series.Add(seriesName);
    //        chart1.Series[seriesName].ChartType = SeriesChartType.StackedBar;
    //        chart1.Series[seriesName].BorderWidth = 2;
    //        chart1.Series[seriesName].ToolTip = "工位号：" + seriesName + "\n  频次：= #VALY";

    //        for (int colIndex = 2; colIndex < dt.Columns.Count; colIndex++)
    //        {
    //            // For each column (column 1 and onward) add the value as a point
    //            string columnName = dt.Columns[colIndex].ColumnName;
    //            int YVal = (int)row[columnName];
    //            chart1.Series[seriesName].Points.AddXY(columnName, YVal);
    //        }
    //    }

    //}
    ///// <summary>
    ///// 成海 2012-04-05
    ///// </summary>
    ///// <param name="dt"></param>
    ///// <param name="chart1"></param>
    public static void ChartMachineStatus(DataTable dt, Chart chart1)
    {
        string seriesNameA = "机床上电";
        chart1.Series.Add(seriesNameA);
        chart1.Series[seriesNameA].ChartType = SeriesChartType.StackedColumn;
        chart1.Series[seriesNameA].BorderWidth = 2;
        chart1.Series[seriesNameA].ToolTip = "机床状态：" + seriesNameA + "\n  频次：= #VALY";
        chart1.Series[seriesNameA].Color = System.Drawing.Color.DeepPink;
        string seriesNameB = "循环开始";
        chart1.Series.Add(seriesNameB);
        chart1.Series[seriesNameB].ChartType = SeriesChartType.StackedColumn;
        chart1.Series[seriesNameB].BorderWidth = 2;
        chart1.Series[seriesNameB].ToolTip = "机床状态：" + seriesNameB + "\n  频次：= #VALY";
        chart1.Series[seriesNameB].Color = System.Drawing.Color.Green;
        string seriesNameC = "机床故障";
        chart1.Series.Add(seriesNameC);
        chart1.Series[seriesNameC].ChartType = SeriesChartType.StackedColumn;
        chart1.Series[seriesNameC].BorderWidth = 2;
        chart1.Series[seriesNameC].ToolTip = "机床状态：" + seriesNameC + "\n  频次：= #VALY";
        chart1.Series[seriesNameC].Color = System.Drawing.Color.Red;
        string seriesNameD = "上料无件";
        chart1.Series.Add(seriesNameD);
        chart1.Series[seriesNameD].ChartType = SeriesChartType.StackedColumn;
        chart1.Series[seriesNameD].BorderWidth = 2;
        chart1.Series[seriesNameD].ToolTip = "机床状态：" + seriesNameD + "\n  频次：= #VALY";
        chart1.Series[seriesNameD].Color = System.Drawing.Color.Blue;
        string seriesNameE = "下料堵塞";
        chart1.Series.Add(seriesNameE);
        chart1.Series[seriesNameE].ChartType = SeriesChartType.StackedColumn;
        chart1.Series[seriesNameE].BorderWidth = 2;
        chart1.Series[seriesNameE].ToolTip = "机床状态：" + seriesNameE + "\n  频次：= #VALY";
        chart1.Series[seriesNameE].Color = System.Drawing.Color.Yellow;
        foreach (DataRow row in dt.Rows)
        {
            string opName = row["工位号"].ToString();
            int YValA = (int)row["机床上电"];
            chart1.Series[seriesNameA].Points.AddXY(opName, YValA);
            int YValB = (int)row["循环开始"];
            chart1.Series[seriesNameB].Points.AddXY(opName, YValB);
            int YValC = (int)row["机床故障"];
            chart1.Series[seriesNameC].Points.AddXY(opName, YValC);
            int YValD = (int)row["上料无件"];
            chart1.Series[seriesNameD].Points.AddXY(opName, YValD);
            int YValE = (int)row["下料堵塞"];
            chart1.Series[seriesNameE].Points.AddXY(opName, YValE);

        }


    }
    /// <summary>
    /// SPC分析基本趋势图
    /// </summary>
    /// <param name="dt"></param>
    /// <param name="chart1"></param>
    public static void ChartQualityData_TrendPictureBasic(DataTable dt, Chart chart1)
    {
        //string seriesName = "series1";
        //chart1.Series.Add(seriesName);
        //chart1.Series[seriesName].MarkerStyle = MarkerStyle.Circle;
        //chart1.Series[seriesName].MarkerSize = 5;
        //chart1.Series[seriesName].MasrkerColor = Color.Magenta;

        //chart1.Series[seriesName].ChartType = SeriesChartType.Line;
        //chart1.Series[seriesName].BorderWidth = 1;
        chart1.Series[0].Points.DataBind(dt.DefaultView, "", "测量值", "Tooltip=ToolTip");
        //chart1.DataBinds();

    }
    /// <summary>
    /// 子组平均值的平均值
    /// n 	子组大小。单个子组观测值的个数
    /// </summary>
    /// <param name="X">全部测量数据数组</param>
    /// <param name="n">子组大小。单个子组观测值的个数</param>
    /// <param name="Xk">子组平均值的数组</param>
    /// <returns></returns>
    static private double Average(double[] X, int n)
    {
        //k	子组个数
        int k;
        k = X.Length / n;
        double[] Xi = new double[n];
        double[] Xk = new double[k];
        for (int j = 0; j < k; j++)
        {
            for (int i = 0; i < n; i++)
            {
                Xi[i] = X[j * n + i];
            }
            //子组平均值
            Xk[j] = Average(Xi);
        }
        //子组平均值的平均值			
        return Average(Xk);
    }
    /// <summary>
    /// 一组观测值的均值
    /// </summary>
    /// <param name="Xn">子组观测值</param>
    /// <returns></returns>
    static private double Average(double[] Xn)
    {
        if (Xn.Length < 1) return 0;
        return Sum(Xn) / (double)Xn.Length;
    }
    /// <summary>
    /// 观测值和
    /// </summary>
    /// <param name="Xn">子组观测值</param>
    /// <returns></returns>
    static private double Sum(double[] Xn)
    {
        double temp;
        temp = 0;
        for (int i = 0; i < Xn.Length; i++)
        {
            temp = temp + Xn[i];
        }
        return temp;
    }
    /// <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>
    /// 均值极差图
    /// </summary>
    /// <param name="X"></param>
    /// <param name="n"></param>
    /// <param name="chart1"></param>
    public static void ChartQualityData_XR(double[] X, int n, Chart chart1)
    {
        //计算XR控制图的数据
        ASPNet_Drawing.CSpc_Data_XR Spc_Data_XR = ASPNet_Drawing.Spc_Data.XR(X, n);
        for (int i = 0; i < Spc_Data_XR.CL_Xk.Length; i++)
        {
            //X
            chart1.Series["Series_X"].Points.AddXY(i + 1, Spc_Data_XR.CL_Xk[i]);
            //R
            chart1.Series["Series_R"].Points.AddXY(i + 1, Spc_Data_XR.CL_Rk[i]);
        }
        chart1.Series["Series_X"].ToolTip = "测量值：= #VALY";
        chart1.Series["Series_R"].ToolTip = "测量值：= #VALY";

        ////均值图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)
        {
            chart1.ChartAreas["ChartArea_X"].AxisY.Maximum = Convert.ToDouble((maxXk + gapAxis_X / 8).ToString("F2"));
        }
        else
        {
            chart1.ChartAreas["ChartArea_X"].AxisY.Maximum = Convert.ToDouble((Spc_Data_XR.UCL_X + gapAxis_X / 8).ToString("F2"));            
        }
        if (minXk < Spc_Data_XR.LCL_X)
        {
            chart1.ChartAreas["ChartArea_X"].AxisY.Minimum = Convert.ToDouble((minXk - gapAxis_X / 8).ToString("F2"));
        }
        else
        {
            chart1.ChartAreas["ChartArea_X"].AxisY.Minimum = Convert.ToDouble((Spc_Data_XR.LCL_X - gapAxis_X / 8).ToString("F2"));            
        }

        ////极差图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)
        {
            chart1.ChartAreas["ChartArea_R"].AxisY.Maximum = Convert.ToDouble((maxRk + gapAxis_R / 8).ToString("F2"));
        }
        else
        {
            chart1.ChartAreas["ChartArea_R"].AxisY.Maximum = Convert.ToDouble((Spc_Data_XR.UCL_R + gapAxis_R / 8).ToString("F2"));            
        }
        if (minRk < Spc_Data_XR.LCL_R)
        {
            chart1.ChartAreas["ChartArea_R"].AxisY.Minimum = Convert.ToDouble((minRk - gapAxis_R / 8).ToString("F2"));
        }
        else
        {
            chart1.ChartAreas["ChartArea_R"].AxisY.Minimum = Convert.ToDouble((Spc_Data_XR.LCL_R - gapAxis_R / 8).ToString("F2"));            
        }


        // Set axis title
        chart1.ChartAreas["ChartArea_X"].AxisY.Title = "均值图";
        chart1.ChartAreas["ChartArea_R"].AxisY.Title = "极差图";
        chart1.ChartAreas["ChartArea_X"].AxisY.TitleFont = new Font("Microsoft Sans Serif", 8);
        chart1.ChartAreas["ChartArea_R"].AxisY.TitleFont = new Font("Microsoft Sans Serif", 8);


        // Set Strip line item
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[0].IntervalOffset = Spc_Data_XR.LCL_X;
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[0].StripWidth = Spc_Data_XR.UCL_X - Spc_Data_XR.LCL_X;

        // Set Strip line item
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[1].IntervalOffset = Spc_Data_XR.CL_X;
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[1].Text = "CL=" + Spc_Data_XR.CL_X.ToString("F2");
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[2].IntervalOffset = Spc_Data_XR.UCL_X;
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[2].Text = "UCL=" + Spc_Data_XR.UCL_X.ToString("F2");
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[3].IntervalOffset = Spc_Data_XR.LCL_X;
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[3].Text = "LCL=" + Spc_Data_XR.LCL_X.ToString("F2");

        // Set Strip line item
        chart1.ChartAreas["ChartArea_R"].AxisY.StripLines[0].IntervalOffset = Spc_Data_XR.LCL_R;
        chart1.ChartAreas["ChartArea_R"].AxisY.StripLines[0].StripWidth = Spc_Data_XR.UCL_R - Spc_Data_XR.LCL_R;

        // Set Strip line item
        chart1.ChartAreas["ChartArea_R"].AxisY.StripLines[1].IntervalOffset = Spc_Data_XR.CL_R;
        chart1.ChartAreas["ChartArea_R"].AxisY.StripLines[1].Text = "CL=" + Spc_Data_XR.CL_R.ToString("F2");
        chart1.ChartAreas["ChartArea_R"].AxisY.StripLines[2].IntervalOffset = Spc_Data_XR.UCL_R;
        chart1.ChartAreas["ChartArea_R"].AxisY.StripLines[2].Text = "UCL=" + Spc_Data_XR.UCL_R.ToString("F2");
        chart1.ChartAreas["ChartArea_R"].AxisY.StripLines[3].IntervalOffset = Spc_Data_XR.LCL_R;
        chart1.ChartAreas["ChartArea_R"].AxisY.StripLines[3].Text = "LCL=" + Spc_Data_XR.LCL_R.ToString("F2");

    }

    /// <summary>
    /// 均值标准差图
    /// </summary>
    /// <param name="X"></param>
    /// <param name="n"></param>
    /// <param name="chart1"></param>
    public static void ChartQualityData_XS(double[] X, int n, Chart chart1)
    {
        //计算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++)
        {
            //X
            chart1.Series["Series_X"].Points.AddXY(i + 1, Spc_Data_XS.CL_Xk[i]);
            //S
            chart1.Series["Series_S"].Points.AddXY(i + 1, Spc_Data_XS.CL_Sk[i]);
        }
        chart1.Series["Series_X"].ToolTip = "测量值：= #VALY";
        chart1.Series["Series_S"].ToolTip = "测量值：= #VALY";

        ////均值图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)
        {
            chart1.ChartAreas["ChartArea_X"].AxisY.Maximum = Convert.ToDouble((maxXk + gapAxis_X / 8).ToString("F2"));
        }
        else
        {
            chart1.ChartAreas["ChartArea_X"].AxisY.Maximum = Convert.ToDouble((Spc_Data_XS.UCL_X + gapAxis_X / 8).ToString("F2"));
        }
        if (minXk < Spc_Data_XS.LCL_X)
        {
            chart1.ChartAreas["ChartArea_X"].AxisY.Minimum = Convert.ToDouble((minXk - gapAxis_X / 8).ToString("F2"));
        }
        else
        {
            chart1.ChartAreas["ChartArea_X"].AxisY.Minimum = Convert.ToDouble((Spc_Data_XS.LCL_X - gapAxis_X / 8).ToString("F2"));
        }

        ////标准差图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)
        {
            chart1.ChartAreas["ChartArea_S"].AxisY.Maximum = Convert.ToDouble((maxRk + gapAxis_R / 8).ToString("F2"));
        }
        else
        {
            chart1.ChartAreas["ChartArea_S"].AxisY.Maximum = Convert.ToDouble((Spc_Data_XS.UCL_S + gapAxis_R / 8).ToString("F2"));
        }
        if (minRk < Spc_Data_XS.LCL_S)
        {
            chart1.ChartAreas["ChartArea_S"].AxisY.Minimum = Convert.ToDouble((minRk - gapAxis_R / 8).ToString("F2"));
        }
        else
        {
            chart1.ChartAreas["ChartArea_S"].AxisY.Minimum = Convert.ToDouble((Spc_Data_XS.LCL_S - gapAxis_R / 8).ToString("F2"));
        }


        // Set axis title
        chart1.ChartAreas["ChartArea_X"].AxisY.Title = "均值图";
        chart1.ChartAreas["ChartArea_S"].AxisY.Title = "标准差图";
        chart1.ChartAreas["ChartArea_X"].AxisY.TitleFont = new Font("Microsoft Sans Serif", 8);
        chart1.ChartAreas["ChartArea_S"].AxisY.TitleFont = new Font("Microsoft Sans Serif", 8);


        // Set Strip line item
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[0].IntervalOffset = Spc_Data_XS.LCL_X;
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[0].StripWidth = Spc_Data_XS.UCL_X - Spc_Data_XS.LCL_X;

        // Set Strip line item
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[1].IntervalOffset = Spc_Data_XS.CL_X;
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[1].Text = "CL=" + Spc_Data_XS.CL_X.ToString("F2");
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[2].IntervalOffset = Spc_Data_XS.UCL_X;
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[2].Text = "UCL=" + Spc_Data_XS.UCL_X.ToString("F2");
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[3].IntervalOffset = Spc_Data_XS.LCL_X;
        chart1.ChartAreas["ChartArea_X"].AxisY.StripLines[3].Text = "LCL=" + Spc_Data_XS.LCL_X.ToString("F2");

        // Set Strip line item
        chart1.ChartAreas["ChartArea_S"].AxisY.StripLines[0].IntervalOffset = Spc_Data_XS.LCL_S;
        chart1.ChartAreas["ChartArea_S"].AxisY.StripLines[0].StripWidth = Spc_Data_XS.UCL_S - Spc_Data_XS.LCL_S;

        // Set Strip line item
        chart1.ChartAreas["ChartArea_S"].AxisY.StripLines[1].IntervalOffset = Spc_Data_XS.CL_S;
        chart1.ChartAreas["ChartArea_S"].AxisY.StripLines[1].Text = "CL=" + Spc_Data_XS.CL_S.ToString("F2");
        chart1.ChartAreas["ChartArea_S"].AxisY.StripLines[2].IntervalOffset = Spc_Data_XS.UCL_S;
        chart1.ChartAreas["ChartArea_S"].AxisY.StripLines[2].Text = "UCL=" + Spc_Data_XS.UCL_S.ToString("F2");
        chart1.ChartAreas["ChartArea_S"].AxisY.StripLines[3].IntervalOffset = Spc_Data_XS.LCL_S;
        chart1.ChartAreas["ChartArea_S"].AxisY.StripLines[3].Text = "LCL=" + Spc_Data_XS.LCL_S.ToString("F2");
    }

    ///// <summary>
    ///// 故障率 lvzhen
    ///// </summary>
    ///// <param name="dt"></param>
    ///// <param name="chart1"></param>
    //public static void ChartFaultPercent(DataTable dt, Chart chart1)
    //{
    //    if (dt.Rows.Count == 0)
    //        return;
    //    foreach (DataRow row in dt.Rows)
    //    {
    //        string seriesName = row["工位号"].ToString();
    //        chart1.Series.Add(seriesName);
    //        chart1.Series[seriesName].ChartType = SeriesChartType.Column;
    //        chart1.Series[seriesName].BorderWidth = 2;
    //        chart1.Series[seriesName].ToolTip = "工位号：" + row["工位号"].ToString() + "\n运行时间: " + row["运行时间"].ToString() + "\n故障时间: " + row["故障时间"].ToString() + "\n故障率：#VALY";
    //        for (int colIndex = 3; colIndex < dt.Columns.Count; colIndex++)
    //        {
    //            // For each column (column 1 and onward) add the value as a point
    //            string columnName = dt.Columns[colIndex].ColumnName;
    //            string YVal = row[columnName].ToString();
    //            chart1.Series[seriesName].Points.AddXY(columnName, YVal);
    //        }
    //        //for (int i = 0; i < 10; i++)
    //        //{
    //        //    chart1.Series[seriesName].Points.AddXY(dt.Rows[i]["报警编号"], dt.Rows[i]["报警数量"]);
    //        //}
    //        //chart1.DataBind();
    //    }
    //}

    ///// <summary>
    ///// 故障率 lvzhen
    ///// </summary>
    ///// <param name="dt"></param>
    ///// <param name="chart1"></param>
    //public static void ChartFaultPercentTrue(DataTable dt, Chart chart1)
    //{
    //    if (dt.Rows.Count == 0)
    //        return;
    //    foreach (DataRow row in dt.Rows)
    //    {
    //        string seriesName = row["工位号"].ToString();
    //        chart1.Series.Add(seriesName);
    //        chart1.Series[seriesName].ChartType = SeriesChartType.Column;
    //        chart1.Series[seriesName].BorderWidth = 2;
    //        chart1.Series[seriesName].ToolTip = "工位号：" + row["工位号"].ToString() + " \n故障率：#VALY";
    //        for (int colIndex = 3; colIndex < dt.Columns.Count; colIndex++)
    //        {
    //            // For each column (column 1 and onward) add the value as a point
    //            string columnName = dt.Columns[colIndex].ColumnName;
    //            string YVal = row[columnName].ToString();
    //            chart1.Series[seriesName].Points.AddXY(columnName, YVal);
    //        }
    //        //for (int i = 0; i < 10; i++)
    //        //{
    //        //    chart1.Series[seriesName].Points.AddXY(dt.Rows[i]["报警编号"], dt.Rows[i]["报警数量"]);
    //        //}
    //        //chart1.DataBind();
    //    }
    //}
    ///// <summary>
    ///// 开机率 lvzhen
    ///// </summary>
    ///// <param name="dt"></param>
    ///// <param name="chart1"></param>
    //public static void ChartKaijiProportion(DataTable dt, Chart chart1)
    //{
    //    chart1.Series[0].Points.DataBind(dt.DefaultView, "操作日期", "开机率", "Tooltip=ToolTip");
    //}

}