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;
using System.Web.UI.DataVisualization.Charting.Utilities;

/// <summary>
///DrawChart 的摘要说明
/// </summary>
public partial class DrawChart
{
    /// <summary>
    /// 排列图
    /// </summary>
    /// <param name="X"></param>
    /// <param name="chart1"></param>
    public static void ChartQualityData_Pareto(double[] X,Chart chart1)
    {
        int n=8;
        chart1.Series["RawData"].Points.DataBindY(X);

        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>
    /// 直方图
    /// </summary>
    /// <param name="X"></param>
    /// <param name="chart1"></param>
    public static void ChartQualityData_Histogram(double[] X, Chart chart1)
    {
        int n = 8;
        chart1.Series["RawData"].Points.DataBindY(X);
        // Populate single axis data distribution series. Show Y value of the
        // data series as X value and set all Y values to 1.
        foreach (DataPoint dataPoint in chart1.Series["RawData"].Points)
        {
            chart1.Series["DataDistribution"].Points.AddXY(dataPoint.YValues[0], 1);
        }
        // Create a histogram series
        HistogramChartHelper histogramHelper = new HistogramChartHelper();
        histogramHelper.SegmentIntervalNumber = n;
        histogramHelper.ShowPercentOnSecondaryYAxis = false;
        // NOTE: Interval width may be specified instead of interval number
        //histogramHelper.SegmentIntervalWidth = 15;
        histogramHelper.CreateHistogram(chart1, "RawData", "Histogram");
    }

    /// <summary>
    /// 分析正态分布图
    /// </summary>
    /// <param name="X">被分析数据组</param>
    /// <param name="Usl">理论上限</param>
    /// <param name="Lsl">理论下限</param>
    /// <param name="chart1"></param>
    public static void ChartQualityData_NormalDistribution(double[] X, double Usl, double Lsl, Chart chart1)
    {
        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;
        chart1.ChartAreas["ChartArea1"].AxisX.Minimum = Math.Round(ValueDownX - X1, 4);
        chart1.ChartAreas["ChartArea1"].AxisX.Maximum = Math.Round(ValueUpX + X1, 4);
        chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
        chart1.ChartAreas["ChartArea1"].AxisY.Maximum = Math.Round(ValueUpY + ValueUpY / 10, 4);
        //绘图正态分布曲线(共取了11个点)
        for (int i = 0; i < sx.Length; i++)
        {
            chart1.Series[0].Points.AddXY(Math.Round(sx[i], 4), Math.Round(sy[i], 4));
        }

        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[1].IntervalOffset = sx[5];
        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[1].Text = "SL=" + sx[5].ToString("F4");
        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[2].IntervalOffset = Usl;
        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[2].Text = "USL=" + Usl.ToString("F4");
        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[3].IntervalOffset = Lsl;
        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[3].Text = "LSL=" + Lsl.ToString("F4");
        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[4].IntervalOffset = QL;
        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[4].Text = "QL=" + QL.ToString("F4");
        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[5].IntervalOffset = QU;
        chart1.ChartAreas["ChartArea1"].AxisX.StripLines[5].Text = "QU=" + QU.ToString("F4");

        //将CpCpk绘制到图形中
        DarwCpCpk(chart1, cp, cpk);
        //TextAnnotation annotation3;
        //annotation3 = new TextAnnotation();
        //annotation3.Text = "Cp=" + cp.ToString("F2");
        //annotation3.ForeColor = Color.Black;
        //annotation3.Font = new Font("Arial", 11); ;
        //annotation3.X = 60;
        //annotation3.Y = 90;
        //chart1.Annotations.Add(annotation3);
        //annotation3 = new TextAnnotation();
        //annotation3.Text = "Cpk=" + cpk.ToString("F2");
        //annotation3.ForeColor = Color.Black;
        //annotation3.Font = new Font("Arial", 11); ;
        //annotation3.X = 80;
        //annotation3.Y = 90;
        //chart1.Annotations.Add(annotation3);
    }
    /// <summary>
    /// SPC分析基本趋势图
    /// </summary>
    /// <param name="dt"></param>
    /// <param name="chart1"></param>
    public static void ChartQualityData_TrendPictureBasic_new(DataTable dt, double Usl, double Lsl, Chart chart1)
    {

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

        double[] X;
        if (dt.Rows.Count < 3) return;
        X = new double[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            X[i] = Convert.ToDouble(dt.Rows[i][0]);
        }

        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;
        double ValueDownX;

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

        ValueUpX = Math.Max(ValueUp, Usl);
        ValueUpX = Math.Max(ValueUpX, QU);

        double X1 = (ValueUpX - ValueDownX) / 10;
        chart1.ChartAreas["ChartArea1"].AxisY.Minimum = Math.Round(ValueDownX - X1, 4);
        chart1.ChartAreas["ChartArea1"].AxisY.Maximum = Math.Round(ValueUpX + X1, 4);


        chart1.ChartAreas["ChartArea1"].AxisY.StripLines[1].IntervalOffset = sx[5];
        chart1.ChartAreas["ChartArea1"].AxisY.StripLines[1].Text = "SL=" + sx[5].ToString("F4");
        chart1.ChartAreas["ChartArea1"].AxisY.StripLines[2].IntervalOffset = Usl;
        chart1.ChartAreas["ChartArea1"].AxisY.StripLines[2].Text = "USL=" + Usl.ToString("F4");
        chart1.ChartAreas["ChartArea1"].AxisY.StripLines[3].IntervalOffset = Lsl;
        chart1.ChartAreas["ChartArea1"].AxisY.StripLines[3].Text = "LSL=" + Lsl.ToString("F4");
        chart1.ChartAreas["ChartArea1"].AxisY.StripLines[4].IntervalOffset = QU;
        chart1.ChartAreas["ChartArea1"].AxisY.StripLines[4].Text = "QU=" + QU.ToString("F4");
        chart1.ChartAreas["ChartArea1"].AxisY.StripLines[5].IntervalOffset = QL;
        chart1.ChartAreas["ChartArea1"].AxisY.StripLines[5].Text = "QL=" + QL.ToString("F4");

        chart1.Series[0].Points.DataBind(dt.DefaultView, "", "测量值", "Tooltip=ToolTip");
        //将CpCpk绘制到图形中
        DarwCpCpk(chart1, cp, cpk);
    }
    /// <summary>
    /// 将CpCpk绘制到图形中
    /// </summary>
    /// <param name="chart1"></param>
    /// <param name="cp"></param>
    /// <param name="cpk"></param>
    static private void DarwCpCpk(Chart chart1,double cp,double cpk)
    {
        TextAnnotation annotation;
        annotation = (TextAnnotation)chart1.Annotations["TextAnnotationCp"];
        annotation.Text = "Cp=" + cp.ToString("F2");
        annotation = (TextAnnotation)chart1.Annotations["TextAnnotationCpk"];
        annotation.Text = "Cpk=" + cpk.ToString("F2");
    }

}
