CHARTrunner 1.6 Software Developer's Kit

Programmer's Guide

Date: 02-July-2003
Copyright © 1999-2003 PQ Systems, Inc.

Introduction

This document applies only to CHARTrunner version 1.6.  It does not apply to earlier versions.

CHARTrunner is a software application for creating, displaying, and publishing statistical process control (SPC) charts. The charts may use data from a wide variety of sources including Microsoft Access, SQL Server, Microsoft Excel, Oracle, and even simple text files. It runs on Microsoft Windows 95, 98, ME, NT, 2000 and XP. CHARTrunner may be used as an SPC charting component that is integrated with other systems or applications. This document describes some of the techniques available for performing this integration.

The CHARTrunner ActiveX components can be used from within your own program to define a chart definition using code, read a chart definition from disk, save a chart definition to disk, display a chart in a window, display a chart in your own Visual Basic form using the CHARTrunner 1.6 Charting Control, generate a chart image file, print a chart, create a clipCHART, get chart statistics, etc.

CHARTrunner-e is the web server version of CHARTrunner. It may be used to publish charts defined in CHARTrunner so that the charts may be viewed by anyone with a web browser and access to the web server where CHARTrunner-e is installed. The CHARTrunner-e client has the ability to completely customize any chart or to define and draw a chart definition "on-the-fly". Currently this feature is only available via scripting code on the client. Click here for more information.

CHARTrunner was created by PQ Systems, Inc. the developers of SQCpack™. To learn more about PQ Systems, visit:

    http://www.pqsystems.com

To learn more about CHARTrunner, visit:

    http://www.chartrunner.com

What's New in Version 1.6?

Licensing

If you install CHARTrunner or any CHARTrunner component on a computer, you must purchase a CHARTrunner license for that computer. CHARTrunner and/or CHARTrunner components are licensed on a per-computer basis. Any system or application derived from CHARTrunner and/or CHARTrunner components may be used only on computers for which a CHARTrunner license has been purchased. The complete terms of each type of license agreement is available at: 

    http://www.chartrunner.com/license.htm 

Reference Manual

Click here for the Reference Manual which provides more information about the objects, properties and methods exposed by the CHARTrunner ActiveX components. Click here for the most recent version on the web.

Programming Environments

CHARTrunner uses a set of ActiveX components. ActiveX is a Microsoft standard for developing programmable, interchangeable software components. These components may be programmed using a variety of software development environments. For example, Microsoft ships a version of the Visual Basic programming language, called VBA, with Microsoft Word and Excel. Macros in Word and Excel are implemented using this VBA scripting language. One way to program the CHARTrunner components is to use VBA from within Word or Excel. 

Here is a partial list of development environments that can be used to program CHARTrunner components: 

The CHARTrunner Software Developer's Kit includes examples using many of these development environments. For a detailed description of the Windows Scripting Host, search the Microsoft web site.

General overview 

When programming CHARTrunner, it is helpful to understand the following terms: 

An Object is a programming construct which can be created, used, and destroyed while a program is running. A Property describes some aspect of the object. For example, a car is an object, the color of the car is a property. A Method is some action that can be taken with the object or done to the object. For example: 

Object: car
Property: car.color = red
Method: car.start (as in start the car)

Objects may have many properties and many methods. As a programmer, if you can see an object’s properties or methods, these are known as public; either public properties or public methods. Objects may have properties and methods that the programmer does not see. These are known as private. Generally, you do not need to know anything about the private properties and methods; you need only to know they exist. 

When programming CHARTrunner, the two primary objects you will use are:

Each of these objects has various properties and methods. By setting these properties and calling the methods, you can take advantage of CHARTrunner functionality without actually running CHARTrunner. These two objects will allow you to do the most common integration tasks. For example, from within your own program or macro you can: 

Creating a Reference to the CHARTrunner 1.6 Application DLL 

When CHARTrunner is installed, an ActiveX file named PqCr16.DLL is installed and registered. This file is usually in the C:\Windows\System folder or in C:\Winnt\System32 under Windows NT/2000/XP. It contains the CHARTrunner ActiveX components. If you are programming in Visual Basic or in VBA (within Word or Excel) you generally must add a reference to this file in your project so that you will be able to use CHARTrunner objects. 

To do this in Visual Basic, open the Projects/References pull-down menu. In Excel, you do this by selecting Tools/Macro/Visual Basic Editor (to launch VB) and then selecting Tools/References from the VB menu. In either case, you need to check the box next to the CHARTrunner 1.6 Application DLL reference. This tells your program that you will be using objects from the within that ActiveX DLL. This step is essential; the following examples will not function without this reference.

Although your project needs to reference only the CHARTrunner 1.6 Application DLL, there are several other files that are needed; these are installed in the same location as PqCr16.DLL when CHARTrunner is installed: 

Some Coding Examples 

