'----- Compiler Directives
$OPTIMIZE ON
$TYPECHECK ON
$OPTION Icon "c:\rapidq\icon\Graph.ico"

'----- Debug Stuff
'$INCLUDE "QDebug.inc"

'----- Code modules (Change path to point to the correct directory on your system)
$INCLUDE "Common.inc"
$INCLUDE "c:\RapidQ\src\QChart\QChart.inc"

'----- Form Event SUB Declarations
Declare Sub btnOnClick (SENDER As QBUTTON)
Declare Sub frmMainResize (SENDER As QFORM)
Declare Sub frmMainClose (SENDER As QFORM)

'----- Define the main form
Create frmMain As QFORM

  Center
  Width = 700
  Height = 527
  Caption = "QChart Example"
  OnResize = frmMainResize
  OnClose = frmMainClose

    Create drwHisto1 As QBUTTON
      Caption = "Histogram"
      OnClick = btnOnClick
    End Create

    Create drwBar1 As QBUTTON
      Caption = "Bar Graph"
      Top = 25
      OnClick = btnOnClick
    End Create

    Create drwBar2 As QBUTTON
      Caption = "Stacked Bar"
      Top = 50
      OnClick = btnOnClick
    End Create

    Create drwBar3 As QBUTTON
      Caption = "%Stacked Bar"
      Top = 75
      OnClick = btnOnClick
    End Create

    Create btnPie As QBUTTON
      Caption = "Pie Chart"
      Top = 100
      OnClick = btnOnClick
    End Create

    Create drwXYpt As QBUTTON
      Caption = "XY Points"
      Top = 125
      OnClick = btnOnClick
    End Create

    Create drwXYln As QBUTTON
      Caption = "XY Lines"
      Top = 150
      OnClick = btnOnClick
    End Create

    Create drwXYboth As QBUTTON
      Caption = "XY Both"
      Top = 175
      OnClick = btnOnClick
    End Create

    Create drwXYlog As QBUTTON
      Caption = "XY Logs"
      Top = 200
      OnClick = btnOnClick
    End Create

    Create drwLine As QBUTTON
      Caption = "Line Graph"
      Top = 225
      OnClick = btnOnClick
    End Create

    Create drwLinePts As QBUTTON
      Caption = "Line w/Points"
      Top = 250
      OnClick = btnOnClick
    End Create

    Create drwBox As QBUTTON
      Caption = "Box Plot"
      Top = 275
      OnClick = btnOnClick
    End Create

    Create drwHiLo1 As QBUTTON
      Caption = "HLC Style 1"
      Top = 300
      OnClick = btnOnClick
    End Create

    Create drwHiLo2 As QBUTTON
      Caption = "HLC Style 2"
      Top = 325
      OnClick = btnOnClick
    End Create

    Create btnRedraw As QBUTTON
      Caption = "Redraw"
      Top = 350
      OnClick = btnOnClick
    End Create

    Create btnClear As QBUTTON
      Caption = "Default"
      Top = 375
      OnClick = btnOnClick
    End Create

    Create btnCopy As QBUTTON
      Caption = "Copy"
      Top = 400
      OnClick = btnOnClick
    End Create

    Create btnSave As QBUTTON
      Caption = "Save"
      Top = 425
      OnClick = btnOnClick
    End Create

    Create btnPrint As QBUTTON
      Caption = "Print"
      Top = 450
      OnClick = btnOnClick
    End Create

    Create btnExit As QBUTTON
      Caption = "Exit"
      Top = 475
      OnClick = btnOnClick
    End Create
    Create Graph As QCHART                        'Create a copy of the new object
      Align = 4                                   'alRight
      Width = frmMain.ClientWidth - btnExit.Width
      Height = frmMain.ClientHeight
      OnPaint = Graph.PaintChart                  'This line REQUIRED to process Repaints
    End Create'Graph

End Create'frmMain

MinSet(frmMain.Handle, Application.Handle) 'Allow Minimize to taskbar (in Common.Inc)

