Exercise: Using an if...else Statement to Change Chart Types

  To display a line chart if the check box is checked, otherwise (else) display a pie chart:

  1. Add a new check box to Limits Dashboard, change the Name to chk_ChartType, and change the Title to Show Chart As A Line Chart (use the Properties dialog box).

  2. Open the Script Editor on the new Show Chart As A Line Chart check box.

  3. Type if (), a return [Enter] an open curly bracket ({), two returns [Enter] and a close curly bracket (}).

    if ()
    {
    
    }

    The parentheses hold the controlling condition test. The curly brackets are for the body of the if, executed only when the condition tests to true.

  4. Click inside the control part of the if, then use the Object browser to navigate to Limits Dashboard Objects , then chk_ChartType , then Properties and then double-click Checked.

    if (chk_ChartType.Checked)
    {
    
    }

    The Description pane shows Property Checked as Boolean. There are two Boolean values: true and false.

  5. After the chk_ChartType.Checked property, type ==true.

    if (chk_ChartType.Checked==true)
    {
    
    }

    Verify that there are two equal signs, meaning match true, not assign the value true. Condition statements use the comparison or logical operators Logical Operators.

  6. Add a statement in the body of the if statement to change the Limits Chart to type Line.

    1. Click inside the body of the if (on the blank line between the curly brackets), then use the Object browser to navigate to Application , then ActiveDocument, then Sections, then Limits Chart, then Properties and then double-click ChartType.

      if (chk_ChartType.Checked==true)
      {
      ActiveDocument.Sections["Limits Chart"].ChartType
      }

      The Description Pane shows Property ChartType as BqChartType. Find the collection for BqChartType in the object model under Constants.

    2. After ChartType, type an equal sign (=), navigate to Constants, then BqChartType, and then double-click bqChartTypeLine.

    3. Type a semicolon (;) at the end of the statement.

      if (chk_ChartType.Checked==true)
      {
      ActiveDocument.Sections["Limits Chart"].ChartType=bqChartTypeLine;
      }

      The semicolon clarifies where the statement ends and is recommended practice.

  7. On a new line, after the close curly bracket for the if, type else, a return [Enter], an open curly bracket ({), two returns [Enter], and a close curly bracket (}).

    }
    else 
    { 
    
    } 

    The curly brackets are for the body of the else, executed only when the condition tests false.

  8. Click in the body of the else and use the Object browser to add a statement to change the Limits Chart to a pie chart.

    else
    {
    ActiveDocument.Sections["Limits Chart"].ChartType=bqChartTypePie;
    }

    The statement should end with a semicolon.

  9. Click OK to save the script and close the Script Editor.

  10. Toggle to Run mode to test the script.

When Show Chart As Line Chart is checked, the chart type changes to Line. When it is not checked, the chart type is Pie. The on/off (checked/unchecked) states of the check box controls two chart type options.