在插入 Javascript 选择更改函数时遇到问题 谷歌可视化.
Having trouble inserting Javascript select onchange function for Google Visualization
我是JS编程的新手。我想做一个选择选项,我可以在其中更改图表类型,例如从线条到条形。数据源来自谷歌电子表格。JS代码如下:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>
Select Chart Type
</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['linechart',corechart','barchart', 'columnchart']});
</script>
<script type="text/javascript">
var graphview;
function init() {
graphview = new google.visualization.ChartWrapper({
dataSourceUrl: 'https://docs.google.com/spreadsheet/tq?tq=select%20A,B......etc
containerId: 'visualization1', chartType: 'LineChart'
graphview.draw();
}
google.setOnLoadCallback(init);
}
</script>
</head>
<body>
<div id="panelgraph", width="600", height="200">
</body>
</html>
选项如下:
<select id="graphbox" name="graphbox" onChange="getChart()">
<option value="Line">Line Chart </option>
<option value="Bar">Bar Chart</option>
<option value="Core">Area Chart</option>
<option value="Column">Column Chart</option>
</select>
我尝试创建一个函数,但不确定如何以及在何处插入它们:
var mygraphbox = document.getElementById("graphbox");
var myinsertgraphhere = document.getElementById("panelgraph");
mygraphbox.onchange =function()
我遵循一些如何使onchange选择选项功能的示例。但是当我想将其应用于我的情况时(例如,使用 if-else 并绘制地图),我遇到了麻烦。请帮助我如何制定它以及如何将其插入 JS 块中。
提前谢谢你,店
这是一个快速的解决方案;将你的javascript更改为:
function init () {
var graphview = new google.visualization.ChartWrapper({
chartType: 'LineChart',
containerId: 'visualization1',
dataSourceUrl: 'https://docs.google.com/spreadsheet/tq?key=<key>&gid=0',
options: {
// chart options
},
query: 'select a, b, c'
});
graphview.draw();
function changeHandler () {
graphview.setChartType(this.options[this.selectedIndex].value);
graphview.draw();
}
var mygraphbox = document.querySelector("#graphbox");
if (typeof(window.addEventListener) == 'function') {
mygraphbox.addEventListener('change', changeHandler, false);
}
else if (typeof(window.attachEvent) == 'function') {
mygraphbox.attachEvent('onchange', changeHandler);
}
}
google.load('visualization', '1', {packages: ['corechart'], callback: init});
并像这样更改选择框:
<select id="graphbox">
<option value="LineChart">Line Chart </option>
<option value="BarChart">Bar Chart</option>
<option value="AreaChart">Area Chart</option>
<option value="ColumnChart">Column Chart</option>
</select>
这样,您可以更改图表类型而无需重新提交查询。
相关文章:
- 将PHP变量传递给jQuery时遇到问题
- 将jQuery转换为用户脚本的普通Javascript时遇到问题
- 将子文档推送到父数组时遇到问题
- JQuery FullCalendar在从ajax成功调用rerenderEvents时遇到问题
- 在将javascript附加到我的html中时遇到问题
- 为什么我在以编程方式打开窗口时遇到问题
- 在将缩略图链接到模态时遇到问题
- 使用 ng 模型获取数据时遇到问题
- 是否有其他人在设计Twitter Typeahead时遇到问题'的搜索栏
- 使用jquery进行整数比较时遇到问题
- 使用PhantomJS下载动态web内容时遇到问题
- 设置RequireJS时遇到问题
- 让Javascript在SharePoint 2010上运行时遇到问题
- I'我在react.js中编写for循环时遇到问题
- 通过jQuery获取上传的文件名时遇到问题
- 我在启动我的节点时遇到问题.js IRC 机器人
- Ruby on Rails 4:在 Rails Web 应用程序中添加 Javascript 文件时遇到问题
- 从谷歌地图自定义控件打开离子模态时遇到问题
- 在使用 YAML 和 Google App Engine 时遇到问题
- 在 Chrome 扩展程序中将鼠标悬停在广告上使用时遇到问题