Create Tables and Plots for All Questions Filtered by Age

From Q
Jump to navigation Jump to search

This script:

  • Starts a project by importing C:\Program Files\Q\Examples\cola.sav (this may be located on a different place on your computer depending upon how Q was installed).
  • Creates Filters (using a Banner question as a shortcut) of all the age categories.
  • Creates Groups, one for each filter category. In each group, the script:
    • Creates tables of text questions.
    • Creates charts of all the non-text questions and:
      • Applies a stacked column chart template to all Pick One – Multi questions.
      • Applies a grid of bars template to all other non-text questions.
  • Applies the relevant filter to every question in the group.


log('Reading in cola.sav');
project.newProject();
data_file = project.addDataFile(".\\Cola.sav","Cola.sav", { auto_tidy_labels: true,
                                                            auto_detect_questions: false,
                                                            strip_labels_html: true });

log("Creating a banner (as a short cut to creating filters.");
var age_banner = data_file.createBanner("Age banner", [[data_file.getQuestionByName("Q3. Age")]]);
age_banner.isFilter = true;
age_banner.isHidden = true;

for (var i = 1; i < age_banner.variables.length; i ++) { //starting at i=1 to skip empty first age category
    var age_var = age_banner.variables[i];
    log("Creating plots and tables for " + age_var.label + ".");
    var group = project.report.appendGroup();
    group.name = age_var.label;
    for (var q_i in data_file.questions) {
        var question = data_file.questions[q_i];
        if (!question.isHidden) {
            var item = group.appendTable();
            item.primary = question;
            if (question.questionType != "Text" && question.questionType != "Text - Multi") {
                if (question.questionType == 'Pick One - Multi')
                    item = item.applyTemplate("Stacked example template.QTemplate");
                else
                    item = item.applyTemplate("Grid example template.QTemplate");
            }
            item.filters = [age_var];
        }
    }
}

See also