我正在尝试将谷歌图表生成的图表保存为 png 图像。该代码适用于除 GeoChart 之外的所有图表。图像有时会出现,但通常只是空白。这是代码。
渲染.js
var system = require('system');
var page = require('webpage').create();
page.open('chart.html, function () {
page.paperSize = { format: 'A4', orientation: 'landscape'};
page.render(system.args[1]);
phantom.exit();
});
图表.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Chart Generation</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {
var data = google.visualization.arrayToDataTable([
['Country', 'Popularity'],
['Germany', 200],
['United States', 300],
['Brazil', 400],
['Canada', 500],
['France', 600],
['RU', 700]
]);
var options = {
width: 400,
height: 200
};
var chart = new google.visualization
.GeoChart(document.getElementById('chart_div'));
chart.draw(data, options);
};
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
终端中的 Usgae :
phantomjs render.js chart.png