Sort/Reorder Rows or Columns - Change the Order of Statistics

From Q
Jump to navigation Jump to search


This rule allows you to choose the order in which statistics are shown on tables.

Example

ReorderStatisticsRule.png

Technical details

Statistics are selected from a menu. The first selected statistic will appear at the top of the cell, followed by the second selected statistic, and so on. It is only necessary to select the statistics that you want to use. Statistics will only be re-ordered if they have been selected to appear on the table (see Statistics - Cells).

This rule should be applied again separately when you want to apply it to another table that uses a different set of statistics. For example, if you add this rule to a table showing a Pick One question to show the n before the %, you should apply the rule again if you want to place the Base n before the Average on table showing a Number question rather than allowing the two tables to share the same rule.

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

// This rule allows the user to select the order of the statistics that are
//  shown in the table using menus.
includeWeb('Table JavaScript Utility Functions');
includeWeb('QScript Utility Functions');

excludeRTables();

const not_selected_string = '(none)';

// Start the form
let menu_description = form.newLabel('Select the order of statistics:');
menu_description.lineBreakAfter = true;

// Change Statistics - Cells, Below, Right?
let stat_position_list = ['Cells'];
if (belowTableExists())
	stat_position_list.push('Below');
if (rightTableExists())
	stat_position_list.push('Right');
const separator = inDisplayr() ? ' > ' : ' - ';
stat_position_list = stat_position_list.map(item => 'Statistics' + separator + item);

let position_label = form.newLabel('Reorder: ');
let position_menu = form.newComboBox('pos', stat_position_list);
position_menu.lineBreakAfter = true;
position_menu.setDefault(stat_position_list[0]);
let description = form.newLabel('Choose the order in which statistics are shown on tables');
description.lineBreakAfter = true;
let control_array = [description, position_label, position_menu, menu_description];
form.setInputControls(control_array);
let selected_position = position_menu.getValue();
let tt;

let table_selected = selected_position.split(separator).pop();
if (table_selected === 'Cells')
	tt = table;
else if (table_selected === 'Below')
	tt = below_table;
else if (table_selected === 'Right')
	tt = right_table;

let available_statistics = tt.availableStatistics;
let translated_statistics = available_statistics.map(function(s) {
    try {
	return tt.getTranslation(s);
    }catch(e) {
	return s;
    }
});

let selection_list = [not_selected_string].concat(translated_statistics);
let last_selection = not_selected_string;
let first_menu = form.newComboBox('box0', selection_list);
first_menu.setDefault(not_selected_string);
first_menu.lineBreakAfter = true;
control_array.push(first_menu);
form.setInputControls(control_array);
last_selection = first_menu.getValue();
let selected_stats = [last_selection];

// Loop while the user continues to make selections and expand the form
while (last_selection != not_selected_string) {
	// Remove any stats already selected from the list
	selection_list = selection_list.filter(function(s) {return selected_stats.indexOf(s) == -1; });
	// Add a new menu
	let new_name = 'box' + (control_array.length - 1); // so first loop iteration will be 'box1'
	let new_menu = form.newComboBox(new_name, selection_list);
	new_menu.setDefault(not_selected_string);
	new_menu.lineBreakAfter = true;
	last_selection = new_menu.getValue();
	control_array.push(new_menu);
	selected_stats.push(last_selection);
	form.setInputControls(control_array);
}
selected_stats.pop();
// map back to English names of statistics
let selected_stats_eng = selected_stats.map(function(s){ return available_statistics[translated_statistics.indexOf(s)]; });

// Build array of reordered statistics
let current_statistics = tt.statistics;
let reordered_statistics = [];

// Add each of the selected stats to the array of reordered statistics
selected_stats_eng.forEach(function(stat) {
    if (stat != not_selected_string) {
        let stat_index = current_statistics.indexOf(stat);
        if (stat_index > -1) {
            reordered_statistics.push(stat);
            current_statistics.splice(stat_index, 1);
        }
    }
});

// Add the remaining statistics at the end of the array of reordered statistics
let final_statistics = reordered_statistics.concat(current_statistics);

// Set the reordered statistics
tt.statistics = final_statistics;

// Generate a summary that indicates the selection
let last = Math.min(3, selected_stats.length);
let rule_name = 'Change the Order of Statistics';
let summary_text = rule_name.charAt(0) + rule_name.slice(1).toLowerCase();
let chosen_stats = '';
if (selected_stats.length > 0)
	chosen_stats += ': ' + selected_stats.slice(0, last).join(', ');
if (selected_stats.length > last + 1)
	chosen_stats += '...';
rule_name += chosen_stats;
summary_text += chosen_stats;

form.setHeading(rule_name);
form.setSummary(summary_text);

// Returns true if Statistics - Below are available for this table.
// This function needs to be kept in the main body of the rule to ensure
// that below_table is included properly before the rule code is
// executed.
function belowTableExists() {
    let exists = true;
    try {
        below_table.statistics;
    } catch (e) {
        exists = false;
    }
    return exists;
}

// Returns true if Statistics - Right are available for this table.
// This function needs to be kept in the main body of the rule to ensure
// that right_table is included properly before the rule code is
// executed.
function rightTableExists() {
    let exists = true;
    try {
        right_table.statistics;
    } catch (e) {
        exists = false;
    }
    return exists;
}

See also