surveyjs / survey-library

Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout.
https://surveyjs.io/form-library
MIT License
4.23k stars 820 forks source link

avg() expression not displaying #7809

Closed ilaplain15115 closed 9 months ago

ilaplain15115 commented 10 months ago

Are you requesting a feature, reporting a bug or asking a question? Question

What is the current behavior? Trying to get an average of two expression values however no value displays.

What is the expected behavior? The expected behavior would show the average of Calculation1 expression value and Calculation2 expression value.

ie Calculation 1= 80% and Calculation 2 = 90%. Average score expression= 85%

How would you reproduce the current behavior (if this is a bug)? Provide the test code and the tested page URL (if applicable) Tested page URL:

Test code

{
 "title": "GitHub",
 "logoPosition": "right",
 "pages": [
  {
   "name": "page1",
   "elements": [
    {
     "type": "paneldynamic",
     "name": "question6",
     "questionCategory": "surveyquestion",
     "templateElements": [
      {
       "type": "paneldynamic",
       "name": "question1",
       "questionCategory": "surveyquestion",
       "templateElements": [
        {
         "type": "matrixdropdown",
         "name": "table1",
         "title": "Order",
         "questionCategory": "surveyquestion",
         "columns": [
          {
           "name": "Column1",
           "cellType": "checkbox",
           "totalType": "count",
           "choices": [
            "Yes"
           ]
          },
          {
           "name": "Column2",
           "cellType": "checkbox",
           "totalType": "count",
           "choices": [
            "No"
           ],
           "storeOthersAsComment": true
          }
         ],
         "choices": [
          1,
          2,
          3,
          4,
          5
         ],
         "rows": [
          "Row 1",
          "Row 2",
          "Row 3",
          "Row 4",
          "Row 5",
          "Row 6",
          "Row 7",
          "Row 8",
          "Row 9",
          "Row 10"
         ]
        },
        {
         "type": "expression",
         "name": "calculation1",
         "title": "Calculation 1",
         "description": "Yes = 10 points\nNo = 0 Points",
         "questionCategory": "surveyquestion",
         "expression": "{panel.table1-total.Column1}*0.1",
         "displayStyle": "percent"
        }
       ],
       "allowAddPanel": false,
       "allowRemovePanel": false,
       "panelCount": 1
      },
      {
       "type": "paneldynamic",
       "name": "question5",
       "questionCategory": "surveyquestion",
       "templateElements": [
        {
         "type": "matrixdropdown",
         "name": "table2",
         "title": "Order",
         "questionCategory": "surveyquestion",
         "columns": [
          {
           "name": "Column1",
           "cellType": "checkbox",
           "colCount": 0,
           "totalType": "count",
           "choices": [
            {
             "value": "Yes",
             "text": "Highly Effective"
            }
           ],
           "storeOthersAsComment": true
          },
          {
           "name": "Column2",
           "cellType": "checkbox",
           "colCount": 0,
           "totalType": "count",
           "choices": [
            {
             "value": "No",
             "text": "Effective"
            }
           ],
           "storeOthersAsComment": true
          },
          {
           "name": "Column3",
           "cellType": "checkbox",
           "colCount": 0,
           "totalType": "count",
           "choices": [
            {
             "value": 1,
             "text": "Need Improvement"
            }
           ],
           "storeOthersAsComment": true
          }
         ],
         "choices": [
          1,
          2,
          3,
          4,
          5
         ],
         "cellType": "checkbox",
         "rows": [
          "Row 1",
          "Row 2",
          "Row 3",
          "Row 4",
          "Row 5",
          "Row 6",
          "Row 7",
          "Row 8"
         ]
        },
        {
         "type": "expression",
         "name": "calculcation2",
         "title": "Calculation2",
         "questionCategory": "surveyquestion",
         "expression": "({panel.table2-total.Column1}*0.125) + ({panel.table2-total.Column2}*0.1)",
         "displayStyle": "percent"
        }
       ],
       "allowAddPanel": false,
       "allowRemovePanel": false,
       "panelCount": 1
      },
      {
       "type": "expression",
       "name": "question2",
       "title": "Score",
       "questionCategory": "surveyquestion",
       "expression": "avg({panel.calculcation2}, {panel.calculation1})"
      }
     ],
     "allowAddPanel": false,
     "allowRemovePanel": false,
     "panelCount": 1
    }
   ]
  }
 ]
}

