The script below is example code you can use in a custom QScript that will create a table and plot for all questions filtered by another question.
Technical details
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). This bit should be removed if running the QScript in Displayr.
- 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.
Method
This example code uses Q3. Age from the cola.sav file to create the filters. You should look for ////EDITING comments in the code below on what you may want to modify.
////EDIT remove this chunk if running in Displayr
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."); //EDIT change Q3. Age to the name of your Nominal variable set to use for the filters
//if using a binary-multi variable set, use var age_banner = data_file.getQuestionByName("Q3. Age")
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
- QScript for an explanation of how to run this code.
- QScript Examples Library for other examples.
- QScript Reference for technical information.
- JavaScript for information about the JavaScript programming language.
- Table JavaScript and Plot JavaScript for tools for using JavaScript to modify the appearance of tables and charts.
- JavaScript Variables for detail on how to create new variables.