【发布时间】:2015-09-17 14:53:47
【问题描述】:
我正在尝试使用谷歌图表事件侦听器来制作一些爆炸图表,例如饼图爆炸到一个表格,然后又爆炸到其他图表。[是否有其他方法可以做到这一点,感谢您的建议。 ]
方法 > 我有一个饼图,它在页面上正确显示,点击任何饼图它应该制作一个包含一些不同内容的表格(使用谷歌图表)。 (我对 php 页面的调用和返回的结果是正确的)但是表格图表以某种方式抛出错误。
我收到"container is not defined" 错误。我在网上找了这个,有人说我需要定义google.setOnLoadCallback,以便在调用之前加载所有divs。我试过了,但没有运气。我知道我在做一些愚蠢的事情,任何帮助将不胜感激。
这是我的 JS,我在我的 html 页面中定义了 m_user_div 和 'module_div'。
/*!ZZ
* Column chart for Most Used Modules.
* History
* 2015-Aug-10
*/
google.load('visualization', '1', {packages: ['corechart']});
google.setOnLoadCallback(mostUsedModules);
function mostUsedModules(param) {
var syshost = param.value;
var jsonChartData = $.ajax(
{
url: "include/mostUsedModules.php",
data: "sys="+syshost,
dataType:"json", async: false
}
).responseText;
// Create our data table out of JSON data loaded from server.
var ChartData = new google.visualization.DataTable(
jsonChartData);
// Define Chart Options .
var options = {
title: 'Most Used Modules (Run Time)',
pieHole: 0.4,
sliceVisibilityThreshold: 0.4/8,
};
// Instantiate and draw chart.
var chart = new google.visualization.PieChart(document.getElementById('module_div'));
chart.draw(ChartData, options);
// ***CHART IS CORRECTLY SHOWN ON THE PAGE***
// Add our selection handler.
google.visualization.events.addListener(chart, 'select', selectHandler);
function selectHandler(e) {
// grab a few details before redirecting
var selection = chart.getSelection();
var row = selection[0].row;
var col = selection[0].column;
var module = ChartData.getValue(row,0);
google.load('visualization', '1.1', {packages: ['table']});
google.setOnLoadCallback(tableList(module));
}
function tableList(module) {
//alert(ChartData.getValue(row,0) + ChartData.getValue(row,1));
var jsonTableData = $.ajax(
{
url: "include/userList.php",
data: "module="+module,
datatype: "json", async: false
}
).responseText;
// *** userList.php returns a valid data in json format ***
// Create our datatable out of Json Data loaded from php call.
var TableData = new google.visualization.DataTable(jsonTableData);
google.load('visualization', '1.1', {packages: ['table']});
var tab_options = {title: 'List of Users Used this Module',
showRowNumber: true,
alternatingRowStyle: true,
height: 20}
// Instantiate and Draw our Table
var table = new google.visualization.Table(document.getElementById('m_user_div'));
table.draw(TableData, tab_options);
//location.href = 'http://www.google.com?row=' + row + '&col=' + col;
}
我的 html 页面 -
<div class="row">
<div class="col-md-4 col-sm-6 col-xs-12">
<div class="dashboard_graph x_panel">
<div class="row x_title">
<div class="col-md-6">
<h2>Most Used Modules <small>Darter (2014-2015)</small>
<ul class="nav navbar-right panel_toolbox">
<li><a class="collapse-link" ><i class="fa fa-chevron-up"></i></a></li>
</ul>
<div class="clearfix"></div>
</h2>
</div>
</div>
<div class="x_content">
<!-- <script type="text/javascript" src="js/google/mostUsedModules.js">
</script> -->
<div id="module_div"></div>
</div>
</div>
</div> <!-- col-md -->
<div class="col-md-8 col-sm-6 col-xs-12">
<div class="dashboard_graph x_panel">
<div class="row x_title">
<div class="col-md-6">
<h2>Most Used Modules <small>Darter (2014-2015)</small></h2>
<ul class="nav navbar-right panel_toolbox">
<li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a></li>
</ul>
<div class="clearfix"></div>
</div>
</div>
<div class="x_content">
<script type="text/javascript" src="js/google/mostUsedModulesCompile.js">
</script>
<script type="text/javascript" src="js/google/mostUsedModules.js">
</script>
<div id="compile_module_div"></div>
<div id="m_user_div"></div>
</div>
</div>
</div> <!-- col-md -->
</div> <!-- row -->
【问题讨论】:
-
document.getElementById('m_user_div')返回什么?回调开头的同一行返回什么? -
我做了一个 'alert(document.getElementById('m_user_div'));它返回'null'
-
如果它在回调函数 ( mostUsedModules ) 的开头也返回 null 。我会确认 HTML 是否正确。
-
html中的m_user_div是否也设置为禁用?
-
我猜它与 div 有关,module_div 警报很好,但 m_user_div 返回 null。如何检查 m_user_div 设置为禁用。我对两个 div 使用完全相同的代码。
标签: javascript php google-visualization