'----- Compiler Directives 
$OPTIMIZE ON
$TYPECHECK ON

'----- Code modules (Change path to point to the correct directory on your system) 
$INCLUDE "QChart.obj"

'----- Form Event SUB Declarations 
DECLARE SUB btnOnClick (SENDER AS QButton)
DECLARE SUB frmMainResize (SENDER AS QForm)

'----- API Constants and Functions 
CONST GWL_HWNDPARENT = (-8)                         'Minimize to task bar 
CONST HWND_DESKTOP = 0
DECLARE FUNCTION SetWindowLong LIB "user32" ALIAS "SetWindowLongA"_
                 (hwnd AS LONG, nIndex AS LONG, dwNewLong AS LONG) AS LONG

'----- Rapid-Q Constants 
CONST alRight = 4
CONST poLandscape = 1

CREATE frmMain AS QForm

  Center
  Width = 640
  Height = 480
  Caption = "QChart Example"
  OnResize = frmMainResize

    CREATE drwHisto1 AS QButton
      Caption = "Histogram"
      OnClick = btnOnClick
    END CREATE  
    
    CREATE drwBar1 AS QButton
      Caption = "Bar Graph 1"
      Top = 25
      OnClick = btnOnClick
    END CREATE  
    
    CREATE drwBar2 AS QButton
      Caption = "Bar Graph 2"
      Top = 50
      OnClick = btnOnClick    
    END CREATE

    CREATE drwBar3 AS QButton
      Caption = "Bar Graph 3"
      Top = 75
      OnClick = btnOnClick    
    END CREATE

    CREATE drwXYpt AS QButton
      Caption = "XY Points"
      Top = 100
      OnClick = btnOnClick    
    END CREATE

    CREATE drwXYln AS QButton
      Caption = "XY Lines"
      Top = 125
      OnClick = btnOnClick    
    END CREATE
    
    CREATE drwXYboth AS QButton
      Caption = "XY Both"
      Top = 150
      OnClick = btnOnClick    
    END CREATE    

    CREATE drwXYlog AS QButton
      Caption = "XY Logs"
      Top = 175
      OnClick = btnOnClick    
    END CREATE    

    CREATE drwLine AS QButton
      Caption = "Line Graph"
      Top = 200
      OnClick = btnOnClick    
    END CREATE

    CREATE drwLinePts AS QButton
      Caption = "Line w/Points"
      Top = 225
      OnClick = btnOnClick    
    END CREATE

    CREATE drwBox AS QButton
      Caption = "Box Plot"
      Top = 250
      OnClick = btnOnClick    
    END CREATE

    CREATE drwHiLo1 AS QButton
      Caption = "HLC Style 1"
      Top = 275
      OnClick = btnOnClick    
    END CREATE

    CREATE drwHiLo2 AS QButton
      Caption = "HLC Style 2"
      Top = 300
      OnClick = btnOnClick    
    END CREATE

    CREATE btnRedraw AS QButton
      Caption = "Redraw"
      Top = 325
      OnClick = btnOnClick
    END CREATE  

    CREATE btnClear AS QButton
      Caption = "Clear"
      Top = 350
      OnClick = btnOnClick
    END CREATE  

    CREATE btnSave AS QButton
      Caption = "Save"
      Top = 375
      OnClick = btnOnClick
    END CREATE  

    CREATE btnPrint AS QButton
      Caption = "Print"
      Top = 400
      OnClick = btnOnClick
    END CREATE  

    CREATE btnExit AS QButton
      Caption = "Exit"
      Top = 425
      OnClick = btnOnClick
    END CREATE  
    
    CREATE Graph AS QChart                              'Create a copy of the new object 
      Align = alRight
      Width = frmMain.ClientWidth - btnClear.Width
      Height = frmMain.ClientHeight          
      Image.OnPaint = Graph.ImagePaint                  'This line REQUIRED to process Repaints 
    END CREATE'Graph 
    
END CREATE'frmMain   