Specify your browser: Chrome browser version: 119.0.6045.160 surveyjs platform (angular or react or jquery or knockout or vue): Angular surveyjs version:1.9.112

JaneSjs commented 9 months ago

Hi @ilaplain15115, Please accept my apologies for the delayed reply. Your survey contains two different Dynamic Panels; the Average Expression field is placed outside the dynamic panel. Therefore, you cannot calcualate the total value by using the specified expression because a Dynamic Panel contains an array of values rather than a single value.

If you do not plan to add more dynamic panels, you can simply use a Panel element to display your matrices. Now, the specified expression works:

{
 "title": "GitHub",
 "logoPosition": "right",
 "pages": [
  {
   "name": "page1",
   "elements": [
    {
     "type": "paneldynamic",
     "name": "question6",
     "questionCategory": "surveyquestion",
     "templateElements": [
      {
       "type": "panel",
       "name": "question1",
       "questionCategory": "surveyquestion",
       "elements": [
        {
         "type": "matrixdropdown",
         "name": "table1",
         "title": "Order",
         "questionCategory": "surveyquestion",
         "columns": [
          {
           "name": "Column1",
           "cellType": "checkbox",
           "totalType": "count",
           "choices": [
            "Yes"
           ]
          },
          {
           "name": "Column2",
           "cellType": "checkbox",
           "totalType": "count",
           "choices": [
            "No"
           ],
           "storeOthersAsComment": true
          }
         ],
         "choices": [
          1,
          2,
          3,
          4,
          5
         ],
         "rows": [
          "Row 1",
          "Row 2",
          "Row 3",
          "Row 4",
          "Row 5",
          "Row 6",
          "Row 7",
          "Row 8",
          "Row 9",
          "Row 10"
         ]
        },
        {
         "type": "expression",
         "name": "calculation1",
         "title": "Calculation 1",
         "description": "Yes = 10 points\nNo = 0 Points",
         "questionCategory": "surveyquestion",
         "expression": "{panel.table1-total.Column1}*0.1",
         "displayStyle": "percent"
        }
       ]
      },
      {
       "type": "panel",
       "name": "question5",
       "questionCategory": "surveyquestion",
       "elements": [
        {
         "type": "matrixdropdown",
         "name": "table2",
         "title": "Order",
         "questionCategory": "surveyquestion",
         "columns": [
          {
           "name": "Column1",
           "cellType": "checkbox",
           "colCount": 0,
           "totalType": "count",
           "choices": [
            {
             "value": "Yes",
             "text": "Highly Effective"
            }
           ],
           "storeOthersAsComment": true
          },
          {
           "name": "Column2",
           "cellType": "checkbox",
           "colCount": 0,
           "totalType": "count",
           "choices": [
            {
             "value": "No",
             "text": "Effective"
            }
           ],
           "storeOthersAsComment": true
          },
          {
           "name": "Column3",
           "cellType": "checkbox",
           "colCount": 0,
           "totalType": "count",
           "choices": [
            {
             "value": 1,
             "text": "Need Improvement"
            }
           ],
           "storeOthersAsComment": true
          }
         ],
         "choices": [
          1,
          2,
          3,
          4,
          5
         ],
         "cellType": "checkbox",
         "rows": [
          "Row 1",
          "Row 2",
          "Row 3",
          "Row 4",
          "Row 5",
          "Row 6",
          "Row 7",
          "Row 8"
         ]
        },
        {
         "type": "expression",
         "name": "calculcation2",
         "title": "Calculation2",
         "questionCategory": "surveyquestion",
         "expression": "({panel.table2-total.Column1}*0.125) + ({panel.table2-total.Column2}*0.1)",
         "displayStyle": "percent"
        }
       ]
      },
      {
       "type": "expression",
       "name": "question2",
       "title": "Score",
       "questionCategory": "surveyquestion",
       "expression": "avg({panel.calculcation2}, {panel.calculation1})"
      }
     ],
     "allowAddPanel": false,
     "allowRemovePanel": false,
     "panelCount": 1
    }
   ]
  }
 ]
}

image

View Demo

Please feel free to reopen this issue if you have additional questions.

Thanks