Graph.ClearAll
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
  Static ClickedBar As Integer                          'Keep track of button clicks
  Static ClickedXY  As Integer
  Static ClickedBox As Integer
  Static ClickedLine As Integer

  Select Case Sender.Caption

    Case "Histogram"
      ClickedBar = ClickedBar + 1                       'Adding new series to existing chart
      If ClickedBar > 3 Then ClickedBar = 1             'one by one
      With Graph
        .Initialize                                     'Set defaults
        .ChartType = ctBar                              'Bar Chart
        .ChartStyle = csHisto                           'Histogram
        .MainTitle.Text = "Histogram Example"           'Change desired options
        .SubTitle.Text = "No Axis Border, Add series with each click"
        .XTitle.Text = "Class Limits"
        .YTitle.Text = "Number"
        .AxisBorder = FALSE
        .XAxis.Grid = FALSE
        .Cols = ClickedBar                              'Set data array dims
        .Rows = 12                                      'Redim the data array
        REDIM Graph.Data(.Cols, .Rows)'<--MUST explicitly reference Graph.Data even inside WITH
        Randomize                                       'Load the data
        For j = 0 To .Rows                              'X Axis Labels
          .LabelList.AddItems Str$(j * 3+3)
        Next
        k = 10                                          'Scale factor for random numbers
        For i = 1 To .Cols
          .LegendList.AddItems "Series " + Str$(i)      'Legend Text
          For j = 1 To .Rows
            .Data(i,j) = Rnd * k                        'Data to be plotted
          Next j
        Next i
        .DrawChart (FALSE)                              'Draw it!
      End With

    Case "Bar Graph"
      ClickedBar = ClickedBar + 1                       'Adding new series to existing chart
      If ClickedBar > 4 Then ClickedBar = 1             'one by one
      With Graph
        .Initialize                                     'Set defaults
        .ChartType = ctBar                              'Bar Chart
        .ChartStyle = csBar                             'Grouped Bars
        .MainTitle.Text = "Bar Graph Example 1"         'change desired options
        .SubTitle.Text = "Standard Bar Graph"
        .XTitle.Text = "X Axis"
        .YTitle.Text = "Y Axis"
'        .BW = TRUE
        If ClickedBar = 2 Then .YAxis.LogScale = TRUE
        .Cols = 6
        .Rows = 3
        REDIM Graph.Data(.Cols,.Rows)
        k = 75                                          'Load the data
        Randomize
        For j = 1 To .Rows
          .LabelList.AddItems "Treatment " + Str$(j)
        Next j
        For i = 1 To .Cols
          .LegendList.AddItems "Grp " + Str$(i)
'          .Series(i).HatchStyle = (i-1) MOD 5           'Hatch Patterns
'          .Series(i).AutoColor = False
'          .Series(i).Color = .Colors(i + 4*(ClickedBar - 1))
          For j = 1 To .Rows
            Select Case ClickedBar
              Case 1,2
                .Data(i,j) = Rnd * k
              Case 3
                .Data(i,j) = Rnd * -k
              Case 4
                .Data(i,j) = (Rnd-Rnd) * k
              End Select
          Next j
        Next i
'        .Data(INT(RND*.Cols + 1),INT(RND*.Rows + 1)) = .Missing 'Missing Data
        .DrawChart (FALSE)                              'Draw it!
      End With

    Case "Stacked Bar"
      With Graph
        .Initialize                                     'Set defaults
        .ChartType = ctBar                              'Bar Chart
        .ChartStyle = csStacked                         'Grouped Bars
        .MainTitle.Text = "Bar Graph Example 2"         'change desired options
        .SubTitle.Text = "Stacked Bar Graph"
        .XTitle.Text = "X Axis"
        .YTitle.Text = "Y Axis"
        .Cols = 6
        .Rows = 3
        REDIM Graph.Data(.Cols, .Rows)
        k = 1000                                        'Load the data
        Randomize
        For j = 1 To .Rows
          .LabelList.AddItems "Group " + Str$(j)
        Next j
        For i = 1 To .Cols
          .LegendList.AddItems "Treatment " + Str$(i)
          For j = 1 To .Rows
            .Data(i,j) = Str$(Rnd * k + 1)
          Next j
        Next i
'        .Data(INT(RND*.Cols + 1),INT(RND*.Rows + 1)) = .Missing 'Missing Data
        .DrawChart (FALSE)                              'Draw it!
      End With

    Case "%Stacked Bar"
      With Graph
        .Initialize                                     'Set defaults
        .ChartType = ctBar                              'Bar Chart
        .ChartStyle = csPctStacked                      'Grouped Bars
        .MainTitle.Text = "Bar Graph Example 3"         'change desired options
        .SubTitle.Text = "Percent Stacked Bar Graph"
        .XTitle.Text = "X Axis"
        .YTitle.Text = "Y Axis"
        .Cols = 6
        .Rows = 2
        REDIM Graph.Data(.Cols, .Rows)
        k = 25                                          'Load the data
        Randomize
        For j = 1 To .Rows
          .LabelList.AddItems "Level " + Str$(j)
        Next j
        For i = 1 To .Cols
          .LegendList.AddItems "Long Legend Title " + Str$(i)
          .Series(i).AutoColor = FALSE
          .Series(i).Color = .Colors(i + 12)
          For j = 1 To .Rows
            .Data(i,j) = Str$(Rnd * k)
          Next j
        Next i
