亚利桑那州没有出现在地图上的原因是您必须将resolution
选项设置为“省”才能获得州地图。使用下拉菜单突出显示选定的状态有点复杂,但肯定是可行的。这是您可以做到的一种方法;在你的 javascript 中:
function drawChart () {
var data = google.visualization.arrayToDataTable([
['State', ''],
['US-AK', 0],
['US-AZ', 0],
['US-HI', 0]
]);
var geochart = new google.visualization.GeoChart(document.getElementById('chart_div'));
var options = {
region:"US",
legend:"none",
width: 556,
height: 347,
resolution: 'provinces',
colorAxis: {
minValue: 0,
maxValue: 1,
colors: ['green', 'red']
}
};
var stateSelector = document.querySelector('#state');
function updateChart () {
var index = this.selectedIndex;
var selectedState = this.options[index].value;
var view = new google.visualization.DataView(data);
view.setColumns([0, {
type: 'number',
calc: function (dt, row) {
return (dt.getValue(row, 0) == selectedState) ? 1 : 0;
}
}]);
geochart.draw(view, options);
}
if (document.addEventListener) {
stateSelector.addEventListener('change', updateChart, false);
}
else if (document.attachEvent) {
stateSelector.attachEvent('onchange', updateChart);
}
else {
stateSelector.onchange = updateChart;
}
geochart.draw(data, options);
}
google.load('visualization', '1', {packages:['geochart'], callback: drawChart});
然后在您的 HTML 中:
<select id="state">
<option value="" selected="selected">Select a state to highlight</option>
<option value="US-AK">Alaska</option>
<option value="US-AZ">Arizona</option>
<option value="US-HI">Hawaii</option>
</select>
<div id="chart_div"></div>
这是一个你可以玩的jsfiddle:http: //jsfiddle.net/asgallant/wwDyU/