shikharanideo / google-motion-charts-with-r

Automatically exported from code.google.com/p/google-motion-charts-with-r
0 stars 0 forks source link

checking if google visualization is already loaded #22

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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);
  }
}

Original issue reported on code.google.com by mark.mel...@savageminds.com on 24 Jan 2013 at 2:36

GoogleCodeExporter commented 8 years ago
Mark,

Could you please provide an example in R which demonstrates the issue and your 
solution.

Mant thanks

Markus

Original comment by markus.g...@googlemail.com on 24 Jan 2013 at 11:26

GoogleCodeExporter commented 8 years ago
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

Original comment by mark.mel...@savageminds.com on 25 Jan 2013 at 1:20

GoogleCodeExporter commented 8 years ago
Thanks Mark, I updated gvis() with your suggestion.

Original comment by markus.g...@googlemail.com on 26 Jan 2013 at 3:07

GoogleCodeExporter commented 8 years ago
Thanks Markus.

BTW it's a nice package you've created.

Original comment by mark.mel...@savageminds.com on 26 Jan 2013 at 3:44