Modify Footers - Add Column Base Sizes

From Q
Jump to navigation Jump to search

This rule adds sample sizes for each column to the footer for a table or chart.

Example

Client = 919; Supplier = 3650;

Technical details

By editing the rule, you can change the delimiter and the text that appears between the name of the column and the sample size. For example, to: Client base is 919 Supplier base is 3650

How to apply this rule

For the first time in a project

  • Select the table(s)/chart(s) that you wish to apply the rule to.
  • Start typing the name of the Rule into the Search features and data box in the top right of the Q window.
  • Click on the Rule when it appears in the QScripts and Rules section of the search results.

OR

  • Select Automate > Browse Online Library.
  • Choose this rule from the list.

Additional applications of the rule

  • Select a table or chart that has the rule and any table(s)/chart(s) that you wish to apply the rule to.
  • Click on the Rules tab (bottom-left of the table/chart).
  • Select the rule that you wish to apply.
  • Click on the Apply drop-down and choose your desired option.
  • Check New items to have it automatically applied to new items that you create. Use Edit > Project Options > Save as Template to create a new project template that automatically uses this rule.

Removing the rule

  • Select the table(s)/chart(s) that you wish to remove the rule from.
  • Press the Rules tab (bottom-right corner).
  • Press Apply next to the rule you wish to remove and choose the appropriate option.

How to modify the rule

  • Click on the Rules tab (bottom-left of the table/chart).
  • Select the rule that you wish to modify.
  • Click Edit Rule and make the desired changes. Alternatively, you can use the JavaScript below to make your own rule (see Customizing Rules).

JavaScript

includeWeb('QScript Utility Functions');
includeWeb('Table JavaScript Utility Functions'); 

excludeRTables();

// Create the form.
let in_displayr = inDisplayr();
form.setHeading('Add Column ' + (in_displayr ? 'Sample' : 'Base') + ' Sizes');
form.setSummary('Show sample size for each column in the footer');
let description = form.newLabel('This rule will add the sample size counts for each column to the footer');
description.lineBreakAfter = true;
let label_text = form.newLabel('Text between column label and number:');
let text_box = form.newTextBox('between');
text_box.lineBreakAfter = true;
text_box.setDefault(' = ');
let label_text1 = form.newLabel('Delimiter:');
let text_box1 = form.newTextBox('delimiter');
text_box1.setDefault('; ');
form.setInputControls([description, label_text, text_box, label_text1, text_box1]);
let between = text_box.getValue();
let delimiter = text_box1.getValue();

// Getting the data
let column_n_missing = table.availableStatistics.indexOf('Column n') === -1;
let base_n_missing = table.availableStatistics.indexOf('Base n') === -1;
if (column_n_missing && base_n_missing)
{
    let message = in_displayr ? 'Sample size or Sample size' : 'n or Base n';
    form.ruleNotApplicable('This rule only works one tables with Column ' + message);
}
let column_ns = column_n_missing ? table.get('Base n') : table.get('Column n');
//
// Computing the ranges
let ranges = [];
for (let col = 0; col < table.numberColumns; col++) {
    let smallest_size = column_ns [0][col];
    let largest_size = smallest_size;
    for (let row = 0; row < table.numberRows; row++) {
        smallest_size = Math.min(smallest_size, column_ns [row][col]);
        largest_size = Math.max(largest_size, column_ns [row][col]);
    }
    ranges.push(smallest_size === largest_size ? smallest_size : smallest_size + ' to ' + largest_size);
}
// updating the footer
let ss = '';
if (ranges.length === 1 || table.blueQuestion.questionType === 'Pick One - Multi')
    ss += ranges[0] + delimiter;
else {
    let column_labels = table.columnLabels;
    for (let col = 0; col < table.numberColumns; col++){
        let col_label = column_labels[col];
        if (col_label != 'NET' && col_label != 'Total' && col_label != 'SUM') {
            ss += column_labels[col] + between + ranges[col] + delimiter;
            }
    }
}
let delim_check = new RegExp(delimiter + "$");
if (delim_check.test(ss))
    ss = ss.substring(0, ss.length - delimiter.length);
table.extraFooters = [ss];

See also