'API Call that lets RapidQ programs minimize to the task bar 
setwindowlong(frmMain.handle, GWL_HWNDPARENT, HWND_DESKTOP)
setwindowlong(application.handle, GWL_HWNDPARENT, frmMain.handle)

WITH Graph
  .Initialize                                           'Set default values 
  .ClearBuffer                                          'Start with a clean slate 
  .ImagePaint                                           'Paint the blank Canvas 
END WITH  
frmMain.ShowModal

'----- Event SUBs 

SUB btnOnClick (SENDER AS QButton)

  DIM i  AS INTEGER                                     'Loop counters... 
  DIM j  AS INTEGER
  DIM k  AS SINGLE                                      'A Scratch Variable 
  
  SELECT CASE Sender.Caption

    CASE "Histogram"
      WITH Graph
        .Initialize                                     'Set defaults 
        .MainTitle.Text = "Histogram Example"           'Change desired options 
        .SubTitle.Text = "No Grid, No Border"
        .XTitle.Text = "X Axis"      
        .YTitle.Text = "Y Axis"
        .DoLegend = False        
        .AxisBorder = False
        .XAxis.Grid = False
        .YAxis.Grid = False
        .Data.ColCount = 1                              'Set .Data Grid dimensions     
        .Data.RowCount = 10
        j = 25                                          'Load the data                            
        RANDOMIZE
        FOR i = 1 to .Data.RowCount
          .Data.Cell(0,i) = "Class " + STR$(i)
          .Data.Cell(1,i) = STR$(RND * j)
        NEXT i
        .ChartType = ctBar                              'Bar Chart 
        .ChartStyle = csHisto                           'Histogram         
        .DrawBar (False)                                'Draw it! 
      END WITH

    CASE "Bar Graph 1"
      WITH Graph
        .Initialize                                     'Set defaults 
        .MainTitle.Text = "Bar Graph Example 1"         'change desired options 
        .SubTitle.Text = "Positive Values"        
        .XTitle.Text = "X Axis"      
        .YTitle.Text = "Y Axis"                                  
        .XAxis.Grid = False
        .Data.ColCount = 5                              'Set .Data Grid dimensions                          
        .Data.RowCount = 2
        k = 25                                          'Load the data                             
        RANDOMIZE
        FOR i = 1 to .Data.ColCount
          .Data.Cell(i , 0) = STR$(i)                   'Legend in Row 0 of each Col         
          FOR j = 1 TO .Data.RowCount
            .Data.Cell(i,j) = STR$(RND * k)
          NEXT j
        NEXT i
        FOR j = 1 TO .Data.RowCount
          .Data.Cell(0,j) = "Treatment " + STR$(j)      'X Labels in Col 0 of each Row 
        NEXT j
        .ChartType = ctBar                              'Bar Chart 
        .ChartStyle = csBar                             'Grouped Bars         
        .DrawBar (False)                                'Draw it! 
      END WITH

    CASE "Bar Graph 2"
      WITH Graph
        .Initialize                                     'Set defaults 
        .MainTitle.Text = "Bar Graph Example 2"         'change desired options 
        .SubTitle.Text = "Negative Values"
        .XTitle.Text = "X Axis"      
        .YTitle.Text = "Y Axis"
        .Data.ColCount = 8                              'Set .Data Grid dimensions     
        .Data.RowCount = 1
        k = 25                                          'Load the data 
        RANDOMIZE
        FOR i = 1 to .Data.ColCount
          .Data.Cell(i , 0) = "Group " + STR$(i)        'Legend in Row 0 of each Col         
          FOR j = 1 TO .Data.RowCount
            .Data.Cell(i,j) = STR$((RND) * -k)
          NEXT j
        NEXT i
        FOR j = 1 TO .Data.RowCount
          .Data.Cell(0,j) = "Level " + STR$(j)          'X Labels in Col 0 of each Row 
        NEXT j
        .ChartType = ctBar                              'Bar Chart 
        .ChartStyle = csBar                             'Grouped Bars          
        .DrawBar (False)                                'Draw it! 
      END WITH

    CASE "Bar Graph 3"
      WITH Graph
        .Initialize                                     'Set defaults 
        .MainTitle.Text = "Bar Graph Example 3"         'change desired options 
        .SubTitle.Text = "Positive and Negative Values"
        .XTitle.Text = "X Axis"      
        .YTitle.Text = "Y Axis"
        .Data.ColCount = 4                              'Set .Data Grid dimensions     
        .Data.RowCount = 3
        k = 20                                          'Load the data 
        RANDOMIZE
        FOR i = 1 to .Data.ColCount
          .Data.Cell(i , 0) = "Long Legend Title " + STR$(i)'Legend in Row 0 of each Col         
          FOR j = 1 TO .Data.RowCount
            .Data.Cell(i,j) = STR$((RND - RND) * k)
          NEXT j
        NEXT i
        FOR j = 1 TO .Data.RowCount
          .Data.Cell(0,j) = "Level " + STR$(j)          'X Labels in Col 0 of each Row 
        NEXT j
        .ChartType = ctBar                              'Bar Chart 
        .ChartStyle = csBar                             'Grouped Bars          
        .DrawBar (False)                                'Draw it! 
      END WITH
     
    CASE "XY Points"
      WITH Graph
        .Initialize                                     'Set defaults 
        .MainTitle.Text = "Scatter Plot Example"        'change desired options 
        .SubTitle.Text = "Data in All Four Quadrants"
        .XTitle.Text = "X Axis"      
        .YTitle.Text = "Y Axis"
        .Data.ColCount = 16                             'Set .Data Grid dimensions                
        .Data.RowCount = 5
        k = .00100                                    '<-- change k for large and small values 
        RANDOMIZE                                       'Load the data 
        FOR i = 2 to .Data.ColCount STEP 2              'for each col pair 
          .Data.Cell(i , 0) = "Group " + STR$(i \ 2)    'Legend in 2nd Col (Y) of Pair, Row 0 
          FOR j = 1 to .Data.RowCount                   'add data to each row 
            .Data.Cell(i - 1 , j) = STR$(k * (RND - RND))'X Values in Col 1 
            .Data.Cell(i , j) = STR$(k * (RND - RND))   'Y Values in Col 2 
          NEXT j  
        NEXT i
        .ChartType = ctXY                               'XY Scatter Chart 
        .ChartStyle = csPointsOnly                      'Just the Points 
        .DrawXY (False)                                 'Draw it! 
      END WITH

    CASE "XY Lines"
      WITH Graph
        .Initialize                                     'Set defaults 
        .MainTitle.Text = "Two Sine Curves"             'change desired options       
        .SubTitle.Text = "Out of Phase"
        .XTitle.Text = "Radians"      
        .YTitle.Text = "Sine of X"
        .BW = True                                   
        .YAxis.Div = 6
        .XAxis.AutoScale = False                        'Manually Scale 
          .XAxis.Max = 2                                'Set Max Manually 
          .XAxis.Min = 0                                'Set Min Manually           
        .YAxis.AutoScale = False                        'Manually Scale 
          .YAxis.Max = 1.5                              'Set Max Manually 
          .YAxis.Min = -1.5                             'Set Min Manually 
        .Data.ColCount = 4                              'Set .Data Grid dimensions                
        .Data.RowCount = 21                             'Load the data 
        FOR i = 2 to .Data.ColCount STEP 2              'for each col pair 
          .Data.Cell(i , 0) = "Group " + STR$(i \ 2)    'Legend in 2nd Col of Pair, Row 0 
          .Series(i\2).LineStyle = (i\2-1) MOD 5        'Cycle line styles 
          FOR j = 1 to .Data.RowCount                   'add data to each row 
            .Data.Cell(i - 1 , j) = STR$((j-1)/10)      'X Values in 1st Col of Pair 
             k = ((j-1)/10 + [(i-2) * 4.75])* 3.141592
            .Data.Cell(i , j) = STR$(SIN(k))            'Y Values in 2nd Col of Pair 
          NEXT j  
        NEXT i
        .ChartType = ctXY                               'XY Scatter Chart 
        .ChartStyle = csLinesOnly                       'Just the Lines         
        .DrawXY (False)                                 'Draw it! 
      END WITH
      
    CASE "XY Both"
      WITH Graph
        .Initialize                                     'Set defaults 
        .MainTitle.Text = "Scatter Plot With Lines"     'change desired options 
        .SubTitle.Text = "All options set to default"
        .XTitle.Text = "X Axis"      
        .YTitle.Text = "Y Axis"        
        .Data.ColCount = 4                              'Set .Data Grid dimensions                
        .Data.RowCount = 11      
        k = 100                                         'Load the data 
        RANDOMIZE
        FOR i = 2 to .Data.ColCount STEP 2              'for each col pair 
          .Data.Cell(i , 0) = "Group " + STR$(i \ 2)    'Legend in 2nd Col of Pair, Row 0 
          FOR j = 1 to .Data.RowCount                   'add data to each row 
            .Data.Cell(i - 1 , j) = STR$(j)             'X Values in 1st Col of Pair 
            .Data.Cell(i , j) = STR$(RND * k)           'Y Values in 2nd Col of Pair 
          NEXT j  
        NEXT i
