Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Control Chart Using .NET

0.00/5 (No votes)
27 Jan 2015 1  
USL/LSL Control Chart using .NET for Quality Control

Introduction

Nowadays, automobile industry is interested in automated measuring machines to ensure quality and to compete in the global industry. Control Chart takes a major role in ensuring quality by measuring automobile components. The purpose is to make a simple Control Bar Chart for Measuring Systems like Camshaft, Crankshaft, and Master Setting, and so on. A digital sensor is used for measuring Camshaft and Crankshaft. Then the measured data is analyzed using the Control Chart .

Control Chart(ref)

The Control Charts are graphs which is used to check the quality of control of a process. In Control charts, UCL/LCL or USL/LSL will be used to check with the resultant data. If Measurement data is within the range of limits, then the process is OK(GOOD). If the Measurement data is above or below the limits, the process is NG(NOT GOOD).

  1. OK (GOOD) -> USL <= Measurement Data >= LSL
  2. NG (NOT GOOD) -> Upper Range -> Measurement Data > USL
  3. NG (NOT GOOD) -> Lower Range -> Measurement Data < LSL

In my simple Control Chart, USL /LSL is used for verifying the data.USL -> Upper Specification Limit and LSL -> Lower Specification Limit. Note in this sample, I have used USL/LSL.

Difference between USL/LSL and UCL/LCL.

(The UCL or upper control limit and LCL or lower control limit are limits set by your process based on the actual amount of variation of your process.)

The USL or upper specification limit and LSL or lower specification limit are limits set by your customer's requirements. This is the variation that they will accept from your process. Reference)

I have been working on several automation projects. Instead of using third-party tools, I planned to create a simple program using .NET for creating a USL/LSL Control Bar chart for master setting. The main purpose of this article is to share what I have developed to other members.

I have created a Control Chart as a User Control so that it can be used easily in all projects.

In this article, I have attached the zip file named as SHANUControlChart_SRC.zip which contains:

  1. "SHANUControlChart_CNT" folder (This folder contains the Control Chart User control Source code.)
  2. "SHANUControlChart_DEMO" folder (This folder contains the Demo program which includes the Control Chart user control with Random Measurement sample using Timer control).

