• 教你用MSChart控件绘制正态分布图形。


          一年多的“潜伏”也算是深资“特务”了,长时间看别人的博客,自己却没有写点东东,对不起那些园中劳碌的人。今天终于可以“洗牌”了做正常劳苦大众。那也得感谢是项目交付期,有了“空挡”可以写点什么,让大家拍砖。

          今天主讲是的绘制正态分布图形所用的质量指标公式的,不注重讲MSChart图形控件的用法,MSChart图形控件博园中有很多实例。正态分布图形所用的质量指标如下:USL(下规格线简称:下限)、USL(上限)、SIGMA(西格玛)、XBAR(平均值)、SAMPLE DATA(样本数据)、Zoom Multiple(缩放倍数)、Most Precision(最大精度)。

    1. Most Precision:最大精度,即小数点后面的位数长度。
      View Code
       1         /// <summary>
       2         /// 获取数据序列的最大精度 (即小数点后面的位数长度)
       3         /// </summary>
       4         /// <param name="sampleData">样本数据</param>
       5         /// <returns></returns>
       6         public static int GetMostPrecision(List<decimal> sampleData)
       7         {
       8             if (sampleData == null || sampleData.Count == 0)
       9             {
      10                 return 0;
      11             }
      12 
      13             int mostPrecision = 0;
      14             int tempValue = 0;
      15 
      16             foreach (decimal value in sampleData)
      17             {
      18                 string data = Math.Abs(value).ToString();
      19                 int dateLength = data.Length;
      20                 int dotIndex = data.IndexOf(".");
      21 
      22                 if (dotIndex > 0)
      23                 {
      24                     tempValue = dateLength - (dotIndex + 1);
      25                 }
      26 
      27                 if (tempValue > mostPrecision) //取更大的精度
      28                 {
      29                     mostPrecision = tempValue;
      30                 }
      31             }
      32 
      33             return mostPrecision;
      34         }
    2. Zoom Multiple:缩放倍数,为了增加图形边沿线的平滑度。
      View Code
       1         /// <summary>
       2         /// 缩放倍数
       3      /// </summary>
       4        /// <param name="mostPrecision"></param>
       5        /// <returns></returns>
       6         private static decimal ZoomMultiple(ref int mostPrecision)
       7         {
       8             decimal zoomMultiple = (decimal)Math.Pow(10, mostPrecision - 1);
       9 
      10             if (mostPrecision <= 2) //保证精度大于二的数据序列图形的平滑
      11             {
      12                 mostPrecision = 4;
      13                 zoomMultiple = 100;
      14             }
      15 
      16             return zoomMultiple;
      17         }
    3. SAMPLE DATA:样本数据是概率运算里的一个概念。随机抽取的部分用于计算出性能优良的数量。
    4. XBAR:这个大家好理解,也很好计算。即是所有样本数据之和的平均值。
      View Code
      1 double xbar = Math.Round(sampleData.Average(), mostPrecision);
    5. SIGMA:是一个希腊字母σ的中文译音,在统计学中,代表标准偏差,用来对过程变异进行测量。
      View Code
       1         /// <summary>
       2         /// 计算Sigma
       3         /// </summary>
       4         /// <param name="sampleData">样本数据</param>
       5         /// <param name="xbar">平均值</param>
       6         /// <returns></returns>
       7         public static double CalculateSigma(List<decimal> sampleData, double xbar)
       8         {
       9             double sigma = 0;
      10             int sampleCount = sampleData.Count;
      11             double powSum = 0;
      12 
      13             if (sampleData == null || sampleCount <= 2
      14 ) //样本个数大于2计算才有意思
      15             {
      16                 return sigma;
      17             }
      18 
      19             foreach (double value in sampleData)
      20             {
      21                 powSum += Math.Pow(value - xbar, 2); //样本值减去均值2的次幂相加。
      22             }
      23 
      24             sigma = Math.Sqrt(powSum / (sampleCount - 1));
      25 
      26             return sigma;
      27         }
    6. USL和LSL一般是抽样人员事先设置好的,也可以用公式得到:
      View Code
      1 double usl = xbar + 3 * sigma;//均值加3sigma
      2 double lsl = xbar - 3 * sigma;//均值减3sigma

           上面是正态分布图所用到的指标计算公式或方法,公式的解析详情http://zhidao.baidu.com/question/86348249.html?an=0&si=5下面该介绍正态分布的公式或方法,至于MSChart样式设置略讲,如有需要联系我kingzheng@yeah.net

         首先清除图表Series集合上的数据点:

    1 chart.Series[serieIndex].Points.Clear();

         X轴正、负界限以及正态公式系数:

    1 int positiveLimit = (int)((xbar + 6 * sigma) * zoomMultiple); //X轴的正界限
    2 int minusLimit = (int)((xbar - 6 * sigma) * zoomMultiple); //X轴的负界限
    3 double coefficient = Math.Round(1 / Math.Sqrt(2 * Math.PI) / sigma, mostPrecision); //系数;如果计算需要精确,就不要四舍五入;建议:为了提高运算效率要四舍五入。

         根据公式生成正态图形所需要的数据点:

     1 List<double> xValues = new List<double>();
     2 List<double> yValues = new List<double>();
     3  
     4 for (int x = minusLimit; x <= positiveLimit; x++)
     5        {
     6        //x轴缩小zoomMultiple倍x每隔1/zoomMultiple变化曲线变平滑
     7          double xValue = x / zoomMultiple;
     8          double yValue = coefficient * Math.Exp(Math.Pow((xValue - xbar), 2) / (-2 * Math.Pow(sigma, 2)));
     9           xValue = Math.Round(xValue, mostPrecision);
    10           yValue = Math.Round(yValue, mostPrecision);
    11            if (yValue > 0.0001)//可设为yValue > 0
    12              {
    13                 xValues.Add(xValue);
    14                 yValues.Add(yValue);
    15              }
    16      }
    17 
    18 //为MSChart绑定数据值
    19  chart.Series[serieIndex].Points.DataBindXY(xValues, yValues);

         为了确保图形显示完全,调整X和Y轴的最大值和最小值刻度:

     1  if (yValues.Count > 0)
     2      {
     3         yAxisMax = 0;
     4         return;
     5 
     6      }
     7 //将Y轴最大值放大倍作为
     8        double yMax = Math.Round(yValues.Max() * 1.1, args.MostPrecision);
     9        double xMin = xValues.Min();           
    10        double xMax = xValues.Max();
    11        double yMin = yValues.Min();
    12        yAxisMax = yValues.Max();
    13 
    14     if (xMin > lsl)
    15       {
    16         xMin = lsl;
    17       }
    18     if (xMax < usl)
    19       {
    20        xMax = usl;
    21      }
    22 //设置轴值x轴加减极大极小值的1/zoomMultiple倍是为了图形能全部绘制出来
    23 chart.ChartAreas[0].AxisX.Minimum = (double)Math.Round(xMin - xMin * 1 / zoomMultiple, mostPrecision);
    24 chart.ChartAreas[0].AxisX.Maximum = (double)Math.Round(xMax + xMax * 1 / zoomMultiple, mostPrecision);
    25 chart.ChartAreas[0].AxisY.Minimum = (double)Math.Round(yMin, mostPrecision);
    26 chart.ChartAreas[0].AxisY.Maximum = (double)Math.Round(yMax, mostPrecision);

           分别添加XBAR、USL、LSL阈值限函数如下:

    View Code
     1         /// <summary>
     2         /// 添加阈值线
     3      /// </summary>
     4         /// <param name="chartArea">图形Area</param>
    
     5         /// <param name="lineName">在线显示的名子</param>
     6         /// <param name="lineOffset">线在图上的位置</param>
     7         /// <param name="lineWidth">线宽</param>
     8         /// <param name="lineColor">线的颜色</param>
     9         public void AddStripLine(ChartArea chartArea, string lineName, double lineOffset, double lineWidth, Color lineColor)
    10         {
    11             StripLine stripLine = new StripLine
    12             {
    13                 BackColor = lineColor,
    14                 StripWidth = lineWidth,
    15                 BackHatchStyle = ChartHatchStyle.DarkVertical,
    16                 Text = lineName,
    17                 TextAlignment = StringAlignment.Far,
    18                 TextLineAlignment = StringAlignment.Center,
    19                 IntervalOffset = lineOffset
    20             };
    21 
    22             chartArea.AxisY.StripLines.Add(stripLine);
    23         }

          最后是根据样本值重置X轴的最大和最小刻度

    View Code
     1         /// <summary>
     2         /// 根据sampleData的最大和最小值重设X轴的最大和最小刻度
     3       /// </summary>
     4         /// <param name="queueValue"></param>
     5         public static void ResetAxisBySampleData(List<decimal> sampleData, Chart chart)
     6         {
     7             if (sampleData == null || sampleData.Count <= 0)
     8             {
     9                 SwapValue(chart);
    10                 return;
    11             }
    12 
    13             double max = (double) sampleData.Max();
    14             double min = (double) sampleData.Min();
    15             double xMax = chart.ChartAreas[0].AxisX.Maximum;
    16             double xMin = chart.ChartAreas[0].AxisX.Minimum;
    17 
    18             if (xMin > xMax)
    19             {
    20                 chart.ChartAreas[0].AxisX.Minimum = min;
    21                 chart.ChartAreas[0].AxisX.Maximum = max;
    22             }
    23         }

    以上是正态分布图所用到的指标和函数,正态直方图所用到的指标和函数下次再讲,时间仓促,写的不细。如有不明的请发邮件,我们一起探讨。Mail:kingzheng@yeah.net 如要转发,请注明出处!!

        

  • 相关阅读:
    get pc name in LAN
    study spring
    android install
    【转】Java:Session详解
    classic problem: producer and consumer
    tomcat install
    验证Monitor.PulseAll功效
    (转)Windows7的图形架构与DX的那点事
    Cannot open your default email folders. The attempt to log on to Microsoft Exchange has failed.
    Monitor.Wait初探(8)
  • 原文地址:https://www.cnblogs.com/netstart/p/Netstart.html
Copyright © 2020-2023  润新知