Modifying Headers - Abbreviate Month Labels

From Q
Jump to: navigation, search

This rule automatically abbreviates the names of months in crosstabs that show date questions in the rows or columns. For example, it will change January 2015 to Jan '15. This allows for more compact column headers. You can choose whether to modify the labels in the rows or the columns of the table.

Example

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

// Abbreviate Month Names

var label = form.newLabel("Months in");
var row_or_column_box = form.newComboBox("rowcol", ["columns", "rows"]);
row_or_column_box.setDefault("columns");
form.setInputControls([label, row_or_column_box]);
var do_rows = row_or_column_box.getValue() == "rows";

form.setSummary("Abbreviate month names in " + row_or_column_box.getValue());

var invalid_message = "the table requires months to be shown in the " + row_or_column_box.getValue();

if (!do_rows && table.columnLabels == null)
    form.ruleNotApplicable(invalid_message);

var labels = do_rows ? table.rowLabels : table.columnLabels;
labels = labels.map(function (label) {
    var label_elements = label.split(" ")
    if (label_elements.length > 2)
        form.ruleNotApplicable(invalid_message);
    var month_prefix = label_elements[0];
    if (month_prefix.indexOf("January") == 0)
        month_prefix = "Jan";
    else if (month_prefix.indexOf("February") == 0)
        month_prefix = "Feb";
    else if (month_prefix.indexOf("March") == 0)
        month_prefix = "Mar";
    else if (month_prefix.indexOf("April") == 0)
        month_prefix = "Apr";
    else if (month_prefix.indexOf("August") == 0)
        month_prefix = "Aug";
    else if (month_prefix.indexOf("September") == 0)
        month_prefix = "Sept";
    else if (month_prefix.indexOf("October") == 0)
        month_prefix = "Oct";
    else if (month_prefix.indexOf("November") == 0)
        month_prefix = "Nov";
    else if (month_prefix.indexOf("December") == 0)
        month_prefix = "Dec";

    var year = label_elements[1];
    if (year.length != 4)
        form.ruleNotApplicable(invalid_message);
    return month_prefix + " '" + year.substring(2);
});

if (do_rows)
    table.rowLabels = labels;
else
    table.columnLabels = labels;

See also