I am trying to handle the click event and using a JavaScript function to alert. My code works perfectly fine. But is there a better way to handle the drill down? Also in my code now to display the drill down graph, whats the good way to do it?
<?php header('Content-type: text/html; charset=utf-8');
$link = mysql_connect('DEMO', 'root', 'demo');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('bi_test',$link) or die ("could not open db".mysql_error());
$query = mysql_query("SELECT * FROM drilldown");
$table = array();
$table['cols'] = array(
array('label' => 'Weekly Task', 'type' => 'string'),
array('label' => 'Percentage', 'type' => 'number'),
array('label' => 'Productive', 'type' => 'string')
);
$rows = array();
while($r = mysql_fetch_assoc($query)) {
$temp = array();
$temp[] = array('v' => (string) $r['weekly_task']);
$temp[] = array('v' => (int) $r['percentage']);
$temp[] = array('v' => (string) $r['productive']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
//echo $jsonTable;
mysql_close($link);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<!--Load the AJAX API-->
<script type="text/javascript">
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
var chart;
var previousSelection;
function drawChart() {
var data = new google.visualization.DataTable(<?php echo $jsonTable; ?>);
data.addColumn({'type': 'string', 'role': 'tooltip', 'p': {'html': true}});
var options = {
title: 'My Productive Weekly Plan',
isStacked: true,
is3D: 'true',
width: 800,
height: 600,
focusTarget: 'category',
tooltip: { isHtml: true }
};
var result = google.visualization.data.group(
data,
[2],
[{'column': 1, 'aggregation': google.visualization.data.sum, 'type': 'number'}]);
chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(result, options);
google.visualization.events.addListener(chart, 'select', selectHandler);
}
function selectHandler() {
var selection = chart.getSelection();
var sliceName;
var item = selection[0];
if( item.row==0)
{
alert('ON click display NO');
sliceName = "Not Productive";
}
else if( item.row==1)
{
alert('ON click display YES');
sliceName = "Productive";
}
var options = {
title: sliceName,
isStacked: true,
is3D: 'true',
width: 800,
height: 600,
focusTarget: 'category',
tooltip: { isHtml: true }
};
//chart.draw(previousSelection, options);
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>