多个谷歌图表上的事件
Event on multiple google charts
我有一个问题,当为谷歌图表使用select事件时,它只为该事件使用一个图表(for循环中的最后一个图表)。有没有办法将同一事件绑定到多个图表上?
这是我的代码:您可以看到,我为每个饼图添加了一个选择侦听器,但只有最后一个饼图(页面上)会发出警报。
<!DOCTYPE html>
<head>
<?php require_once 'header.php'; ?>
<?php
class typeChart {
public $type;
public $function;
public $addit;
public $title;
public $jscript;
public function __construct($type, $string, $title, $additional = 0) {
$this->type = $type;
$this->function = "report/" . $string;
$this->addit = $additional;
$this->title = $title;
$this->jscript = $string;
}
}
$arr = array();
array_push($arr, new typeChart('table', 'getDataProjects', 'Data Projects'));
array_push($arr, new typeChart('pie', 'getStatusProjects', 'Status Projects', 1));
array_push($arr, new typeChart('pie', 'getTypeProjects', 'Type Projects', 1));
array_push($arr, new typeChart('pie', 'getBillingProjects', 'Billing Projects', 1));
array_push($arr, new typeChart('pie', 'getTypeFinance', 'Type Finance'));
array_push($arr, new typeChart('pie', 'getTypeInterventions', 'Type Interventions'));
array_push($arr, new typeChart('bar', 'getCompletedYear', 'Completed Year'));
array_push($arr, new typeChart('pie', 'getLeadTimeProjects', 'LeadTime Projects'));
array_push($arr, new typeChart('pie', 'getDeadlineInvoicing', 'Deadline Invoicing'));
?>
<script type="text/javascript">
google.load("visualization", "1", {packages: ["corechart,table"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
<?php
$counter = 0;
?>
<?php foreach ($arr as $res) { ?>
<?php
$counter++;
$datatable = 'dataTable' . $counter;
?>
function selectHandler(myChart) {
var selectedItem = myChart.getSelection()[0];
if (selectedItem) {
var topping = <?php echo $datatable; ?>.getValue(selectedItem.row, 0);
alert(topping);
}
}
var jsonData = $.ajax({
url: "<?php echo site_url($res->function); ?>",
dataType: "json",
async: false
}).responseText;
var <?php echo $datatable; ?> = new google.visualization.DataTable(jsonData);
var options = {
title: '<?php echo $res->title; ?>'
};
$("#charts").append('<table>');
<?php if ($res->type == 'table') { ?>
$("#charts").append('<tr><td><div id="<?php echo $res->title; ?>" style="width: 700px; height: 300px; margin: 0 auto;"></div></td></tr>');
var <?php echo $res->jscript; ?> = new google.visualization.Table(document.getElementById('<?php echo $res->title; ?>'));
google.visualization.events.addListener(<?php echo $res->jscript; ?>, 'select', function() {
selectHandler(<?php echo $res->jscript; ?>);
});
<?php echo $res->jscript; ?>.draw(<?php echo $datatable; ?>, options);
<?php } ?>
<?php if ($res->type == 'pie') { ?>
$("#charts").append('<td><div id="<?php echo $res->title; ?>" style="width: 700px; height: 300px;"></div></td>');
var <?php echo $res->jscript; ?> = new google.visualization.PieChart(document.getElementById('<?php echo $res->title; ?>'));
google.visualization.events.addListener(<?php echo $res->jscript; ?>, 'select', function() {
selectHandler(<?php echo $res->jscript; ?>);
});
<?php echo $res->jscript; ?>.draw(<?php echo $datatable; ?>, options);
<?php
if ($res->addit == 1) {
$bar = $res->title . 'Bar';
?>
$("#charts").append('<td><div id="<?php echo $bar; ?>" style="width: 700px; height: 300px;"></div></td></tr>');
var x = new google.visualization.ColumnChart(document.getElementById('<?php echo $bar; ?>'));
x.draw(<?php echo $datatable; ?>, options);
google.visualization.events.addListener(<?php echo $res->jscript; ?>, 'select', function() {
selectHandler(<?php echo $res->jscript; ?>);
});
<?php } else { ?>
<?php } ?>
<?php } ?>
<?php } ?>
$("#charts").append('</table>');
}
</script>
</head>
<body>
<div id='cssmenu'>
<ul>
<li><a href="<?php echo site_url('home/index'); ?>"><span>HOME</span></a></li>
<li><a href="<?php echo site_url('manage/index'); ?>"><span>MANAGE</span></a></li>
<li class='active'><a href="<?php echo site_url('report/index'); ?>"><span>REPORT</span></a></li>
<li style="float: right"><a href="<?php echo site_url('login/logout'); ?>"><span>LOGOUT</span></a></li>
</ul>
</div>
<table id="charts">
</table>
</body>
</html>
提前感谢
在多个图表中使用单个事件处理程序并不困难,只需要作弊一点。给定此事件处理程序:
function selectHandler(myChart) {
var selectedItem = myChart.getSelection()[0];
if (selectedItem) {
var topping = data.getValue(selectedItem.row, 0);
alert(topping);
}
}
您需要使用一个自定义函数来设置事件,该函数调用selectHandler
并在图表中传递:
google.visualization.events.addListener(myChart1, 'select', function () {
selectHandler(myChart1);
});
google.visualization.events.addListener(myChart2, 'select', function () {
selectHandler(myChart2);
});
您的代码会有点复杂,因为您不仅有多个图表,而且还有多个DataTables(在当前版本的代码中被覆盖)。您需要修复变量命名,然后将DataTable和图表作为参数传递给selectHandler
。
相关文章:
- 在谷歌地图上获取事件的x,y坐标
- 谷歌地图劫持了iphone's滚动(触摸事件)-如何恢复
- 当谷歌地图进入街景时,我能抓住什么事件
- 谷歌地图动态创建的标记点击事件使用相同的标记
- 如何在多承诺链中处理谷歌地图API V3事件
- 将谷歌地图点击事件中的LatLng传递到rails控制器
- 在JQuery中使用谷歌地图Api事件/侦听器
- 点击事件在谷歌地图和AJAX
- Video.js+移动设备上的谷歌IMA:'点击'导致错误的事件
- 使用谷歌图表将mousedown事件注册为click
- 信息窗口谷歌地图点击事件给出错误
- 从谷歌分析中获取所有事件
- 谷歌分析事件跟踪
- 谷歌分析跟踪内部链接不正常工作的事件
- 完整的日历错误时间在点击事件谷歌日历
- 重叠圆圈阻止鼠标悬停事件-谷歌地图v3
- 显示谷歌地图点击事件 - 谷歌地图API
- 避免在地图平移时自定义叠加点击事件(谷歌地图 API)
- 简单点击事件谷歌地图
- 页面浏览量作为事件谷歌分析