cmen / CMENGoogleChartsBundle

This Bundle provides a Twig extension and PHP objects to display Google charts in your Symfony application.
https://developers.google.com/chart/
Other
75 stars 21 forks source link

Update chart in twig template after AJAX request not displaying #42

Open Luka2010 opened 4 years ago

Luka2010 commented 4 years ago

I try since weeks to update the chart via an AJAX request. I use tabs which send of the AJAX request In my normal view I have the following function:

 $('#country-tabs a').on('click', function (e) {
            e.preventDefault()
            $(this).tab('show');
            var $this = $(this),
            loadurl = $this.attr('href');

            var form = $('form');

            var jsonData = $.ajax({
                url: loadurl,  
                type:       'POST',
                data: form.serializeArray(),
                dataType:   'json',  
                async: false,

                success: function(data, status) {              
                    $("div#client-loop-container").html(data.chartview); 
                    $('#status').addClass('status-bar-success');
        }
          }).responseText;

        });

My controller then returns

if ($request->isXmlHttpRequest())
        {   
          $result = $this->renderView('fruits/chart.html.twig', [
                  'fruitCounts' => $fruitCounts,
                  'barchart' => $barchart
            ]);
           return new JsonResponse(['success' => true, 'chartview' => $result]);
     }

My chart.twig.html then has the following:

    <div id="client-loop-container">
        <div class="w-100" id="div_chart"></div>
         {{  dump(barchart) }}      
         {% block javascripts %}
             <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
            <script type="text/javascript">          
              {{ gc_draw(barchart, 'div_chart') }}
            </script>
        {% endblock %}

dump(barchart) Shows CMEN\GoogleChartsBundle\GoogleCharts\Charts\LineChart with the updated data, but I do not get any Chart displayed. What am I missing here? Is it a bug, do I use it wrong or is it simply not possible to do?

chekinih commented 4 years ago

I have the same issue