Using the Code

  1. First, we will start with the User Control. To create a user control:
    1. Create a new Windows Control Library project.
    2. Set the Name of Project and Click Ok (here, my user control name is SHANUControlChart_CNT).
    3. Add all the controls which are needed.
    4. In code behind, declare all the public variables and Public property variables. Here, USL/LSL/Nominal and Measurement Data Public property has been declared which will be used to pass the data from the windows application.
      'Public Property Declaration
         Public Property MasterData() As String
             Get
                 Return lblMasterData.Text
             End Get
             Set(value As String)
                 lblMasterData.Text = value
             End Set
         End Property
      
         Public Property USLData() As String
             Get
                 Return lblUslData.Text
             End Get
             Set(value As String)
                 lblUslData.Text = value
             End Set
         End Property
      
         Public Property LSLData() As String
             Get
                 Return lblLslData.Text
             End Get
             Set(value As String)
                 lblLslData.Text = value
             End Set
         End Property
      
      
         Public Property NominalData() As String
             Get
                 Return lblNominalData.Text
             End Get
             Set(value As String)
                 lblNominalData.Text = value
             End Set
         End Property
      
    5. In my User control, I have used Timer control to always check for the Measurement data and produce the Bar Chart. In user control Load, I have Enabled and Start the Timer. In Timer Tick Event, I called the Function "LoadcontrolChart()". In this function, I set all USL/LSL and measurement data and draw the Chart control.
      Public Sub LoadcontrolChart()
              Try
                  'For Bar and GageLoad
                  upperLimitChk = False
                  lowerLimitchk = False
                  errLimtchk = False
                  Dim pointVal As Integer
                  Dim calcvalues As Double
                  Dim calcvalues1 As Double
                  '  Dim upperValue As Double
                  Dim hasread As Integer
                  Dim UpperRange As Double
                  Dim err As String
                  pointVal = 3
                  Dim ival As Integer
      
                  sensordata = Convert.ToDouble(lblMasterData.Text.Trim())
      
                  frmMasteringPictuerBox.Refresh()
      
                  upperValue = System.Convert.ToDouble(lblUslData.Text)
                  lovervalue = System.Convert.ToDouble(lblLslData.Text)
                  If upperValue = lovervalue Then
                      lovervalue = lovervalue - 1
                  End If
      
                  inputValue = System.Convert.ToDouble(lblMasterData.Text)
                  'here we call the draw rectangle function 
                  drawRectanles(barheight, barwidth, upperValue, lovervalue, 
                                loverInitialvalue, upperInititalvalue, xval, yval)
                
      
              Catch ex As Exception
              End Try
          End Sub

      In this method, we check the Measurement data with USL and LSL Limit Value. If Measurement data is within the range of USL and LSL, then the resultant output will be Ok. I have used the if condition to check the resultant values as below:

       ''This method is to draw the control chart 
          Public Sub drawRectanles(ByVal barheight As Double, ByVal barwidth As Double, _
                ByVal uppervalue As Double, ByVal lovervalue As Double, _
                ByVal loverinitialvalue As Double, ByVal upperinitialvalue As Double, _
                ByVal xval As Double, ByVal yval As Double)
              Try
                  Dim limitsline As Double
                  Dim lowerlimitline As Double
                  Dim underrange As Double
                  Dim upperrange As Double
                  Dim differentpercentage As Double
                  Dim totalheight As Double
                  Dim inputvalueCal As Double
                  Dim finaldisplayvalue As Double
                  Dim backColors1 As Color
                  Dim backColors2 As Color
                  'this is for the range percentage Calculation
                  differentpercentage = uppervalue - lovervalue
                  differentpercentage = differentpercentage * 0.2
                  'Upper range value
                  upperrange = uppervalue + differentpercentage
                  'Lover range value
                  underrange = lovervalue - differentpercentage
                  totalheight = upperrange - underrange
                  'For Upper Limit
                  ''limitsline = barheight * 0.2 - 10
                  limitsline = lovervalue - underrange
                  limitsline = limitsline / totalheight
                  limitsline = barheight * limitsline + 10
                  'For Lower Limit
                  lowerlimitline = uppervalue - underrange
                  lowerlimitline = lowerlimitline / totalheight
                  lowerlimitline = barheight * lowerlimitline + 10
                  'lowerlimitline = barheight - limitsline + 10
                  'for finding the rangedata
                  inputvalueCal = inputValue - underrange
                  finaldisplayvalue = inputvalueCal / totalheight
                  finaldisplayvalue = barheight * finaldisplayvalue
                  Dim g As Graphics = frmMasteringPictuerBox.CreateGraphics
                  Dim f5 As Font
                  f5 = New Font("arial", 22, FontStyle.Bold, GraphicsUnit.Pixel)
                  ''If condition to check for the result and display 
                  ''the chart accordingly depend on limit values.
                  If inputValue = "0.000" Then
                      If zeroDisplay = 0 Then
                          backColors1 = Color.Red
                          backColors2 = Color.DarkRed
                          Dim a5 As New System.Drawing.Drawing2D.LinearGradientBrush_
                             (New RectangleF(0, 0, 90, 19), backColors1, backColors2, _
                              Drawing.Drawing2D.LinearGradientMode.Horizontal)
                          g.DrawString("NG -> UnderRange", f5, a5, 
                                        System.Convert.ToInt32(xval) - 40, _
                                 barheight + 24)
                          lblMasterData.ForeColor = Color.Red
      
                          frmMasteringlblmsg.Text = "NG -> UnderRange"
                      Else
                          backColors1 = Color.GreenYellow
                          backColors2 = Color.DarkGreen
                          Dim a5 As New System.Drawing.Drawing2D.LinearGradientBrush_
                                 (New RectangleF(0, 0, 90, 19), backColors1, backColors2, _
                                  Drawing.Drawing2D.LinearGradientMode.Horizontal)
                          g.DrawString("OK -> Good", f5, a5, _
                                 System.Convert.ToInt32(xval) - 40, barheight + 24)
                          lblMasterData.ForeColor = Color.GreenYellow
      
                          frmMasteringlblmsg.Text = "OK -> Good"
                      End If
                  ElseIf inputValue >= lovervalue And inputValue <= uppervalue Then
                      backColors1 = Color.GreenYellow
                      backColors2 = Color.DarkGreen
                      If upperLimitChk = False Then
                          backColors1 = Color.GreenYellow
                          backColors2 = Color.DarkGreen
                          lblMasterData.ForeColor = Color.GreenYellow
                      Else
                          backColors1 = Color.Red
                          backColors2 = Color.DarkRed
                          lblMasterData.ForeColor = Color.Red
                      End If
                      Dim a5 As New System.Drawing.Drawing2D.LinearGradientBrush_
                            (New RectangleF(0, 0, 100, 19), backColors1, backColors2, _
                             Drawing.Drawing2D.LinearGradientMode.Horizontal)
                      g.DrawString("OK -> Good", f5, a5, 
                                    System.Convert.ToInt32(xval) - 40, barheight + 24)
      
                      frmMasteringlblmsg.Text = "OK -> Good"
                      'frmMasteringlblOrignalData.ForeColor = Color.GreenYellow
                  ElseIf inputValue < lovervalue Then
                      backColors1 = Color.Red
                      backColors2 = Color.DarkRed
                      Dim a5 As New System.Drawing.Drawing2D.LinearGradientBrush_
                           (New RectangleF(0, 0, 100, 19), backColors1, backColors2, _
                            Drawing.Drawing2D.LinearGradientMode.Horizontal)
                      g.DrawString("NG -> UnderRange", f5, 
                                    a5, System.Convert.ToInt32(xval) - 40, _
                            barheight + 24)
                      lblMasterData.ForeColor = Color.Red
                      'frmMasteringlblOrignalData.ForeColor = Color.Red
      
                      frmMasteringlblmsg.Text = "NG -> UnderRange"
                  ElseIf inputValue > uppervalue Then
                      backColors1 = Color.Red
                      backColors2 = Color.DarkRed
                      Dim a5 As New System.Drawing.Drawing2D.LinearGradientBrush_
                          (New RectangleF(0, 0, 100, 19), backColors1, backColors2, _
                          Drawing.Drawing2D.LinearGradientMode.Horizontal)
                      g.DrawString("NG -> UpperRange", f5, a5, 
                                   System.Convert.ToInt32(xval) - 40, _
                          barheight + 24)
                      lblMasterData.ForeColor = Color.Red
                      'frmMasteringlblOrignalData.ForeColor = Color.Red
      
                      frmMasteringlblmsg.Text = "NG -> UpperRange"
                  Else
                      backColors1 = Color.Red
                      backColors2 = Color.DarkRed
                      Dim a5 As New System.Drawing.Drawing2D.LinearGradientBrush_
                         (New RectangleF(0, 0, 100, 19), backColors1, backColors2, _
                          Drawing.Drawing2D.LinearGradientMode.Horizontal)
                      g.DrawString("Not Good", f5, a5, _
                                    System.Convert.ToInt32(xval) - 40, barheight + 24)
                      lblMasterData.ForeColor = Color.Red
      
                      frmMasteringlblmsg.Text = "Not Good"
                  End If
              
                  Dim apen As New Pen(Color.Black, 2)
                  g.DrawRectangle(Pens.Black, System.Convert.ToInt32(xval) - 2, _
                        System.Convert.ToInt32(yval) - 2, System.Convert.ToInt32(barwidth) + 3, _
                        System.Convert.ToInt32(barheight) + 3)
      
                  g.DrawLine(apen, System.Convert.ToInt32(xval) - 15, _
                                       System.Convert.ToInt32(limitsline), _
                  System.Convert.ToInt32(xval), System.Convert.ToInt32(limitsline))
                  g.DrawLine(apen, System.Convert.ToInt32(xval) - 15, _
                                       System.Convert.ToInt32(lowerlimitline), _
                  System.Convert.ToInt32(xval), System.Convert.ToInt32(lowerlimitline))
                  Dim a1 As New System.Drawing.Drawing2D.LinearGradientBrush_
                                      (New RectangleF(0, 0, 100, 19), _
                  Color.Blue, Color.Orange, Drawing.Drawing2D.LinearGradientMode.Horizontal)
                  Dim f As Font
                  f = New Font("arial", 10, FontStyle.Bold, GraphicsUnit.Pixel)
                  g.DrawString((String.Format("{0:N3}", CDbl(uppervalue.ToString))), _
                  f, a1, System.Convert.ToInt32(xval) - 40, _
                                     System.Convert.ToInt32(limitsline) + 1)
                  g.DrawString((String.Format("{0:N3}", CDbl(lovervalue.ToString))), _
                  f, a1, System.Convert.ToInt32(xval) - 40, _
                                     System.Convert.ToInt32(lowerlimitline) + 1)
                  g.DrawString((String.Format("{0:N3}", CDbl(upperrange.ToString))), _
                  f, a1, System.Convert.ToInt32(xval) - 40, 9)
                  g.DrawString((String.Format("{0:N3}", CDbl(underrange.ToString))), _
                  f, a1, System.Convert.ToInt32(xval) - 40, barheight + 10)
                  Dim a As New System.Drawing.Drawing2D.LinearGradientBrush(New RectangleF_
                  (xval, barheight + 10, barwidth, finaldisplayvalue + 1), backColors1, _
                  backColors2, Drawing.Drawing2D.LinearGradientMode.Vertical)
                
                  Dim a2 As New System.Drawing.Drawing2D.LinearGradientBrush_
                                 (New RectangleF(0, 0, 100, 19), _
                  Color.Black, Color.Black, Drawing.Drawing2D.LinearGradientMode.Horizontal)
                  Dim f2 As Font
                  f2 = New Font("arial", 12, FontStyle.Bold, GraphicsUnit.Pixel)
                  If inputValue >= upperrange Then
                      g.FillRectangle(a, New RectangleF(xval, 10, barwidth, barheight))
                      g.DrawString((String.Format("{0:N3}", CDbl(inputValue.ToString))), _
                      f2, a2, System.Convert.ToInt32(xval) + 40, yval - 8)
                  ElseIf inputValue <= underrange Then
                      g.FillRectangle(a, New RectangleF(xval, barheight + 10, barwidth, 4))
                      g.DrawString((String.Format("{0:N3}", CDbl(inputValue.ToString))), _
                      f2, a2, System.Convert.ToInt32(xval) + 40, barheight + 10)
                  Else
                      g.FillRectangle(a, New RectangleF_
                      (xval, barheight - finaldisplayvalue + 10, barwidth, finaldisplayvalue))
                      g.DrawString((String.Format("{0:N3}", CDbl(inputValue.ToString))), _
                      f2, a2, System.Convert.ToInt32(xval) + 40, _
                             barheight - System.Convert.ToInt32(finaldisplayvalue))
                  End If
                  ' End If
                  g.Dispose()
              Catch ex As Exception
              End Try
          End Sub
    6. After completion save, build and run the project.
  2. Now we create a Windows application and add and test our "SHANUControlChart_CNT" User Control.
    1. Create a new Windows project.
    2. Open your form and then from Toolbox > right click > choose items > browse select your user control DLL and add.
    3. Drag the User Control to your windows form.
    4. Place all the USL/LSL and Measurement Textbox for input from the user. In Manual check button click pass all the data to user control using the public property as below:
      private void btnDisplay_Click(object sender, EventArgs e)
             {
                 shanuControlChart.USLData = txtusl.Text;
                 shanuControlChart.LSLData = txtLSL.Text;
                 shanuControlChart.NominalData = txtNominal.Text;
                 shanuControlChart.MasterData = txtData.Text;
             }
      
    5. In my demo program, I have used the Timer for the random sample Measurement data result checking. I have used the "btnRealTime" as Toggle button. When clicked for the first time, Enable and Start the Timer and same button is clicked again, Stop the Timer. When timer is Started, I have generated a random number and pass the different data to user control and check for the chart result.
       private void btnRealTime_Click(object sender, EventArgs e)
              {
                  if (btnRealTime.Text == "Real Time Data ON")
                  {
                       btnRealTime.Text = "Real Time Data OFF";
                       btnRealTime.ForeColor = Color.Red;
                       timer1.Enabled = true;
                       timer1.Start();
                  }
                  else
                  {
                      btnRealTime.Text = "Real Time Data ON";
                      btnRealTime.ForeColor = Color.DarkGreen;
                      timer1.Enabled = false;
                      timer1.Stop();
                  }
              }
      
      // Timer Tick Event to check for the different random sample Measurement test data.
      
              private void timer1_Tick(object sender, EventArgs e)
              {
                 Random rnd =new Random();
      
              Double rndval = rnd.Next(1, 20);
      
              txtData.Text = rndval.ToString("0.000");//FormatNumber(rndval.ToString(), 3, , 0)
      
              shanuControlChart.USLData = txtusl.Text;
              shanuControlChart.LSLData = txtLSL.Text;
              shanuControlChart.NominalData = txtNominal.Text;
              shanuControlChart.MasterData = txtData.Text;
              }

Conclusion

The main aim of this article is to create a simple and standard Control Bar Chart user control for the automobile industry.

History

  • 2014/07/14: Initial release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here