亞利桑那州不顯示在地圖上的原因是因爲您必須將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/
漂亮!我喜歡你用普通的JS而不是jQuery寫的。是否可以在沒有任何數據的情況下啓動,並在開始時只顯示地圖,然後在選擇下拉選項時突出顯示一個狀態? – mvp
是的,你也可以這樣做:http://jsfiddle.net/asgallant/wwDyU/1/ – asgallant