'        .Data(INT(RND*.Cols + 1),INT(RND*.Rows + 1)) = .Missing 'Missing Data
        .DrawChart (FALSE)                              'Draw it!
      End With

    Case "Pie Chart"
      With Graph
        .Initialize                                     'Always call this first
        .ChartType = ctPie
        .ChartStyle = csPiePct                          'Show % at wedges (csPieVal shows Values)
        .AxisFont.Size = 12
        .LegendFont.Size = 12
        .MainTitle.Text = "Pie Chart"
        .SubTitle.Text = "Click Again For New Data"
        .Cols = 1                                       'Only 1 col for pie data
        .Rows = 6
        REDIM Graph.Data(.Cols,.Rows)
        k = 100                                         'Load the data
        Randomize
        For i = 1 To .Rows
          .Series(i).AutoColor = FALSE
          .Series(i).Color = .Colors(i + 12)
'          .Series(i).HatchStyle = (i -1) MOD 5
          .LegendList.AddItems "Group " + Str$(i)
          .Data(1,i) = Rnd * k ' * i
        Next
'        .Data(1,INT(RND*.Rows + 1)) = .Missing 'Missing Data
        .DrawChart(FALSE)                               'Always call this last
      End With

    Case "XY Points"
      With Graph
        .Initialize                                     'Set defaults
        .ChartType = ctXY                               'XY Scatter Chart
        .ChartStyle = csPoints                          'Just the Points
        .MainTitle.Text = "Scatter Plot Example"        'change desired options
        .SubTitle.Text = "Data in All Four Quadrants"
        .XTitle.Text = "X Axis"
        .YTitle.Text = "Y Axis"
        .Cols = 8                                       'Number of series
        .Rows = 5                                       'Number of points in each series
        REDIM Graph.XYData(.Cols,.Rows,2)               '3rd Dim 1=Xval, 2=Yval
        k =  75                                         '<-- change k for large and small values
        Randomize                                       'Load the data
        For i = 1 to .Cols
          .LegendList.AddItems "Group " + Str$(i)
          For j = 1 to .Rows
            .XYData(i,j,1) = k * (Rnd - Rnd)            'X values
            .XYData(i,j,2) = k * (Rnd - Rnd)            'Y values
          Next
        Next
        .DrawChart (FALSE)                              'Draw it!
      End With

    Case "XY Lines"
      With Graph
        .Initialize                                     'Set defaults
        .ChartType = ctXY                               'XY Scatter Chart
        .ChartStyle = csLines                           'Just the Lines
        .MainTitle.Text = "Two Sine Curves"             'change desired options
        .SubTitle.Text = "Out of Phase"
        .XTitle.Text = "Radians"
'        .YTitle.Text = "Sine of X"
        .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
        .Cols = 2
        .Rows = 21
        REDIM Graph.XYData(.Cols,.Rows,2)
        For i = 1 To .Cols
          .LegendList.AddItems "Group " + Str$(i)
          .Series(i).LineWidth = 4
          For j = 1 To .Rows
            .XYData(i,j,1) = (j-1)/10                   'X Values
             k = ((j-1)/10 + [(i-1) * 9.5])* 3.141593
            .XYData(i,j,2) = Sin(k)                     'Y Values
          Next j
        Next i
'        .XYData(INT(RND*.Cols + 1),INT(RND*.Rows + 1),1) = .Missing 'Missing Data
        .DrawChart (FALSE)                              'Draw it!
      End With

    Case "XY Both"
      ClickedXY = ClickedXY + 1                         'Adding new series to existing chart
      If ClickedXY > 3 Then ClickedXY = 1               'one by one
      With Graph
        .Initialize                                     'Set defaults
        .ChartType = ctXY                               'XY Scatter Chart
        .ChartStyle = csBoth                            'Lines and Points
        .MainTitle.Text = "Scatter Plot With Lines"     'change desired options
        .SubTitle.Text = "Double Width Lines"
        .XTitle.Text = "X Axis"
        .YTitle.Text = "Y Axis"
        .MarkerSize = .MarkerSize * 1.5
        .Cols = ClickedXY
        .Rows = 11
        REDIM Graph.XYData(.Cols,.Rows,2)
        k = 132                                         'Load the data
        Randomize
        For i = 1 To .Cols
          .LegendList.AddItems "Group " + Str$(i)
          .Series(i).LineWidth = 2
          For j = 1 To .Rows
            .XYData(i,j,1) = Str$(j)                    'X Values
            .XYData(i,j,2) = Str$(Rnd * k)              'Y Values
          Next j
        Next i