Here are some examples. They assume CHARTrunner is installed this folder: 

  • C:\Program Files\PQ Systems\CHARTrunner 1.6\

  • Note: If you have installed CHARTrunner in a different folder, you will need to adjust the examples accordingly. In this first example, we show you how to get started in the Excel VB editor. The rest of the examples will be source code only and assume you can get into an appropriate development environment such as VBA in Excel, VB 6, or one of the others mentioned above.

    Start Excel. From the pull down menu, select Tools/Macro/Macros. You should see a dialog as shown above. 

    Give the macro a name by typing DrawFirstChart. Next, click on the Create button. You should see something like the picture below (you will put your source code between the Sub and the End Sub):

    From the pull down menu, select Tools/References. Put a check next to CHARTrunner 1.6 Application DLL so that the properties and methods of the objects contained in that ActiveX DLL are available to your scripting code.

    Enter the following code for our first example:

    Sub DrawFirstChart()

     

    Dim bRtn As Boolean

    Dim bShowModal As Boolean

    Dim sFile As String

    Dim sMsg As String

    Dim oChRunApp As New CHARTrunner16.Application

     

       sFile = "C:\Program Files\PQ Systems\CHARTrunner 1.6\SampleCharts\Sample 01.crf"

       

       ' Show the chart window modally.  In some scripting environments you must display the chart

       ' in a modal window or an error will occur.  In others, you can use a non-modal chart

       ' window if you like.

       bShowModal = True

     

       bRtn = oChRunApp.DisplayChart(sFile, bShowModal)

       

       If Not bRtn Then

          MsgBox oChRunApp.sLastError, vbExclamation

       End If

     

    End Sub 'DrawFirstChart'

    Once you have entered this source code try to run the macro. Do this by pressing the F5 (run) key while in the Excel VB editor. A CHARTrunner chart should be displayed in a modal chart window. If a chart is not displayed, you will need to debug the source code! Remember, if you installed CHARTrunner in a different folder, you will have to modify this source code accordingly. 

    Let’s discuss this example source code. The first five lines that start with Dim declare some variables that will be used in the macro. The most important variable is oChRunApp. This variable is declared using the New keyword which ensures that a new instance of this object will be created. In this case, the object is a CHARTrunner16.Application object. This is the primary object you will use when programming CHARTrunner

    The next line sets the variable sFile to the full path to one of the CHARTrunner sample charts. 

    This line of code is where all the action is:              

    bRtn = oChRunApp.DisplayChart(sFile, bShowModal) 

    We are using a method of the CHARTrunner16.Application object that is identified by the variable oChRunApp. The method is the DisplayChart method. The DisplayChart method requires only the first argument, the remaining arguments are optional and assume a default value if they are not supplied. In this case, the required argument is sFile, a string variable that contains the full path to a CHARTrunner chart definition file. The DisplayChart method returns a Boolean value, either True or False, that indicates whether the method completed successfully. By checking the variable bRtn, we can ask (in the next line of code) if the DisplayChart method was successful or not. When DisplayChart fails (returns False) the oChRunApp.sLastError property can be checked to see what went wrong.

    When you use the DisplayChart method, the chart is generated from fresh data. This means that the data source, as setup in the chart definition file, is queried for the most recent data, then the chart is displayed. The same is true for printing a chart or saving a chart image.

    Your first lesson in programming CHARTrunner is now complete. Although this example was done using VBA within Excel¸ the same general steps can be used in the other development environments. 

    Additionally, an Excel spreadsheet (ChartRunner.xls) containing example code can be found in the ..\Excel folder within the CHARTrunner Software Developer's Kit. The example (above) used "early binding" that required you to create a reference to the CHARTrunner 1.6 Application DLL using Tools/References. The sample code in ChartRunner.xls is setup by default to use "late binding" which does not require that a reference to CHARTrunner 1.6 Application DLL be established. The code shown Example 1 below also uses "late binding" since that is the only binding method supported by Windows Script Host.

    Example 1: Using the Windows Script Host.

    Microsoft® Windows® Script Host (WSH) is a language-independent scripting host for ActiveX® scripting engines. You can download Windows Scripting host from the Microsoft web site. The WSH allows you to create and run programs that use ActiveX components such as the CHARTrunner components. 

    If you remember batch files from the days of MS-DOS, you will recognize that the WSH gives you similar capability within the Windows environment. You can use a simple text editor such as the Windows Notepad to create files containing scripts. These scripts can instantiate ActiveX objects and then use their properties and methods. Currently, WSH scripts can be written in either VBScript or JavaScript. 

    Once a script has been written, it is saved either as a .vbs file or a .js file. The .vbs file is for VBScript ; the .js file is for JavaScript. Double clicking on either of these files in the Windows Explorer will run the scripts. Listed below are two WSH scripts that will display a CHARTrunner chart. They do the same thing that we did in the previous Excel example. You can use Notepad to create and try these scripts or you can look at these examples in the CHARTrunner Software Developer's Kit folders. You will notice slight syntax differences from the Excel example, but conceptually these scripts do the same thing. 

    VBScript Example (Sample1.vbs)

    Dim bRtn

    Dim oChRunApp

     

    Set oChRunApp=CreateObject("CHARTrunner16.Application")

     

    bRtn = oChRunApp.DisplayChart _

        ("C:\Program files\PQ Systems\CHARTrunner 1.6\SampleCharts\Sample 01.crf", True)

     

    If Not bRtn Then

       MsgBox oChRunApp.sLastError

    End If 

    The first two lines declare some variables. Note that these variables do not have a type, such as string or Boolean. In WSH, all variables are variants. Next, we create a CHARTrunner16.Application object by calling the CreateObject method. The next line is where the chart is displayed. Notice the True argument. This tells CHARTrunner to display the chart form modally. WSH does not allow the display of non-modal forms. Not setting this will cause the DisplayChart method to fail and return False. The rest of the code is similar to the previous Excel example. Next, we’ll look at the same script in JavaScript.

    JavaScript Example (Sample1.js)

    // Example Windows Script Host JavaScript - CHARTrunner

     

    // Declare some variables.

    var bShowModal

    var bRtn

    var sFile

    var sErrorMsg

    var oChRunApp

     

    // Set some of the variables to specific values.

    sFile = "C:\\Program files\\PQ Systems\\CHARTrunner 1.6\\SampleCharts\\Sample 01.crf"

    bShowModal = true;

     

    // Create the ChartRunner Application object.

    oChRunApp = WScript.CreateObject("CHARTrunner16.Application");

     

    // Ask the Application object to display the chart.

    bRtn = oChRunApp.DisplayChart(sFile, bShowModal);

     

    // If DisplayChart failed, put up a message box.

    if (bRtn != true)

       {

       sErrorMsg="Error in DisplayChart: " + oChRunApp.sLastError;

       WScript.Echo(sErrorMsg);

       } 

    The JavaScript syntax is slightly different but the example script does the same thing as the previous ones. Notice that we set bShowModal to true. When using WSH, you must set this to true to display a chart. Another difference to notice is the use of double \\ characters in the file name. Any line beginning with // is a comment in JavaScript.

    These examples demonstrated the same capability to display a CHARTrunner chart. The remaining examples will each show a different capability. To simplify the documentation, these will be shown in VBA code generated as Excel macros. Each of the examples is available in the Excel folder where you installed your CHARTrunner Software Developer's Kit. Lines beginning with a single quote are comments. 

    Example 2: Generating an Image from a CHARTrunner Chart.

    Sub GenerateChartBitmap()

     

    Dim bRtn As Boolean

    Dim sChartDefFile As String

    Dim sOutputFile As String

    Dim sMsg As String

    Dim oChRunApp As New CHARTrunner16.Application

     

       ' The chart definition file.

       sChartDefFile = "C:\Program Files\PQ Systems\CHARTrunner 1.6\SampleCharts\Sample 01.crf"

     

       ' The bitmap file to be created. Not specifying a path here

       ' usually results in the file being saved in c:\My Documents\

       sOutputFile = "Example2.jpg"

       

       ' Bitmap size is 640x480 pixels, Image type = eCrImageJpg

       ' Other valid image types are eCrImageBmp or eCrImageEmf.

       bRtn = oChRunApp.GenerateChartImageFile(sChartDefFile, sOutputFile, eCrImageJpg, 640, 480)

       

       If Not bRtn Then

          MsgBox oChRunApp.sLastError

       End If

     

    End Sub 'GenerateChartBitmap'

    Example 3: Displaying a Read-Only Chart.

    When you use the DisplayChart method, the chart is displayed in its own window with its own pull down menus. While the chart is displayed, the user may interact with the chart in various ways. There are options for editing the chart definition, compute control limits, and modify the style of the chart. If you would prefer not to allow the end users to make these changes, you must modify the way you call the DisplayChart method. Here is an example: 

    Sub DrawReadOnlyChart()

     

    Dim bRtn As Boolean

    Dim sChartDefFile As String

    Dim sMsg As String

    Dim bShowModal As Boolean

    Dim bReadOnly As Boolean

    Dim oChRunApp As New CHARTrunner16.Application

     

       ' The chart definition file.

       sChartDefFile = "C:\Program Files\PQ Systems\CHARTrunner 1.6\SampleCharts\Sample 01.crf"

     

       bShowModal = True

       bReadOnly = True

     

       bRtn = oChRunApp.DisplayChart(sChartDefFile, bShowModal, bReadOnly)

      

       If Not bRtn Then

          MsgBox oChRunApp.sLastError

       End If

     

    End Sub 'DrawReadOnlyChart'

    The DisplayChart method takes three arguments: Filename, bShowModal, and bReadOnly. Only the file name is required. The other arguments are optional. If you don’t set them, they default to False. If you want a read-only chart, you have to set the optional arguments as in the example above. 

    Example 4: Printing a Chart.

    Sub PrintChart()

     

    Dim bRtn As Boolean

    Dim bShowPrintDialog As Boolean

    Dim bPrintSep As Boolean

    Dim sFile As String

    Dim sMsg As String

    Dim sPrinterName As String

    Dim oChRunApp As New CHARTrunner16.Application

     

       sFile = "C:\Program Files\PQ Systems\CHARTrunner 1.6\SampleCharts\Sample 01.crf"

       

       bShowPrintDialog = True

       bPrintSep = False

       sPrinterName = ""                ' Use the default printer.

       

       bRtn = oChRunApp.PrintChart(sFile, bShowPrintDialog, bPrintSep, sPrinterName)

     

       If Not bRtn Then

          MsgBox oChRunApp.sLastError

       End If

     

    End Sub 'PrintChart'

    When you call the PrintChart method, the only required argument is sFile, the full path to the chart definition file. The other arguments are optional. If bPrintSep is True, each child chart in a multi-chart will print on its own page. If bShowPrintDialog is True, the printer dialog will be displayed.

    Example 5: Printing Several Charts at Once by Printing a Chart Workspace.

    The following example uses a CHARTrunner workspace file instead of a regular chart definition file. CHARTrunner saves three types of files. 1) Regular chart files that have a .CRF file extension, 2) Multi-chart files that contain a list of 1 or more regular charts and have a .CRM file extension, and 3) Chart workspace files that also contain a list of 1 or more regular charts and have a .CRW file extension. The difference between multi-charts and workspaces is that a multi-chart is viewed or printed with all charts in a single window or on a single page; for chart workspaces, each chart is viewed in its own window or printed on its own page. 

    Sub PrintChartsInWorkspace()

     

    Dim bRtn As Boolean

    Dim bShowPrintDialog As Boolean

    Dim bPrintSep As Boolean

    Dim sFile As String

    Dim sMsg As String

    Dim sPrinterName As String

    Dim oChRunApp As New CHARTrunner16.Application

     

       sFile = "C:\Program Files\PQ Systems\CHARTrunner 1.6\SampleCharts\Workspace sample.crw"

           

       bShowPrintDialog = True

       bPrintSep = False

       sPrinterName = ""                ' Use the default printer.

     

       bRtn = oChRunApp.PrintChart(sFile, bShowPrintDialog, bPrintSep, sPrinterName)

       

       If Not bRtn Then

          MsgBox oChRunApp.sLastError

       End If

     

    End Sub 'PrintChartsInWorkspace'

    Example 6: Using a Workspace to Generate Multiple Chart Images.

    A chart workspace is defined to contain several regular charts. You may want to generate an image file of each chart in the workspace. In the following example, we generate JPEG images. Remember that we also have options to generate .BMP images or .EMF images. EMF is Enhanced Windows Metafile. 

    Sub ChartBitmapsFromWorkspace()

     

    Dim bRtn As Boolean

    Dim lWidth As Long

    Dim lHeight As Long

    Dim sFile As String

    Dim sMsg As String

    Dim sOutputFolder As String

    Dim oChRunApp As New CHARTrunner16.Application

     

       ' Valid Image types: 
       Const eCrImageJpg = 1 ' Jpg image.
       Const eCrImageBmp = 2 ' Bitmap image.
       Const eCrImageEmf = 3 ' Enhanced Windows metafile image.

       sFile = "C:\Program Files\PQ Systems\CHARTrunner 1.6\SampleCharts\Workspace sample.crw"

       sOutputFolder = "C:\Temp"

       lWidth = 640

       lHeight = 480

     

       bRtn = oChRunApp.SaveWorkspaceImages(sFile, eCrImageJpg, lWidth, lHeight, sOutputFolder)

       

       If Not bRtn Then

          MsgBox oChRunApp.sLastError

       End If

     

    End Sub 'ChartBitmapsFromWorkspace'

    In the method SaveWorkspaceImages used above, there are five possible arguments: 

        Filename, ImageType, ImageWidth, ImageHeight, OutputFolder 

    Only the first argument, Filename, is required. All the other arguments are optional. If you do not set them, CHARTrunner will use default values. The following line of code would also have worked in the previous example: 

      bRtn = oChRunApp.SaveWorkspaceImages(sFile) 

    In CHARTrunner, each chart has a property that specifies the default filename to use when saving a chart image. You can see this on the Misc. tab of the chart definition form. If you do not specify the last argument, the folder, when you call SaveWorkspaceImages, each chart will save its image into the file specified on the Misc. tab of the chart definition form. If you do specify a folder and it does not exist on your computer, an error will occur.

    Example 7: Introduction to the PqCrDef Object.

    Up to this point, all of the examples have used only the CHARTrunner16.Application object. This is the most commonly used object in CHARTrunner but there are others. The next example uses the PqCrDef  object. This is a chart definition object. Like all other objects, it has various properties you can set and various methods you can call. Think of a PqCrDef object as an in-memory instance of a complete CHARTrunner chart definition. 

    Example where we use an existing chart but change some of its settings before displaying it. 

    Sub ChangeChartSettings()

     

    Dim bRtn As Boolean

    Dim sFile As String

    Dim sMsg As String

    Dim oChRunApp As New CHARTrunner16.Application

    Dim oCrDef As PQCrDef

     

    Const eObjPQCrDef = 1
    Const eCrPathFull = 1        ' Name represents a full path/file.

       ' Have the CHARTrunner16.Application create an instance of a PqCrDef.

       Set oCrDef = oChRunApp.CreatePqObject(eObjPQCrDef)

       

       sFile = "C:\Program Files\PQ Systems\CHARTrunner 1.6\SampleCharts\Sample 01.crf"

       

       ' Open the chart definition file.

       bRtn = oCrDef.OpenFile(sFile, eCrPathFull)

       If Not bRtn Then

          MsgBox "Error opening file: " & oCrDef.sLastError

          GoTo MyExit

       End If

       

       ' Next, we'll change some of the chart settings; like titles.

       oCrDef.sTopTitle(1, 1) = "Titles placed here via sample code"

       oCrDef.sTopTitle(2, 1) = "Date: " & Date & "  Time: " & Time

       oCrDef.sBottomTitle(1, 3) = "Is this cool,,, or what?"

       

       ' And grid lines.

       oCrDef.ControlChartSettings.bDrawVerticalGrid = True

       oCrDef.ControlChartSettings.bDrawHorizontalGrid = True

      

       ' Set the chart to compute temporary control limits based on all the data.

       oCrDef.eLimitOption = eCrTempLimits

       

       ' Now, let's show the chart.

       bRtn = oCrDef.Display(bShowModal:=False)

       If Not bRtn Then

          MsgBox "Error in Display: " & oCrDef.sLastError

       End If

       

    MyExit:

     

       Exit Sub

       

    End Sub 'ChangeChartSettings'

    In this example, almost all the work used the PqCrDef object. The CHARTrunner16.Application object was used only to create the instance of our PqCrDef object, which we named oCrDef in the example. Notice that the PqCrDef object has a Display method. This is similar to the DisplayChart method of the CHARTrunner16.Application object that we have been using in the previous examples. The PqCrDef object has many properties; we have set only a few of them in this example. For details about the other properties and methods, see the reference section later in this document.

    Example 8: Create a Complete Chart in Code and Display It.

    The previous examples assume that someone has used CHARTrunner to create and save a chart definition file. You may need to create charts on the fly from within other systems you are running. The next example creates and displays a chart using only macro or script code. The chart is never saved to the disk. Rather, it is created and used only in memory. 

    First, we will review basic information about how CHARTrunner gets to data. Every CHARTrunner chart has a data source and a data definition. The data source describes the overall type and location of the data. For example, Microsoft Excel, Access, and SQL Server are all valid data sources. If you are using Excel, something like C:\data\datasheet.xls describes the specific location of the data. The data definition is a more detailed description of the specific data (within the data source) that you want to include in the chart. For example, if your data source is an Access database named C:\mydata.mdb, the data definition could name a specific table within mydata.mdb such as a table named DailyRuns. Now, the DailyRuns table will contain several different columns of data. The data definition also tells CHARTrunner which specific columns you want to include for analysis in this chart. 

    To create a chart from scratch, you will be working with many properties and methods of the PqCrDef object. Some of the important properties you will use and a brief description are listed below.  See the Reference Manual for more details on PQCrDef properties and methods.

    eChartType Describes the type of chart that will be displayed; options include:
     eCrTypeXbarRange, eCrTypeHistogram, eCrTypePareto
    eDataSourceType Describes the type of data source to be used.
    eJetDataType When eDataSourceType specifies a DAO/JET data source (eCrDataSourceTypeJet) then this property specifies what type of DAO/JET data will be accessed.  Valid options include: eCrJetDataAccess, eCrJetDataExcel8, eCrJetDataText, eCrJetDataDBase5, etc.
    sDataSourceFile For file based systems, this is the complete path/file name to the data source.
    For example "C:\MyData\DailyRuns.mdb"
    sTable For database systems, this is the name of the table the data will come from.
    sQueryName If you are using a stored query, view, or procedure as your data source, this is the name.
    eRecSelectType Tells CHARTrunner what type of record source is being used. Valid options include:
    eCrRecSetTable, eCrRecSetNamedQuery, eCrRecSetStoredProc, eCrRecSetCustomQuery
    cColDefs This is a collection of PqCrColDef objects. Each member in this collection describes one of the columns in your data source to CHARTrunner. It tells CHARTrunner that you want to include the column in the chart and how you want to treat the column in terms of the type of data it contains. More on this in the example code shown below.
    eResultRecSelectType Tells CHARTrunner which of the resulting records to include in the chart. Options are: eUseAll or eUseLastN. If eUseLastN, you also set sResultRecSelectN. For example, you might set sResultRecSelectN to 25 to include the last 25 records in the chart.

     

     

    Sub CreateAndDisplayChart()

     

    On Error GoTo ErrorHandler

    Dim bRtn As Boolean
    Dim j As Integer
    Dim sFile As String
    Dim sMsg As String
    Dim sStyleFile As String
    Dim oCrDef As PQCrDef
    Dim oColDef As PQCrColDef
    Dim oChRunApp As New CHARTrunner16.Application

       ' Assume success.
       bRtn = True

       Screen.MousePointer = vbHourglass

       ' Create an instance of a chart definition object.
       Set oCrDef = oChRunApp.CreatePqObject(eObjPqCrDef)

       ' Give the chart a name.
       oCrDef.sChartName = "My Sample Chart"

       ' Set the type of chart we want.
       oCrDef.eChartType = eCrTypeXbarRange

       ' Tell the chart object to use DAO/JET as the data source.
       oCrDef.eDataSourceType = eCrDataSourceTypeJet

       ' Tell the chart object we will be using an Access database.
       oCrDef.eJetDataType = eCrJetDataAccess

       ' Use the sample database installed with CHARTrunner.
       oCrDef.sDataSourceFile = AppFolder$ & "\SampleData\SampleAccess7.mdb"

       ' Tell oCrDef that we will select records from a table.
       oCrDef.eRecSelectType = eCrRecSetTable

       ' Tell it the name of the table.
       oCrDef.sTableName = "GapDimA"

       ' Next, we add a PqCrColDef object to the oCrDef's
       ' cColDefs collection for each column we want to
       ' reference in the chart.

       ' First the Identifier.
       Set oColDef = oChRunApp.CreatePqObject(eObjPQCrColDef)
       oColDef.sName = "Date"
       oColDef.eTreatColAs = eCrColId
       oCrDef.cColDefs.Add oColDef
       Set oColDef = Nothing

       ' Then the 'Measurements'.
       For j = 1 To 3
          Set oColDef = oChRunApp.CreatePqObject(eObjPQCrColDef)
          oColDef.sName = "Gap" & CStr(j)
          oColDef.eTreatColAs = eCrColMeasurement
          oCrDef.cColDefs.Add oColDef
          Set oColDef = Nothing
       Next j

       ' Set the chart to compute temporary limits.
       oCrDef.eLimitOption = eCrTempLimits

       ' Label this x-axis with one of the identifiers.
       oCrDef.ControlChartSettings.eLabelWith = eLineChartLabelTypeID

       ' Use this column for the x-axis identifier label.
       oCrDef.ControlChartSettings.sChartLabelColumn = "Date"

       ' Label every subgroup on the x-axis.
       oCrDef.ControlChartSettings.eHowToLabelXAxis = eLineChartLabelAll

       ' Point to the 'default' style for control charts.
       sStyleFile = oChRunApp.sStyleFolder & "\Default control chart style.cst"

       ' Read the chart style definition into this chart object.
       bRtn = oCrDef.ReadChartStyleFile(sStyleFile, eCrPathFull)

       ' Set some control chart settings.
       oCrDef.ControlChartSettings.bDrawDataLines = True
       oCrDef.ControlChartSettings.bDrawDataPoints = True
       oCrDef.ControlChartSettings.bShowChartTitle = True

       ' Put two titles centered on the chart.
       oCrDef.sTopTitle(1, 2) = "This chart created from SCRATCH in Visual Basic code."
       oCrDef.sBottomTitle(1, 2) = "Date created: @D"

       ' Display the chart.
       bRtn = oCrDef.Display(bShowModal:=True, bReadOnly:=False)
       If Not bRtn Then
          MsgBox oCrDef.sLastError, vbExclamation
       End If

    MyExit:

       Screen.MousePointer = vbNormal
       Exit Sub

    ErrorHandler:

       MsgBox Err.Description & " - " & Err.Number, vbExclamation
       Resume MyExit
      

    End Sub 'CreateAndDisplayChart'

    Example 9: In-Memory Recordset and Chart Statistics.

    This is a VBScript example (Sample9.vbs) that demonstrates using an in-memory ADO recordset to chart data that has been generated or gathered by your program.  The chart definition is built in code.  The chart is either displayed or an image file is generated, then PQCrDef.GetDataObject() is used to get a reference to the measurement data object (oMD) for the chart.  The data object allows access to all the statistics for the chart.

    ' VBScript - Sample9.vbs

    MsgBox "This example builds a chart from scratch using VBScript and a PQCrDef object." _
    & vbCrLf & vbCrLf _
    & "In addition, an in-memory ADO recordset is created as the source of data for" _
    & " the chart, and after the chart is rendered some statistics from the chart are" _
    & " displayed to demonstrate using PQCrDef.GetDataObject() to get the chart's data" _
    & " object."

    Call Process


    ' ===========================================================
    ' Process the chart.
    ' ===========================================================

    Sub Process()

    Const SGNUM = "SubgroupNumber"
    Const MEAS1 = "Measurement1"

    ' Since we are using late binding, we have to define some constants
    ' in place of Enum values we would have gotten with early binding.
    Const adInteger = 3
    Const adSingle = 4
    Const eUseAll = 0
    Const eCrColId = 11
    Const eCrColMeasurement = 14
    Const eCrTypeInd = 8
    Const eCrDataSourceTypeAdo = 3
    Const eObjPQCrDef = 1
    Const eObjPQCrColDef = 3

    Const eCrImageJpg = 1 ' JPEG image.
    Const eCrImageBmp = 2 ' Bitmap image.
    Const eCrImageEmf = 3 ' Windows enhanced metafile.
    Const eCrImagePng = 4 ' Portable network graphic.

    Dim bRtn
    Dim bGenerateImage
    Dim j
    Dim sFileName
    Dim sMsg
    Dim oRs
    Dim oCrApp             ' CHARTrunner16.Application.
    Dim oCrDef             ' PQCrDef.
    Dim oLocColDef         ' PQCrColDef.
    Dim oMD                ' PQDM3.PQMeasurementData

    ' The output image file.
    sFileName = "C:\TestImage.jpg"

    sMsg = "You can either generate the chart image file (" & sFileName _
         & ") or display the chart in a window." _
         & vbCrLf & vbCrLf _
         & "Click Yes to generate the chart image file, No to display the chart, or Cancel"
    j = MsgBox( sMsg, vbYesNoCancel, "What to do?")
    Select Case j
       Case vbYes
          bGenerateImage = True
       Case vbNo
          bGenerateImage = False
       Case Else
          ' Cancel.
          Exit Sub
    End Select

    ' Whether to display the chart.
    bDisplayChart = Not bGenerateImage

    Set oCrApp = CreateObject("CHARTrunner16.Application")
    If oCrApp Is Nothing Then
       MsgBox "Cannot create CHARTrunner16.Application"
       Exit Sub
    End If

    Set oRs = CreateObject("ADODB.Recordset")
    If oRs Is Nothing Then
       MsgBox "Cannot create ADODB.Recordset"
       Exit Sub
    End If

    ' Create a small, simple, in-memory recordset to use for the chart.
    oRs.Fields.Append SGNUM, adInteger
    oRs.Fields.Append MEAS1, adSingle

    ' Open the recordset.
    oRs.Open

    ' Add 10 records with random data for our chart.
    Randomize
    For j = 1 To 10
       oRs.AddNew
       oRs.Fields(SGNUM) = j
       oRs.Fields(MEAS1) = 10 * Rnd(1)
       oRs.Update
    Next

    ' Have the CHARTrunner application object instantiate
    ' a chart definition object for us.
    Set oCrDef = oCrApp.CreatePqObject(eObjPQCrDef)

    ' Bail out if we are not running 1.6.68 or higher which has support
    ' for the oCrDef.bKeepDataObjects property.
    On Error Resume Next
    bRtn = oCrDef.bKeepDataObjects
    If Err.Number <> 0 Then
       MsgBox "CHARTrunner 1.6.68 or higher is required for this demo."
       Exit Sub
    End If
    On Error GoTo 0

    ' Get the chart definition from the default chart.
    Call oCrDef.SetToDefaults(oCrApp.env.sDefaultChartFile)

    ' Specify some chart titles.
    oCrDef.sTopTitle(1, 2) = "Chart built from code using PQCrDef object with an in-memory external recordset"
    oCrDef.sTopTitleFont(1, 2) = "Arial,12,B,C"
    oCrDef.sTopTitle(2, 2) = "~"
    oCrDef.sTopTitle(3, 1) = "Grand Mean=@MEAN"
    oCrDef.sTopTitleFont(3, 1) = "Arial,14,B,L"
    oCrDef.sTopTitle(3, 2) = "Sigma=@SIGMA"
    oCrDef.sTopTitleFont(3, 2) = "Arial,14,B,C"
    oCrDef.sTopTitle(3, 3) = "Kurtosis=@KURT"
    oCrDef.sTopTitleFont(3, 3) = "Arial,14,B,R"
    oCrDef.sTopTitle(4, 2) = "~"
    oCrDef.sBottomTitle(2, 2) = "~"
    oCrDef.sBottomTitle(3, 2) = "http://www.chartrunner.com"
    oCrDef.sBottomTitleFont(3, 2) = "Arial,14,B,C"

    ' Tell the chart how many decimal places to use for chart statistics.
    oCrDef.iControlChartDecimalDigits = 5

    ' Tell the chart definition object to use the in-memory recordset we just created.
    Call oCrDef.UseExternalRecordset(oRs)

    ' Tell the chart definition object we want an Individuals chart.
    oCrDef.eChartType = eCrTypeInd

    ' Tell the chart to use "all" the records.
    oCrDef.eResultRecSelectType = eUseAll

    ' Create "Column Definition" object #1.
    Set oLocColDef = oCrApp.CreatePqObject(eObjPQCrColDef)

    ' Set the ColumnDef properties for the first field in our recordset.
    oLocColDef.eDataSourceType = eCrDataSourceTypeAdo
    oLocColDef.eColType = adInteger
    oLocColDef.sName = SGNUM
    oLocColDef.eTreatColAs = eCrColId
    oLocColDef.bUniqueID = True
    ' Add the ColumnDef to the chart's collection of ColumnDefs
    oCrDef.cColDefs.Add oLocColDef, "1"

    ' Create "Column Definition" object #2.
    Set oLocColDef = oCrApp.CreatePqObject(eObjPQCrColDef)

    ' Set the ColumnDef properties for the second field in our recordset.
    oLocColDef.eDataSourceType = eCrDataSourceTypeAdo
    oLocColDef.eColType = adSingle
    oLocColDef.sName = MEAS1
    oLocColDef.eTreatColAs = eCrColMeasurement
    ' Add the ColumnDef to the chart's collection of ColumnDefs
    oCrDef.cColDefs.Add oLocColDef, "2"

    ' Give the chart a name.
    oCrDef.sChartName = "Sample9.vbs"

    ' Set the default output image filename, so that oCrDef.Validate2() 
    ' does not complain.
    oCrDef.sImageFileName = sFileName

    ' Validate the chart definition using the Validate2() method which is
    ' compatible with VBScript.
    bRtn = oCrDef.Validate2()
    If bRtn = False Then
       ' We have a validation error, so display the error message.
       MsgBox "Validation Error: " & oCrDef.sLastError
       Exit Sub
    End If

    ' Tell the chart to retain the measurement data object after the chart
    ' image file is created. This allows us to use oCrDef.GetDataObject()
    ' later to get a reference to the data object that was used during the
    ' call to oCrDef.GenerateChartImageFile().
    oCrDef.bKeepDataObjects = True

    If bGenerateImage Then
       ' Have the chart form generate a chart image.
       bRtn = oCrDef.GenerateChartImageFile(eCrImageJpg, sFileName, 800, 600)
       If bRtn Then
          MsgBox "Image File Generated = " & sFileName
       Else
          MsgBox oCrDef.sLastError
          Exit Sub
       End If
    End If

    If bDisplayChart Then
       ' Display the chart.
       bRtn = oCrDef.Display( True)
       If bRtn = False Then
          ' We have an error, so display the error message.
          MsgBox oCrDef.sLastError
          Exit Sub
       End If
    End If

    ' Get a reference to the measurement data object for this chart.
    Set oMD = oCrDef.GetDataObject()

    If oMD Is Nothing Then
       MsgBox "GetDataObject() Failed: " & oCrDef.sLastError
       Exit Sub
    End If

    ' Demonstrate some of the properties you can get from the
    ' measurement data object.
    sMsg = "Statistics from the chart's measurement data object:" _
         & vbCrLf & vbCrLf _
         & "Grand Mean = " & oMD.GrandMean & vbCrLf _
         & "Minimum = " & oMD.Minimum & vbCrLf _
         & "Maximum = " & oMD.Maximum & vbCrLf _
         & "Sigma = " & oMD.Sigma & vbCrLf _
         & "Kurtosis = " & oMD.Kurtosis
    MsgBox sMsg

    End Sub 'Process'

    Example: Create a clipCHART From a Chart Definition File.

    This is a VBScript example (clipCHART1.vbs) that uses CHARTrunner16.Application.CreateClipChart to create a clipCHART from an existing chart definition file.  See also the JavaScript example clipCHART1.js.

    Dim oChRunApp
    Dim bRtn
    Dim sClipChartFile
    Dim sMsg

    ' You will need to change the location of the output clipCHART file if
    ' you do not have a C:\Temp folder.
    sClipChartFile = "C:\Temp\clipCHART1.ccf"

    sMsg = "This VBScript example will create the clipCHART file " _
         + sClipChartFile + " using the Application.CreateClipChart method."
    MsgBox sMsg

    Set oChRunApp = CreateObject("CHARTRunner16.Application")

    ' Here is the function prototype for CreateClipChart():
    '
    ' Public Function CreateClipChart(ByVal sCrFile As String, _
    ' ByVal sCcFile As String)) As Boolean
    '
    ' Parameters:
    ' sCrFile is the full path to a CHARTrunner chart definition file.
    '
    ' sCcFile is the full path for the generated clipCHART file.

    bRtn = oChRunApp.CreateClipChart( _
       "C:\Program files\PQ Systems\CHARTrunner 1.6\SampleCharts\Sample 01.crf", _
       sClipChartFile)

    If bRtn = False Then
       ' We have an error, so display the error message.
       MsgBox oChRunApp.sLastError
    Else
       MsgBox "Output clipCHART file = " & sClipChartFile
    End If

    Example: Create a clipCHART From a PQCrDef object.

    Here is a JavaScript example (clipCHART2.js) that uses PQCrDef.CreateClipChart to create a clipCHART from the chart definition contained in a PQCrDef object.  See also the VBScript example clipCHART2.vbs.

    // Declare some variables.
    var bRtn
    var sChartDefFile
    var sClipChartFile
    var sMsg
    var oChRunApp
    var oCrDef

    // You will need to change the location of the output image file if
    // you do not have a C:\Temp folder.
    sClipChartFile = "C:\\Temp\\clipCHART2.ccf";

    sMsg = "This JavaScript example will create the clipCHART file "
         + sClipChartFile + " using the PQCrDef.CreateClipChart method.";
    WScript.Echo(sMsg);

    // Set some of the variables to specific values.
    sFile = "C:\\Program files\\PQ Systems\\CHARTrunner 1.6\\SampleCharts\\Sample 01.crf"

    // Create the ChartRunnerApp object.
    oChRunApp = WScript.CreateObject("CHARTRunner16.Application");

    // Create the Chart Definition object.
    oCrDef = oChRunApp.CreatePQObject(1);

    // Read the Chart Definition file.
    bRtn = oCrDef.ReadChartDefinition(sFile, 1);
    if (bRtn != true)
       WScript.Echo(oCrDef.sLastError);

    // Specify some chart titles.
    oCrDef.sTopTitle(1, 2) = "Example clipCHART Created in JavaScript using PQCrDef.CreateClipChart";
    oCrDef.sTopTitleFont(1, 2) = "Arial,14,B,C";
    oCrDef.sTopTitle(2, 2) = "~";
    oCrDef.sBottomTitle(2, 2) = "~";
    oCrDef.sBottomTitle(3, 2) = "http://www.chartrunner.com";
    oCrDef.sBottomTitleFont(3, 2) = "Arial,14,B,C";

    // Here is the function prototype for CreateClipChart():
    //
    // Public Function CreateClipChart(ByVal sCcFile As String) As Boolean
    //
    // Parameters:
    // sCcFile is the full path for the generated clipCHART file.

    bRtn = oCrDef.CreateClipChart( sClipChartFile);
    if (bRtn != true) 
       WScript.Echo(oCrDef.sLastError);
    else
       WScript.Echo("Output clipCHART file = " + sClipChartFile);