A Rule governs how an output in Q, such as a chart or table, appears. The simplest rules are essentially identical to Conditional Formatting in Excel. For example, the chart below shades the bars proportional to the values. This example is created using Automate > Color Scale and modifying the color options (on the Rules tab, beneath the table).
Rules can also be used to manipulate the contents of outputs. In the example below, the sample size is inserted into each column heading. This uses Automate > Browse Online Library > Modify Headers > Add Sample Size to Column Labels.
In the next example, a traditional table has been over-written with results from a special form of regression, Shapley Driver Analysis. This example uses Regression - Legacy Driver Analysis - Table of Shapley Importance Scores, whereby a rule is added via QScript.
Method
JavaScript within Rules runs at the end of the process of calculating a table or chart and generating the output. The process for charts is similar to tables - in the background, Q is actually generating a table to construct the data to chart, including the series labels.
Manipulations of data performed by Rules using JavaScript can be very different from when changing the data elsewhere. For example, if you apply a filter to a table using Table JavaScript, it will not automatically be applied in the statistics shown below and to the right of the table. To change these requires additional code.
The process is as follows:
- The main table output is generated, which incorporates:
- Statistics selected by using the table context menu and the toolbar.
- Any changes to the row or column question's categories, including re-ordering, renaming, merging, etc., any operation done via the table context menu and the toolbar.
- If there are any Statistics - Right selected, another table output is generated for the table on the right, which is merged with the main table output later.
- If there are any Statistics - Below selected, another table output is generated for the table below, which is merged with the main table output later.
- The multiple comparison correction is performed, generating the Corrected p and Multiple Comparison Adjustment statistics (which affect displays of significance).
- Cell significance is calculated, determining arrow length and font color.
- The sample size description (shown in the table's footer) is calculated from the table's cell statistics (n, Base n, etc.).
- The JavaScript is run.
- The same table outputs that were given to your JavaScript, and modified by your JavaScript, are used to display on the screen, or exported to PDF/Office.
- If you apply a Rule to a basic non-R-based chart in Q or convert a table to this type of chart, the changes are effectively applied to the underlying table first and then carried over to the chart where applicable. The following chart types are the most compatible with Rules:
- Bar/Column/Line charts
- Stacked column/bar charts
- Grid of bars charts
Next
Adding Rules to Project Templates