Sorting and Reordering - Change the Order of Statistics

From Q
Jump to: navigation, search

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



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.


  • 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).


// 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");
var not_selected_string = "(none)";
// Start the form
var menu_description = form.newLabel('Select the order of statistics:');
menu_description.lineBreakAfter = true;
// Change Statistics - Cells, Below, Right?
var stat_position_list = ["Statistics - Cells"];
if (belowTableExists())
	stat_position_list.push("Statistics - Below");
if (rightTableExists())
	stat_position_list.push("Statistics - Right");
var position_label = form.newLabel('Reorder: ');
var position_menu = form.newComboBox("pos", stat_position_list);
position_menu.lineBreakAfter = true;
position_menu.setDefault("Statistics - Cells");
var control_array = [position_label, position_menu, menu_description]
var selected_position = position_menu.getValue();
var tt;
if (selected_position == "Statistics - Cells")
	tt = table;
else if (selected_position == "Statistics - Below")
	tt = below_table;
else if (selected_position == "Statistics - Right")
	tt = right_table;
var remaining_statistics = tt.availableStatistics;
var selection_list = [not_selected_string].concat(remaining_statistics);
var last_selection = not_selected_string;
var first_menu = form.newComboBox("box0", selection_list);
first_menu.lineBreakAfter = true;
last_selection = first_menu.getValue();
var 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
	var new_name = "box" + (control_array.length - 1); // so first loop iteration will be 'box1'
	var new_menu = form.newComboBox(new_name, selection_list);
	new_menu.lineBreakAfter = true;
	last_selection = new_menu.getValue();
// Build array of reordered statistics
var current_statistics = tt.statistics;
var reordered_statistics = [];
// Add each of the selected stats to the array of reordered statistics
selected_stats.forEach(function(stat) {
	if (stat != not_selected_string)
		var stat_index = current_statistics.indexOf(stat);
		if (stat_index > -1) {
			current_statistics.splice(stat_index, 1);
// Add the remaining statistics at the end of the array of reordered statistics
var final_statistics = reordered_statistics.concat(current_statistics);
// Set the reordered statistics
tt.statistics = final_statistics;
// Generate a summary that indicates the selection
var last = Math.min(3, selected_stats.indexOf(not_selected_string));
var rule_name = "Change the order of statistics";
if (selected_stats.length > 0)
	rule_name += ": " + selected_stats.slice(0, last).join(', ');
if (selected_stats.length > last + 1)
	rule_name += "...";
// 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() {
    var exists = true;
    try { 
    } 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() {
    var exists = true;
    try { 
    } catch (e) {
        exists = false;
    return exists;

See also

Personal tools