Modify Cell Content - Change Color of Cells with Small Sample Sizes

From Q
Jump to navigation Jump to search


This rule changes the color of cells and adds a footer where the sample size (Column n) is less than a user-specified amount.

Example

ShadingSmallSamplesRule.PNG

Technical details

It will also deal with situations where missing data causes the sample size of each column to vary by row.

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('Table JavaScript Utility Functions');

excludeRTables();

let stat_name = table.getTranslation('Column n');
if (table.availableStatistics.indexOf('Column n') == -1)
    form.ruleNotApplicable('this format only applies to tables with the ' +
			   stat_name + ' statistic');

// Set up controls for user input.
form.setHeading('Change the Color of Cells With Small Sample Sizes');
let description = form.newLabel('Choose a color to apply to cells when the ' + stat_name + ' is smaller than a specified value');
description.lineBreakAfter = true;
let label = form.newLabel('Minimum ' + stat_name + ':');
let numeric_up_down  = form.newNumericUpDown('threshold');
numeric_up_down.setDefault(30);
numeric_up_down.setIncrement(1);
numeric_up_down.setMaximum(999999999);
numeric_up_down.lineBreakAfter = true;
let color_label = form.newLabel("Color:");
let color_picker = form.newColorPicker('color');
color_picker.setDefault('Pink');
form.setInputControls([description, label, numeric_up_down, color_label, color_picker]);
let min_column_n = numeric_up_down.requireValue();
form.setSummary('Change color of cells where ' + stat_name + ' < ' + min_column_n);

let color_to_use = color_picker.getValue();

// Figure out which cells need to be highlighted
let column_ns = table.get('Column n');// Get column sample sizes.
let colors = table.cellColors; // Obtain the current cell colors.
let color_flag = false; // Keep track of whether any cells have been highlighted.
let extra_footers = table.extraFooters;// Store the extra footnotes we generate for each blank column.
for (let column = 0; column < table.numberColumns; column++) {// loop through the columns
    for (let row = 0; row < table.numberRows; row++) {
	// Get the sample size for this cell
        let column_n = column_ns[row][column];
        // If the column's sample size is less than min_sample_size, make each cell in the column blank.
        if (column_n < min_column_n) {
            colors[row][column] = color_to_use;
            color_flag = true;
        }
    }
}

// Apply changes to the table
if (color_flag) {
    table.cellColors = colors; // Set the new table colors
    extra_footers.push('Shaded cells have a sample size that is smaller than ' + min_column_n + '.');
    // Set the extra footnotes.
    table.extraFooters = extra_footers;
}

See also