问题是头部中的<base>标记。最简单的解决方案是删除它。更复杂的一个 - 使用回调函数来修复图表何时准备就绪:
<!DOCTYPE html>
<html>
<head>
<base href="/">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://www.google.com/jsapi"></script>
</head>
<body>
<div id="chart" style="height:500px;width:900px;"></div>
<script type="text/javascript">
google.load("visualization", "1", {
callback: function () {
function fixGoogleCharts(element) {
return function () {
$(element).find('svg').each(function () {
$(this).find("g").each(function () {
if ($(this).attr('clip-path')) {
if ($(this).attr('clip-path').indexOf('url(#') == -1)
return;
$(this).attr('clip-path', 'url(' + document.location + $(this).attr('clip-path').substring(4))
}
});
$(this).find("rect").each(function () {
if ($(this).attr('fill')) {
if ($(this).attr('fill').indexOf('url(#') == -1)
return;
$(this).attr('fill', 'url(' + document.location + $(this).attr('fill').substring(4))
}
});
});
};
}
var chartElement = document.getElementById('chart');
var data = google.visualization.arrayToDataTable([
['Country', 'Popularity'],
['Germany', 200],
['United States', 300],
['Brazil', 400],
['Canada', 500],
['France', 600],
['RU', 700]
]);
var options = {};
var chart = new google.visualization.GeoChart(chartElement);
// uncomment next string to fix geochart legend gradient
//google.visualization.events.addListener(chart, 'ready', fixGoogleCharts(chartElement));
chart.draw(data, options);
},
packages: ["geochart"]
});
</script>
</body>
</html>
这个 API 问题在这里讨论。一定要在调用之前放置事件监听器chart.draw