我需要帮助对HTML页面进行编码,以便当您从下拉菜单中选择“C152”选项时(请参阅下面的代码),Google图表将更改为C152图表,当您从中选择“C172”时下拉菜单,图表将更改为C172图表。基本上,图表将根据下拉菜单中的选择进行更改。 (每个图表都有一组不同的数据点和选项。)我已经做了一段时间的研究,但没有找到我想要做的工作。我希望有人能指出我正确的方向。这是我的代码示例。提前致谢
<!DOCTYPE html>
<html>
<head>
<th>
<select name='select1' >
<option selected disabled>Choose</option>
<option onclick="c152()" value="c152">C152</option>
<option onclick="c172()" value="c172">C172</option>
</select></th>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var datac152 = google.visualization.arrayToDataTable
([['X', 'Y'],
[31, 1000],
[31, 1350],
[32.65, 1670],
[36.5, 1670],
[36.5, 1000]
]);
var datac172 = google.visualization.arrayToDataTable
([['X', 'Y'],
[35, 1500],
[35, 1950],
[38.65, 2300],
[47.3, 2300],
[47.3, 1500]
]);
var optionsc152 = {
legend: 'none',
hAxis: {title: 'Center of Gravity (inches)', minValue: 30, maxValue: 38 },
vAxis: {title: "Total Weight (lbs)"},
axes: {
y: {
all: {
range: {
max: 1800,
min: 1000
}
}
}
},
colors: ['#009933'],
pointSize: 0,
title: 'Cessna 152 Load Limits',
backgroundColor: '#eeeeee',
pointShape: 'circle'
};
var optionsc172 = {
legend: 'none',
hAxis: {title: 'Center of Gravity (inches)', minValue: 34, maxValue: 48 },
vAxis: {title: "Total Weight (lbs)"},
axes: {
y: {
all: {
range: {
max: 2300,
min: 1500
}
}
}
},
colors: ['#009933'],
pointSize: 0,
title: 'Cessna 172 Load Limits',
backgroundColor: '#eeeeee',
pointShape: 'circle'
};
var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
chart.draw(datac152, optionsc152);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 963px; height: 500px;"></div>
</body>
</body>
</html>
答案 0 :(得分:0)
不使用onclick选项,而是在select标签上使用onchange 获取选定的值并将其传递给chart.draw()
HTML:
<html>
<head>
<th>
<select id="chart" name='select1' onchange="change()">
<option selected disabled>Choose</option>
<option value="c152">C152</option>
<option value="c172">C172</option>
</select></th>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
</head>
<body>
<div id="chart_div" style="width: 963px; height: 500px;"></div>
</body>
</body>
</html>
JS:
google.charts.load("current", {
packages: ["corechart"]
});
function change() {
var listbox = document.getElementById("chart");
var selIndex = listbox.selectedIndex;
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;
console.log(selValue);
google.charts.setOnLoadCallback(drawChart);
function drawChart(x, y) {
var datac152 = google.visualization.arrayToDataTable([
['X', 'Y'],
[31, 1000],
[31, 1350],
[32.65, 1670],
[36.5, 1670],
[36.5, 1000]
]);
var datac172 = google.visualization.arrayToDataTable([
['X', 'Y'],
[35, 1500],
[35, 1950],
[38.65, 2300],
[47.3, 2300],
[47.3, 1500]
]);
var optionsc152 = {
legend: 'none',
hAxis: {
title: 'Center of Gravity (inches)',
minValue: 30,
maxValue: 38
},
vAxis: {
title: "Total Weight (lbs)"
},
axes: {
y: {
all: {
range: {
max: 1800,
min: 1000
}
}
}
},
colors: ['#009933'],
pointSize: 0,
title: 'Cessna 152 Load Limits',
backgroundColor: '#eeeeee',
pointShape: 'circle'
};
var optionsc172 = {
legend: 'none',
hAxis: {
title: 'Center of Gravity (inches)',
minValue: 34,
maxValue: 48
},
vAxis: {
title: "Total Weight (lbs)"
},
axes: {
y: {
all: {
range: {
max: 2300,
min: 1500
}
}
}
},
colors: ['#009933'],
pointSize: 0,
title: 'Cessna 172 Load Limits',
backgroundColor: '#eeeeee',
pointShape: 'circle'
};
var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
if (selValue == 'c152') {
x = datac152;
y = optionsc152;
}
if (selValue == 'c172') {
x = datac172;
y = optionsc172;
}
chart.draw(x, y);
}
}
Codepen - http://codepen.io/nagasai/pen/RRjLxL