Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

How To Use New Google Charts Library Loader base on Multiple Numerical Input que

  • RitaShen
  • RitaShen's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 3 months ago #221743 by RitaShen
Hi there

I know this question was discussed before,  How To Use New Google Charts Library Loader
But I'm confused that how to use google chart (Pie Chart) base on Multiple Numerical Input question?

Many thanks
Rita

File Attachment:

File Name: limesurvey...7566.lss
File Size:16 KB

version: 3.27.19
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago - 3 years 3 months ago #221759 by tpartner
What behaviour are you looking for?

Where do you want the chart to appear?

Do you want the chart shown and updated as the respondent inserts answers?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 3 years 3 months ago by tpartner.
The topic has been locked.
  • RitaShen
  • RitaShen's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 3 months ago #221779 by RitaShen
Dear tpartner

sure, I want the chart shown and updated as the respondent inserts answers.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago #221792 by tpartner
In that case, you will need to wrap the drawChart() function in a parent function that is called when the API is loaded. The drawChart() function is called when a change is made to the numeric inputs.

Code:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" data-author="Tony Partner">  
  $(document).on('ready pjax:scriptcomplete',function(){
 
    var qID = '{QID}';
    var thisQuestion = $('#question'+qID);
 
    // Load the Visualization API and the corechart package.
    google.charts.load('current', {'packages':['corechart']});
 
    // Set a callback to run when the Google Visualization API is loaded.
    google.charts.setOnLoadCallback(initChart);
 
    // This function is called when the Google Visualization API is loaded.
    function initChart() {
 
      // Insert the chart wrapper
      $('.question-text', thisQuestion).append('<div id="chart_wrapper_'+qID+'" class="chart-wrapper" />');
 
      // A function that:
      //  - Creates and populates a data table,
      //  - Instantiates the pie chart
      //  - Passes in the data
      //  - Draws the chart.
      function drawChart() {
 
        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Subquestion');
        data.addColumn('number', 'Value');
        var dataRows = [];
        $('.question-item', thisQuestion).each(function(i) {
          var thislabel = $.trim($('.control-label:eq(0)', this).text());
          var thisValue = ($.trim($(':text.form-control:eq(0)', this).val()) == '') ? 0 : Number($(':text.form-control:eq(0)', this).val());
          dataRows.push([thislabel, thisValue]);
        });
        data.addRows(dataRows);
 
        // Set chart options
        var options = {
          //'title':'Some chart title',
          'width':400,
          'height':300};
 
        // Instantiate and draw the chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chart_wrapper_'+qID+''));
        chart.draw(data, options);
      }
 
      $(':text.form-control', thisQuestion).on('keyup change', function(e){   
        drawChart();
      });
    }
    });
</script>

 

Sample survey attached: 

File Attachment:

File Name: limesurvey...6(1).lss
File Size:19 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • RitaShen
  • RitaShen's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 3 months ago #221795 by RitaShen
Tony Partner, I appreciate your help very much.
I've tried to insert the sample file, but it cannot appeared the pie chart smoothly
 
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago #221796 by tpartner
Do you see any JavaScript errors in the console? Do you have the XSS filter disabled?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago #221797 by tpartner
Oops, found a bug. Use this script and this sample survey.
 
 
Code:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" data-author="Tony Partner">  
  $(document).on('ready pjax:scriptcomplete',function(){
 
    var qID = '{QID}';
    var thisQuestion = $('#question'+qID);
 
    // Load the Visualization API and the corechart package.
    google.charts.load('current', { 'packages':['corechart'] });
 
    // Set a callback to run when the Google Visualization API is loaded.
    google.charts.setOnLoadCallback(initChart);
 
    // This function is called when the Google Visualization API is loaded.
    function initChart() {
 
      // Insert the chart wrapper
      $('.question-text', thisQuestion).append('<div id="chart_wrapper_'+qID+'" class="chart-wrapper" />');
 
      // A function that:
      //  - Creates and populates a data table,
      //  - Instantiates the pie chart
      //  - Passes in the data
      //  - Draws the chart.
      function drawChart() {
 
        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Subquestion');
        data.addColumn('number', 'Value');
        var dataRows = [];
        $('.question-item', thisQuestion).each(function(i) {
          var thislabel = $.trim($('.control-label:eq(0)', this).text());
          var thisValue = ($.trim($(':text.form-control:eq(0)', this).val()) == '') ? 0 : Number($(':text.form-control:eq(0)', this).val());
          dataRows.push([thislabel, thisValue]);
        });
        data.addRows(dataRows);
 
        // Set chart options
        var options = {
          //'title':'Some chart title',
          'width':400,
          'height':300};
 
        // Instantiate and draw the chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chart_wrapper_'+qID+''));
        chart.draw(data, options);
      }
 
      $(':text.form-control', thisQuestion).on('keyup change', function(e){   
        drawChart();
      });
    }
    });
</script>

 

File Attachment:

File Name: limesurvey...6(2).lss
File Size:20 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: RitaShen
The topic has been locked.
  • RitaShen
  • RitaShen's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 3 months ago #221821 by RitaShen
Dear,

I appreciate your help very much.
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose