Insights Canvas: Run Calculations on Your Survey Data

The Calculation data element allows you to reference survey question data and statistics, run calculations on the data, and output the results to your Insights Canvas (all using SurveyGizmo's Custom Scripting library). Learn about adding other Data elements .

Setup

1. Click anywhere on your canvas or click the green Insert button to access the add-element menu. Select Calculation from the available options.

2. On the Settings tab, provide a Title for the Calculation element and input your script into the Calculation Workspace field. See the Scripting Functions section of this tutorial.

3. Use the Layout & Options tab to customize several visual aspects of the Calculation element. The following settings are available:

Display Options:

  • Width and Height - Set the width and height of your question chart (in pixels).
  • Rotation - Use this field to rotate the shape element clockwise. Enter a value between 0-360.
  • Order (z-index) - This feature is particularly important if you want to place your shape behind another element i.e. Text. In order to ensure that the shape is behind the text, you must input a higher z-index value for the text element than the z-index value for the shape. For example, a text element with a z-index of '2' would show in-front of a shape with a z-index of '1'.
  • Hide Element - Check the Hide this element checkbox to remove this specific question chart from view on your canvas.

Class Name

Use the class name provided to reference the calculation element in custom CSS via Canvas Style > Custom Code > Custom CSS. For more details regarding Insights Canvas styling options, visit the tutorial.

Tap Event

Learn about triggering Tap Events from your Calculation Element.

Scripting Functions

In addition to all functions in the SurveyGizmo Scripting Library the below functions are available to use for scripting in Insights Canvases.

getresults

Use getresults to pull data and available statistics for compatible questions. The following statistics are returned depending on the question type: min, max, average/mean, sum, sum of squares, variance, std_deviation, std_deviation_bounds

ParametersDescriptionRequired
sidThe Survey ID of the survey containing the question data that you want to reference.true
qidThe Question ID of the question that contains the data that you want to reference.true

json_encode

This function is available to JSON encode object data so that it is easier to work with. 

ParametersDescriptionRequired
value

The value you wish to encode. Can be any type. All string data must be UTF-8 encoded.

true
optionsOptions for formatting output: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT,JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE,JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_THROW_ON_ERROR.
false
depthThe maximum depth. Must be greater than zero.
false

json_decode

This function is available to decode JSON data. 

ParametersDescriptionRequired
json

The JSON string you wish to decode. 

true
assocWhen TRUE, returned objects will be converted into associative arrays.
false
depthRecursion depth.false
options
Options for formatting output: JSON_BIGINT_AS_STRING, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR.
false

Example 1

You might be wondering when you would use the Calculation element. 

In the below example script, we pull the results to NPS questions from two separate surveys. We then add up the Promoters, Passives, and Detractors from both surveys and last, calculate a combined NPS Score for the two surveys. The combined data is then output directly to your Insights Canvas via the Calculation element.

The items highlighted in yellow should be replaced with your survey and question IDs.

sid1 = 1241309 --Survey ID of first survey
qid1 = 2 --Question ID of NPS Question in first survey
sid2 = 1275369 --Survey ID of the second survey
qid2 = 2 --Question ID of NPS Question in second survey

arr1 = getresults(sid1, qid1)
arr2 = getresults(sid2, qid2)
 
detractors1 = arr1['result']['buckets'][0]['count']
passives1 = arr1['result']['buckets'][1]['count']
promoters1 = arr1['result']['buckets'][2]['count']

detractors2 = arr2['result']['buckets'][0]['count']
passives2 = arr2['result']['buckets'][1]['count']
promoters2 = arr2['result']['buckets'][2]['count']

total_det = detractors1 + detractors2
total_pas = passives1 + passives2
total_pro = promoters1 + promoters2

total = total_det + total_pas + total_pro
raw_nps = (total_pro / total * 100) - (total_det / total * 100)
nps = round(raw_nps, 1)

print("Total Promoters: " .. total_pro .. "
 Total Passives: " .. total_pas .. "
Total Detractors: " .. total_det)
print("
Combined NPS Score: " .. nps)

The resulting output is as follows (example):

Total Promoters: 43
Total Passives: 19
Total Detractors: 79
Combined NPS Score: -26

Example 2

This calculation can be used to calculate an average score for a number of questions. In this example, we are calculating an average score using three different Rating (Likert Scale) questions. The averaged data is then output directly to your Insights Canvas via the Calculation element.

In this example, the three Rating (Likert Scale) questions are all required. We calculate the sum of the answers to each question, then add them all together. We then average the value and divide by the number of total responses.

The items highlighted in yellow should be replaced with your survey and question IDs.

first = getresults(1275421,2)
second = getresults(1275421,3)
third = getresults(1275421,4)

total_responses = first['result']['total']

if total_responses then
    sum1 = 0
    for key,value in pairs (first['result']['values']) do
       sum1 = sum1 + (value['rep_val'] * value['count'])
    end
    
    sum2 = 0
    for key,value in pairs (second['result']['values']) do
       sum2 = sum2 + (value['rep_val'] * value['count'])
    end

    sum3 = 0
    for key,value in pairs (third['result']['values']) do
       sum3 = sum3 + (value['rep_val'] * value['count'])
    end
    
    sum = (sum1 + sum2 + sum3)
    average_3 = sum / 3
    get_average = (average_3 / total_responses)
    average = round(get_average, 1)
end

print("<h4>Average Score<br/>"..average.."</h4>")

The resulting output is as follows (example):

Average Score

3.6

You can use css to style the way items with the h4 tag in your Insights Canvas

Compatible Questions

The following Question Types are compatible with the getresults function on the Insights Canvas:

Scripting and Other Custom Solutions

We’re always happy to help you debug any documented script that is used as is. That said, we do not have the resources to write scripts on demand or to debug a customized script.

If you have customization ideas that you haven't figured out how to tackle, we're happy to be a sounding board for SurveyGizmo features and functionality ideas that might meet your needs. Beyond this, check out our Professional Services; these folks have the scripting chops to help you to achieve what you are looking for!

Basic Standard Market Research HR Professional Full Access Reporting
Free Individual Team & Enterprise
Feature Included In