Modifying the Whole Table or Plot - Showing Only the Top k and Specified Rows

From Q
Jump to: navigation, search

This rule allows you to select the number of rows to show on a table or chart. The user is able to specify the number of rows (i.e., k), and also to specify particular rows that must always appear. Colors can be assigned to the rows that must always appear. If used after first applying Sorting and Reordering - Sort Rows (Automatically Updates when Data Changes), it allows you to show, for example, the top 3 brands automatically.

Example

This example applies two rules. First, Sorting and Reordering - Sort Rows (Automatically Updates when Data Changes), and then this rule. It has been set so that it shows the top two brands and, if Pepsi appears, it highlights it in green. In this instance, Pepsi was not in the top two rows, so it is appended to the table as a third row.

TopKAndSpecified.png

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

You can find a detailed discussion of this code at Creating a Custom Rule.

// Heading for the form
form.setHeading('Selecting the number of rows to show on a table and specifying required rows');
 
includeWeb("Table JavaScript Utility Functions");
 
// Creating input for user to specify number of rows
var number_label = form.newLabel('Number of rows:'); //creating the phrase 'Number of rows:' 
var numeric_up_down = form.newNumericUpDown('threshold'); //creating a control where a user can enter a number.
numeric_up_down.lineBreakAfter = true;
numeric_up_down.setDefault(5);// specifying the default number of rows to be shown
numeric_up_down.setIncrement(1); // specifying that the user must enter whole numbers.
numeric_up_down.setMinimum(0); // preventing the user from having tables with less than 0 rows (not 1, as a user may wish to only specify the brands)
var control_array = [number_label, numeric_up_down];
 
// Textboxes for selecting rows to always retain
var label_row = form.newLabel('Rows to always retain:');
var text_box = form.newTextBox('textBox');
control_array.push(label_row);
control_array.push(text_box);
form.setInputControls(control_array);
 
// Adding addition text boxes, if required
var last_selection = text_box.getValue();
var required_row_labels = [];
while (last_selection != "") {
    required_row_labels.push(last_selection);
    var new_name = "textBox" + (control_array.length - 1);
    var new_menu = form.newTextBox(new_name);
    last_selection = new_menu.getValue();
    control_array.push(new_menu);
    form.setInputControls(control_array);
}
 
// control for the output color
var label_color = form.newLabel('Color for required rows:');
var color_picker = form.newColorPicker('color');
color_picker.setDefault('White');
form.setOutputControls([label_color, color_picker]);
 
// control for the output color
var color = color_picker.getValue();
form.setOutputColor(color);
 
// Extracting the number of rows provided by the user and assigning it to a variable.
var maximum_number_rows = numeric_up_down.getValue();
 
// Creating a summary
var summary = 'Top ' + maximum_number_rows + ' rows';
if (required_row_labels.length != 0)
    summary += '; always show "' + required_row_labels.join('", "') + '"';
form.setSummary(summary);
 
// Deleting the rows more than the specified number (unless they are required)
var row_labels = table.rowLabels;
var n_extra_required_rows = 0;
while (table.numberRows > maximum_number_rows + n_extra_required_rows) {
    var last_row = table.numberRows - 1 - n_extra_required_rows;
    var last_label = row_labels[last_row];
    if (required_row_labels.indexOf(last_label) != -1) {
        n_extra_required_rows++;
    } else {
        deleteRowComplete(last_row);
    }
}
 
// Coloring rows
row_labels = table.rowLabels; //getting again as may have changed
var colors = table.cellColors;
row_labels.forEach(function (label, index) {
    if (required_row_labels.indexOf(row_labels[index]) != -1) { // check if row label contains text
        for (var i = 0; i < table.numberColumns; i++) // set color for each element in row
            colors[index][i] = color;
    }
});
if (table.numberRows != 0)
    table.cellColors = colors;
 
// 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 { 
        right_table.statistics;
    } catch (e) {
        exists = false;
    }
    return exists;
}

See also

Personal tools
Namespaces

Variants
Actions
Navigation
Categories
Toolbox