'        .Data.Cell(1,4) = ""                            'Uncomment to see how QChart 
'        .Data.Cell(4,8) = ""                            'handles missing data                 
        .ChartType = ctXY                               'XY Scatter Chart 
        .ChartStyle = csBoth                            'Lines and Points 
        .DrawXY (True)                                  'Draw it as an Overlay 

      END WITH

    CASE "XY Logs"
      WITH Graph
        .Initialize                                     'Set defaults 
        .MainTitle.Text = "Log - Log Scaling"           'change desired options 
        .SubTitle.Text = "Grid with no Tics"
        .XTitle.Text = "X Axis"      
        .YTitle.Text = "Y Axis" 
        .YAxis.Tics = False
        .YAxis.LogScale = True
        .XAxis.Tics = False
        .XAxis.LogScale = True 
        .DoLegend = False                       
        .Data.ColCount = 2                              'Set .Data Grid dimensions                
        .Data.RowCount = 10
        RANDOMIZE                                       'Load the data 
        FOR i = 2 to .Data.ColCount STEP 2              'for each col pair 
          .Data.Cell(i , 0) = "Group " + STR$(i \ 2)    'Legend in 2nd Col of Pair, Row 0 
          FOR j = 1 to .Data.RowCount                   'add data to each row 
            .Data.Cell(i - 1 , j) = STR$(j)             'X Values in 1st Col of Pair 
            .Data.Cell(i , j) = STR$(RND * 10^ (j/5))   'Y Values in 2nd Col of Pair 
          NEXT j  
        NEXT i
        .ChartType = ctXY                               'XY Scatter Chart 
        .ChartStyle = csBoth                            'Points and Lines 
        .DrawXY (True)                                  'Draw it! 
      END WITH

    CASE "Line Graph"
      WITH Graph
        .Initialize                                     'Set defaults 
        .MainTitle.Text = "Simple Line Graph"           'change desired options 
        .SubTitle.Text = "X Labels Alternate Up and Down for Room"
        .XTitle.Text = "X Axis"      
        .YTitle.Text = "Y Axis"
        .Data.ColCount = 5                              'Set .Data Grid dimensions                
        .Data.RowCount = 12
        k = 18                                          'Load the data 
        RANDOMIZE
        FOR i = 1 to .Data.ColCount                     'for each col 
          .Series(i).LineStyle = (i-1) MOD 5            'Cycle line styles           
          .Data.Cell(i , 0) = "Group " + STR$(i)        'Legend in Row 0 of Each Col 
          FOR j = 1 to .Data.RowCount                   'add data to each row 
            .Data.Cell(0 , j) = "Label " + STR$(j)      'X Labels 
            .Data.Cell(i , j) = STR$(RND * k)           'Y Values 
          NEXT j  
        NEXT i
        .ChartType = ctLine                             'Line Chart 
        .ChartStyle = csLinesOnly                       'Lines only         
        .DrawLine (False)                               'Draw it! 
      END WITH    
    
    CASE "Line w/Points"
      WITH Graph
        .Initialize                                     'Set defaults 
        .MainTitle.Text = "Line Graph With Points"      'change desired options 
        .SubTitle.Text = "Tics Only, No Grid"
        .XTitle.Text = "X Axis"      
        .YTitle.Text = "Y Axis"
        .BW = True
        .XAxis.Grid = False                             'Tics only 
        .YAxis.Grid = False
        .Data.ColCount = 4                              'Set .Data Grid dimensions                
        .Data.RowCount = 6
        k = 18                                          'Load the data 
        RANDOMIZE
        FOR i = 1 to .Data.ColCount                     'for each col 
          .Data.Cell(i , 0) = "Group " + STR$(i)        'Legend in Row 0 of Each Col 
          FOR j = 1 to .Data.RowCount                   'add data to each row 
            .Data.Cell(0 , j) = "Lbl " + STR$(j)        'X Labels 
            .Data.Cell(i , j) = STR$(RND * k)           'Y Values 
          NEXT j  
        NEXT i