'        .XYData(INT(RND*.Cols + 1),INT(RND*.Rows + 1),1) = .Missing 'Missing Data
        .DrawChart (FALSE)                              'Draw it!
      End With

    Case "XY Logs"
      With Graph
        .Initialize                                     'Set defaults
        .ChartType = ctXY                               'XY Scatter Chart
        .ChartStyle = csBoth                            'Points and Lines
        .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
        .Cols = 1
        .Rows = 10
        REDIM Graph.XYData(.Cols,.Rows,2)
        Randomize                                       'Load the data
        For i = 1 to .Cols
          .LegendList.AddItems "Group " + Str$(i)
          For j = 1 To .Rows
            .XYData(i,j,1) = j                          'X Values
            .XYData(i,j,2) = Rnd * 10^ (j/5)            'Y Values
          Next j
        Next i
'        .XYData(INT(RND*.Cols + 1),INT(RND*.Rows + 1),1) = .Missing 'Missing Data
        .DrawChart (TRUE)                               'Draw it!
      End With

    Case "Line Graph"
      With Graph
        .Initialize                                     'Set defaults
        .ChartType = ctLine                             'Line Chart
        .ChartStyle = csLines                           'Lines only
        .MainTitle.Text = "Simple Line Graph"           'change desired options
        .SubTitle.Text = "Five Available Line Styles"
        .XTitle.Text = "X Axis"
        .YTitle.Text = "Y Axis"
        .Cols = 5
        .Rows = 12
        REDIM .Data(.Cols,.Rows)
        k = 100                                         'Load the data
        Randomize
        For j = 1 to .Rows
          .LabelList.AddItems "Label " + Str$(j)
        Next
        For i = 1 To .Cols                              'for each col
          .Series(i).LineStyle = (i-1) Mod 5            'Cycle line styles
          .LegendList.AddItems "Group " + Str$(i)
          For j = 1 To .Rows                            'add data to each row
            .Data(i,j) =  Str$(Rnd * k)
          Next j
        Next i
'        .Data(INT(RND*.Cols + 1),INT(RND*.Rows + 1)) = .Missing 'Missing Data
        .DrawChart (FALSE)                              'Draw it!
      End With

    Case "Line w/Points"
      ClickedLine = ClickedLine + 1                     'Change options on existing chart
      If ClickedLine > 3 Then ClickedLine = 1           'one by one
      With Graph
        .Initialize                                     'Set defaults
        .ChartType = ctLine                             'Line Chart
        .ChartStyle = csBoth                            'Points and Lines
        .MainTitle.Text = "Line Graph With Points"      'change desired options
        .SubTitle.Text = "Tics Only, Data Plotted in B&W, Custom Color Scheme"
        .XTitle.Text = "X Axis"
        .YTitle.Text = "Y Axis"
        If ClickedLine = 2 Then .YAxis.LogScale = TRUE
        If ClickedLine = 3 Then .ChartStyle = csPoints
        .BW = TRUE                                      'Data drawn in B&W
        .XAxis.Grid = FALSE                             'Tics only
        .YAxis.Grid = FALSE
        .bgColor = .Colors(23)                          'different colors
        .PlotAreaColor = .Colors(24)
        .MainFont.Color = .Colors(1)
        .SubFont.Color = .Colors(1)
        .AxisFont.Color = .Colors(7)
        .LegendFont.Color = .Colors(7)
        .GridColor = .Colors(8)
        .Xaxis.Color = .Colors(8)
        .YAxis.Color = .Colors(8)
        .Cols = 4
        .Rows = 6
        REDIM Graph.Data(.Cols,.Rows)
        k = 12                                          'Load the data
        Randomize
        For j = 1 to .Rows
          .LabelList.AddItems "Lbl " + Str$(j)
        Next
        For i = 1 To .Cols
          .LegendList.AddItems "Group " + Str$(i)
          For j = 1 To .Rows
            .Data(i,j) =  Str$(Rnd * k)
          Next j
        Next i
