Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Charts don't appear if loaded via AJAX
Proposed resolution
See latest patch, changing from
google.setOnLoadCallback(drawChart);
to
google.setOnLoadCallback(drawChart, true);
But this solves the issue only partially. It will work only for the first reload using Ajax.
Remaining tasks
User interface changes
None.
API changes
None.
Original report by @albach
Hi, I've been using google chart tools for all our charts in our internal site as static pages, when we wanted to add ajax, we found that by calling the same draw_chart no chart is drawn. How can we achieve the ajax load of the chart being either via new load of the object or updated of the data?
Comment | File | Size | Author |
---|---|---|---|
#13 | google_chart_tools_(modification).zip | 2.52 KB | Rolxxc |
#11 | google-chart-tools-ajax-1613258-11-7.x-1.4.patch | 6.72 KB | grom358 |
#9 | ajaxload-1613258-9.patch | 1.21 KB | bluetegu |
#3 | ajaxload-1613258-3.patch | 568 bytes | iLLin |
Comments
Comment #1
btopro CreditAttribution: btopro commentedHave noticed this issue as well, can't figure out why though.
Comment #2
iLLin CreditAttribution: iLLin commentedI had this issue tonight and this is the line that needs to change:
open google_chart_tools.js and on line 12 change
google.setOnLoadCallback(drawChart);
google.setOnLoadCallback(drawChart, true);
That will tell google to load the function on DOM ready instead of Window Ready. Its late, I will get a patch in the morning.
Comment #3
iLLin CreditAttribution: iLLin commentedPatch added.
Comment #4
dshields CreditAttribution: dshields commentedDoes anyone know if its possible to get this working via Views integration?
I'm trying to use exposed filters and finding that no chart will load on submit
Comment #5
herd45 CreditAttribution: herd45 commentedSame problem as #4
Comment #6
rgrms CreditAttribution: rgrms commentedI know that last response here was around a year ago, but anyway I've figured it out.
As exposed form submit fires an ajaxComplete event, you have to add a javascript handler for it. In order to avoid unpredictable bechavior, you have to specify selector that related to view, however, it will work even with 'body' selector.
place it in google_chart_tools.js
Generally, using ajaxComplete handler allows to do anything with every exposed filter form submit, not just the charts.
Comment #7
k_zoltan CreditAttribution: k_zoltan commentedI had the same issue as people mentioning above.
Have a view with exposed filters using block display and since this issue https://www.drupal.org/node/690748#comment-6664022
The solution is in the patch attached to this issue in the comment #3
This is enough for solving the issue when you need to load a chart dynamically using AJAX eventually using Views.
Tested with latest Drupal, Views and Google Chart Tools
Comment #8
k_zoltan CreditAttribution: k_zoltan commentedThis solves the issue only partially. It will work only for the first reload using Ajax.
Comment #9
bluetegu CreditAttribution: bluetegu commentedFor some reason the solution of #3 worked for me in Chrome but not in FF. I was loading charts via ajax with ajaxblocks module. Anyhow, what worked for me was to replace setOnLoadCallback with specifying the callback in the load function. See attached patch. I didn't test it with the other cases mentioned above.
Comment #10
grom358 CreditAttribution: grom358 commentedIn porting Google Analytics Reports module it uses a custom ajax callback and render a chart. So what I did was change the javascript so could call drawCharts with the settings for that chart.
Comment #11
grom358 CreditAttribution: grom358 commentedUploaded the patch again following patch naming conventions.
Comment #12
xtfer CreditAttribution: xtfer commentedComment #13
Rolxxc CreditAttribution: Rolxxc commentedHello, I read a lot about the issues module, and I have a proposal.
the problem is that the module executes a merge of the information in the datatable. however the php contains the information you want to graph from the server. To avoid merge information, this data is sent to the js.
Performing several inspections, I added the following code after line 23 on google_chart_tools.module:
// Line 23
drupal_add_js (drupal_get_path ('module', 'google_chart_tools') '/google_chart_tools.js.');
// Extra code
$ Key = array_keys ($ settings ["chart"]);
$ Num = (count ($ settings ["chart"] [$ key [0]] [header]));
//existing attach code goes here
...
//end of existing code
---------------------------------------------------------------------------------------------------------------
In google_chart_tools.js after the attach the following code:
// Line 12
attach: function (context, settings) {
// Extra Code,
var val_php = settings.google_chart_tools.val;
var key = settings.google_chart_tools.arr;
google.setOnLoadCallback (drawChart, true);
//existing attach code goes here
...
//end of existing code
---------------------------------------------
And finally this, after this code:
var options = settings.chart [ChartID] .options;
// Extra code
if (key == settings.chart [ChartID] .containerId) {
var t_row = data.getNumberOfRows();
var rest = (t_row-val_php);
data.removeRows (val_php, rest);
}
//existing attach code goes here
...
//end of existing code
---------------------
note: the partial solution of google.setOnLoadCallback (drawChart, true); enables the graphic display after called by ajax. good!
I'm not good with grammar, sorry for this, I hope it's work for you, please comments.
Comment #14
kedramonHi,
I found that, if we remove the google.setOnLoadCallback function, and just call the drawChart function it will work in ajax mode.
Looks like we need to check somehow if the google Visualization API is loaded.
Comment #15
govind.maloo CreditAttribution: govind.maloo at Salsa Digital commentedpatch #11 is working perfectly and solved mentioned issue.
https://github.com/govCMS/govCMS is using the same patch in prod env as well.