'        .Data.Cell(1,4) = ""                            'Uncomment to see QChart handle missing data 
        .ChartType = ctLine                             'Line Chart 
        .ChartStyle = csBoth                            'Points and Lines         
        .DrawLine (False)                               'Draw it! 
      END WITH 

    CASE "Box Plot"
      WITH Graph
        .Initialize                                     'Set defaults 
        .MainTitle.Text = "Box and Whisker Plot"        'change desired options 
        .XTitle.Text = "X Axis"      
        .YTitle.Text = "Y Axis"
        .XAxis.Grid = False
        .XAxis.Tics = False
        .YAxis.AutoScale = False
          .YAxis.Min = 0
          .YAxis.Max = 50
        .Data.ColCount = 16                             'Set .Data Grid dimensions                             
        .Data.RowCount = 5
        k = 10                                          'Load the data 
        RANDOMIZE
        FOR i = 1 to .Data.ColCount                     'for each col 
          .Data.Cell(i , 0) = "Group " + STR$(i)        'Legend in Row 0 of Each Col 
          .Data.Cell(i , 1) = STR$(RND * k + 40)        'Max Values 
          .Data.Cell(i , 2) = STR$(RND * k + 30)        'Q3 Values 
          .Data.Cell(i , 3) = STR$(RND * k + 20)        'Q2 Values 
          .Data.Cell(i , 4) = STR$(RND * k + 10)        'Q1 Values 
          .Data.Cell(i , 5) = STR$(RND * k)             'Min Values                                                 
        NEXT i
        .ChartType = ctBox                              'Box and Whisker Plot 
        .ChartStyle = csWhisker        
        .DrawBox (False)                                'Draw it! 
      END WITH  
        
    CASE "HLC Style 1"
      'NOTE: If values for .Data.Cells 2, 3 and 4 are set equal,  
      'Box routine will draw a Hi-Lo-Close antenna style graph 
      WITH Graph
        .Initialize                                     'Set defaults 
        .MainTitle.Text = "Hi-Lo-Close Plot"            'change desired options 
        .SubTitle.Text = "Antenna Style"
        .XTitle.Text = "X Axis"      
        .YTitle.Text = "Y Axis"
        .XAxis.Grid = False
        .XAxis.Tics = False
        .Data.ColCount = 8                             'Set .Data Grid dimensions                             
        .Data.RowCount = 5
        k = 10                                          'Load the data 
        RANDOMIZE
        FOR i = 1 to .Data.ColCount                     'for each col 
          .Data.Cell(i , 0) = "Group " + STR$(i)        'Legend in Row 0 of Each Col 
          .Data.Cell(i , 1) = STR$(RND * k + 50)        'Hi Value 
          .Data.Cell(i , 2) = STR$(RND * k + 37)        '<-- Set all Three to  
          .Data.Cell(i , 3) = .Data.Cell(i , 2)         '<-- the mid or "Close" 
          .Data.Cell(i , 4) = .Data.Cell(i , 2)         '<-- Value 
          .Data.Cell(i , 5) = STR$(RND * k + 25)        'Lo Value 
        NEXT i
        .ChartType = ctBox                              'Hi-Lo-Close Plot 
        .ChartStyle = csHiLo        
        .DrawBox (False)                                'Draw it!     
      END WITH  

    CASE "HLC Style 2"
      'NOTE: If values for .Data.Cells 1 & 2 and 4 & 5 are set equal,  
      'Box routine will draw a Hi-Lo-Close box style graph 
      WITH Graph
        .Initialize                                     'Set defaults 
        .MainTitle.Text = "Hi-Lo-Close Plot"            'change desired options 
        .SubTitle.Text = "Box Style"        
        .XTitle.Text = "X Axis"      
        .YTitle.Text = "Y Axis"
        .XAxis.Grid = False
        .XAxis.Tics = False
        .Data.ColCount = 8                              'Set .Data Grid dimensions                             
        .Data.RowCount = 5
        k = 10                                          'Load the data 
        RANDOMIZE
        FOR i = 1 to .Data.ColCount                     'for each col 
          .Data.Cell(i , 0) = "Group " + STR$(i)        'Legend in Row 0 of Each Col 
          .Data.Cell(i , 1) = STR$(RND * k + 50)        'Hi Value 
          .Data.Cell(i , 2) = .Data.Cell(i , 1)         '<-- Set = to Hi Value  
          .Data.Cell(i , 3) = STR$(RND * k + 35)        'Close Value 
          .Data.Cell(i , 4) = STR$(RND * k + 20)        'Lo Value 
          .Data.Cell(i , 5) = .Data.Cell(i , 4)         '<-- Set = Lo Value 
        NEXT i
        .ChartType = ctBox                              'Hi-Lo-Close Plot 
        .ChartStyle = csWhisker                         'Whiskers are hidden :) 
        .DrawBox (False)                                'Draw it!     
      END WITH  
      
    CASE "Redraw"
      WITH Graph 
        .MainFont.Name =  "Times New Roman"              'Change some options  
        .SubFont.Name =   "Times New Roman"              'on the currently defined chart 
        .PlotAreaColr = .Colors(15)             
        SELECT CASE .SubFont.Size
          CASE .MainFont.Size
            .SubFont.Size = 12
          CASE ELSE
            .SubFont.Size = .MainFont.Size
        END SELECT
        SELECT CASE .DoLegend
         CASE TRUE
           .DoLegend = False
         CASE FALSE
           .DoLegend = True        
       END SELECT
       SELECT CASE .GreyScale
         CASE TRUE
           .GreyScale = False
         CASE FALSE
           .GreyScale = True        
       END SELECT
       .RedrawChart                                     'and redraw the chart 
     END WITH
      
    CASE "Clear"
      Graph.ClearBuffer                                 'Start with a clean slate 
      Graph.ImagePaint                                  'Paint the blank Canvas 
      
    CASE "Save"
      Graph.SaveChart ("")

    CASE "Print"
      Graph.PrintChart (Printer.PrinterIndex, poLandscape, 40, 1)

    CASE "Exit"
      Application.Terminate  
      
  END SELECT

END SUB  
'------------------------------------------------------------------------------------------- 

SUB frmMainResize (SENDER AS QForm)
  
  WITH Graph
    .Width = frmMain.ClientWidth - btnClear.Width       'Calculate new sizes 
    .Height = frmMain.ClientHeight          
    .RedrawChart                                        'Redraw the QChart Object 
  END WITH  
  'Plus anything else that needs doing in your form resize 
  
END SUB