-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Google Charts not working within jsdom #1463
Comments
var jsdom = require("jsdom");
//TODO newscript way to load, can update someday
//http://www.gstatic.com/charts/loader.js
//google.charts.load('current', {'packages':['corechart']});
//google.charts.setOnLoadCallback(docharting);
jsdom.env({
html: `<div id="map" style="width: 900px; height: 500px;"></div><script src="http://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart']}]}"></script>`,
features: {
FetchExternalResources :["script", "link", "img"],
ProcessExternalResources: ["script"],
},virtualConsole: jsdom.createVirtualConsole().sendTo(console),
done: function (err, window) {
console.log(window.document.body.innerHTML)
if(err){
console.log("err: " + err);
return;
}else{
console.log("Initiating charting...");
var google = window.google;
console.log("Google visualization: " + google.visualization);
window.docharting = function(err, obj){
console.log("err " + JSON.stringify(err) + ", obj: " + obj);
console.log("Starting chart drawing...");
// Create the data table.
console.log(google.visualization)
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
['Mushrooms', 3],
['Onions', 1],
['Olives', 1],
['Zucchini', 1],
['Pepperoni', 2]
]);
// Set chart options
var options = {'title':'How Much Pizza I Ate Last Night',
'width':400,
'height':300};
d = window.document;
elm = d.getElementById('map');
console.log("Got map container " + el + ", starting chart rendering...");
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(elm);
google.visualization.events.addListener(chart, 'ready', function () {
console.log("Chart ready, capturing image uri");
imageURI = chart.getImageURI();
console.log(imageURI);
});
console.log("Data ready, starting drawing...");
chart.draw(data, options);
console.log("Done drawing");
}
if(google){
//google.load("visualization", "1", {packages:["corechart"]});
//google.setOnLoadCallback(function(){
// console.log("Google onload callback called");
// window.docharting();
//});
setTimeout(window.docharting, 4000);
}else{
console.log("Google apis did not initialize properly");
}
}
}
}); |
Thank you Sebmaster, this was very helpful, though I'm still not able to get end to end solution to work. The goal is using node and jsdom to use google charts API server side to generate chart images for emails and PDFs... To that end I took your changes and enhanced slightly...
Any ideas on how to test if chart actually working or why chart ready event not firing properly, and also why the program isn't just hanging waiting for my promise to resolve like I'd expect... Thank you!
|
I had a similar problem that I solved by using the new Google library loader, hacking up a quick patch for issue #1495 and building node with full internationalization support. Here's how I did it. The first problem is how the Google library loader chains resource loading. It creates an element with a After resolving that issue with a quick hack, the code to create a chart is pretty straightforward: var jsdom = require('jsdom');
jsdom.env({
html: '<html><head></head><body><div id="chart_div"></div></body></html>',
scripts: ["https://www.gstatic.com/charts/loader.js"],
features: {
FetchExternalResources: ["script", "link"],
ProcessExternalResources: ["script"],
},
done: function(err, window) {
var document = window.document;
var google = window.google;
// 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(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
['Mushrooms', 3],
['Onions', 1],
['Olives', 1],
['Zucchini', 1],
['Pepperoni', 2]
]);
// Set chart options
var options = {'title':'How Much Pizza I Ate Last Night',
'width':400,
'height':300};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
google.visualization.events.addListener(chart, 'ready', function () {
// Do something with the chart here
console.log('chart ready');
});
chart.draw(data, options);
}
}
}); The other issue I ran into was Node by default is not built with full internationalization support. If you get this fatal error:
Then building node as described here should solve it. |
I have made a repo for generating google chart images on node if you are interested in. |
Hi,
I read online many examples of using jsdom to do server side charting. I can't seem to get it to work with Google Charts though. I'm using the latest 8.4.0 build and I've tried a few variations like auto loading the full google visualization api or using the recommended load base api script first then visualization. Both had issues, code is below, commented parts is for other way.
Trying to load it sync all in script tag using googles autoload way gives this error:
var data = new google.visualization.DataTable();
TypeError: google.visualization.DataTable is not a function
If I change it to use the google.load after jsdom gives done event it just exists and I get no details
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(function(){
console.log("Google onload callback called");
window.docharting();
});
full code:
The text was updated successfully, but these errors were encountered: