| Issue 22: | checking if google visualization is already loaded | |
| 1 person starred this issue and may be notified of changes. | Back to list |
In the javascript code generated (at least with the gadget code I looked at), there is something like:
// jsDisplayChart
function displayChartColumnChartID15fa166ce74()
{
google.load("visualization", "1", { packages:["corechart"] });
google.setOnLoadCallback(drawChartColumnChartID15fa166ce74);
}
The possible problem here is if google.visualization is already loaded then the callback won't be called.
You could have something like this to cater for that situation:
// jsDisplayChart
function displayChartColumnChartID15fa166ce74()
{
if (typeof google === "object" && typeof google.visualization === "object") {
drawChartColumnChartID15fa166ce74();
} else {
google.load("visualization", "1", { packages:["corechart"] });
google.setOnLoadCallback(drawChartColumnChartID15fa166ce74);
}
}
Jan 24, 2013
Project Member
#1
markus.g...@googlemail.com
Jan 25, 2013
Hi Markus,
Sorry, I probably wasn't very clear.
I just created a Google gadget as per the code below.
Column <- gvisColumnChart(df,
options=list(legend='none', width=300,
height=200))
plot(Column)
gadget <- createGoogleGadget(Column)
I used the createGoogleGadget function because I wanted an XML formatted
content, which I then return directly to the browser.
The javascript code in the browser accepts this gadget xml and gets the
contents of the the "Content" tag, which contains the html and javascript
code.
I then do a $('<id of dom element>').replaceWith(<html & javascript code
from Content tag>) and the chart is displayed on the page. Or at least it
would be if the function drawChartColumnChartID15fa166ce74() was called.
But it's not called because the google visualisation library is already
loaded (and drawChartColumnChartID15fa166ce74() is only called in the
callback of google.load)
My suggested fix is in gvis.R, where I've altered the function below to
generate the javascript code to check if google.visualization is already
loaded and if so call it.
jsDisplayChart <- '
// jsDisplayChart
function displayChart%s()
{
if (typeof google === "object" && typeof google.visualization === "object") {
drawChart%s();
} else {
google.load("visualization", "1", { packages:["%s"] %s});
google.setOnLoadCallback(drawChart%s);
}
}
'
jsDisplayChart <- sprintf(jsDisplayChart, chartid, chartid,
ifelse(!is.null(options$gvis$gvis.editor),'charteditor',
tolower(package)),
ifelse(!is.null(options$gvis$gvis.language),
paste(",'language':'",
options$gvis$gvis.language, "'", sep=""), ''),
chartid
)
Best regards
Mark
Jan 26, 2013
Thanks Mark, I updated gvis() with your suggestion.
Status:
Fixed
Jan 26, 2013
Thanks Markus. BTW it's a nice package you've created. |