'        .Data(INT(RND*.Cols + 1),INT(RND*.Rows + 1)) = .Missing 'Missing Data
        .DrawChart (FALSE)                              'Draw it!
      End With

    Case "Box Plot"
      With Graph
        .Initialize                                     'Set defaults
        .ChartType = ctBox                              'Box and Whisker Plot
        .MainTitle.Text = "Box and Whisker Plot"        'change desired options
        .XTitle.Text = "X Axis"
        .YTitle.Text = "Y Axis"
        .XAxis.Grid = FALSE
        .DoLegend = FALSE
        .Cols = 6
        .Rows = 5
        REDIM Graph.Data(.Cols,.Rows)
        k = 20                                          'Load the data
        Randomize
        For i = 1 To .Cols                              'for each col
          .Series(i).AutoColor = FALSE
          .Series(i).Color = .Colors(2)
          .LabelList.AddItems "Group " + Str$(i)
'          .LegendList.AddItems "Group " + Str$(i)
          .Data(i , 1) = Str$(Rnd * k + 100)            'Max Values
          .Data(i , 2) = Str$(Rnd * k + 75)             'Q3 Values
          .Data(i , 3) = Str$(Rnd * k + 50)             'Q2 Values
          .Data(i , 4) = Str$(Rnd * k + 25)             'Q1 Values
          .Data(i , 5) = Str$(Rnd * k)                  'Min Values
        Next i
        .DrawChart (FALSE)                              'Draw it!
      End With

    Case "HLC Style 1"
      With Graph
        .Initialize                                     'Set defaults
        .ChartType = ctHiLo                             'Hi-Lo-Close Plot
        .ChartStyle = csAntenna
        .MainTitle.Text = "Hi-Lo-Close Plot"            'change desired options
        .SubTitle.Text = "Antenna Style"
        .XTitle.Text = "X Axis"
        .YTitle.Text = "Y Axis"
        .XAxis.Grid = FALSE
        .Cols = 12
        .Rows = 3
        REDIM Graph.Data(.Cols,.Rows)
        k = 10                                          'Load the data
        Randomize
        For i = 1 To .Cols                              'for each col
          .LabelList.AddItems "Group " + Str$(i)
          .Data(i , 1) = Rnd * k + 100                  'Hi Value
          .Data(i , 2) = Rnd * k + 75                   'Close Value
          .Data(i , 3) = Rnd * k + 50                   'Lo Value
        Next i
        .DrawChart (FALSE)                              'Draw it!
      End With

    Case "HLC Style 2"
      With Graph
        .Initialize                                     'Set defaults
        .ChartType = ctHiLo                             'Hi-Lo-Close Plot
        .ChartStyle = csHiLoBox
        .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
        .XAxis.Labeled = FALSE
        .Cols = 12
        .Rows = 3
        REDIM Graph.Data(.Cols,.Rows)
        k = 10                                          'Load the data
        Randomize
        For i = 1 To .Cols                              'for each col
          .LegendList.AddItems "Group " + Str$(i)
          .Data(i , 1) = Rnd * k + 75                   'Hi Value
          .Data(i , 2) = Rnd * k + 50                   'Close Value
          .Data(i , 3) = Rnd * k + 25                   'Lo Value
        Next i
        .DrawChart (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
        .PlotAreaColor = .Colors(24)
        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 "Default"
      With Graph                                        'The minimum (3) required calls
        .Initialize                                     'Always call this first
        REDIM .Data(.Cols,.Rows)                        '.Cols and .Rows set to 1 in .Initialize
        .DrawChart(FALSE)                               'Always call this last
      End With

    Case "Copy"
      Graph.CopyToClipboard (Graph.Width, Graph.Height)

    Case "Save"
      Graph.SaveChart (0)

    Case "Print"
      Graph.PrintChart (Printer.PrinterIndex, 1, 40, 1, TRUE)

    Case "Exit"
      frmMain.Close

  End Select

End Sub

'-------------------------------------------------------------------------------------------
Sub frmMainResize (SENDER As QFORM)

  '----- Prevent initial resize exception
  If AppStart Then AppStart = FALSE : Exit Sub

  With Graph
    .Width = frmMain.ClientWidth - btnExit.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

'-------------------------------------------------------------------------------------------
Sub frmMainClose (SENDER As QFORM)

  Application.Terminate

End Sub

'-------------------------------------------------------------------------------------------