Modify Whole Table or Plot - Show Only Top K Rows (e.g., Top 5 Rows)

From Q
Jump to navigation Jump to 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). 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. See also Modifying the Whole Table or Plot - Showing Only the Top k and Specified Rows.

Example

Top3.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.

includeWeb("Table JavaScript Utility Functions");
 
// creating the form
form.setHeading('Show Only the Top Rows');
let description = form.newLabel("Show only the top rows in the table.")
description.lineBreakAfter = true;
let number_label = form.newLabel('Number of rows:'); //creating the phrase 'Number of rows:' 
let numeric_up_down = form.newNumericUpDown('threshold'); //creating a control where a user can enter a number.
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(1); // preventing the user from having tables with less than 1 row
form.setInputControls([description, number_label, numeric_up_down]); // telling Q to create the form
// Extracting the number of rows provided by the user and assigning it to a variable.
let maximum_number_rows = numeric_up_down.getValue();
 
// Deleting the rows.
while(table.numberRows > maximum_number_rows)
    deleteRowComplete(table.numberRows - 1); 
// creating the form title
form.setSummary('Show the top ' + maximum_number_rows + ' rows');
 
// 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