I have a simple Google visualization dashboard with following code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>
Google Visualization API Sample
</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1.1', {packages: ['controls']});
</script>
<script type="text/javascript">
function drawVisualization() {
// Prepare the data
var data = google.visualization.arrayToDataTable([
['Name', 'Gender', 'Age', 'Donuts eaten'],
['Michael' , 'Male', 12, 5],
['Elisa', 'Female', 20, 7],
['Robert', 'Male', 7, 3],
['John', 'Male', 54, 2],
['Jessica', 'Female', 22, 6],
['Aaron', 'Male', 3, 1],
['Margareth', 'Female', 42, 8],
['Miranda', 'Female', 33, 6]
]);
// Define a category picker control for the Gender column
var categoryPicker = new google.visualization.ControlWrapper({
'controlType': 'CategoryFilter',
'containerId': 'control1',
'options': {
'filterColumnLabel': 'Gender',
'ui': {
'labelStacking': 'vertical',
'allowTyping': false,
'allowMultiple': false
}
}
});
// Define a table
var table = new google.visualization.ChartWrapper({
'chartType': 'Table',
'containerId': 'chart1',
'options': {
'width': '300px'
}
});
// Create a dashboard
new google.visualization.Dashboard(document.getElementById('dashboard')).
bind(categoryPicker,table).
// Draw the entire dashboard.
draw(data);
}
google.setOnLoadCallback(drawVisualization);
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<div id="dashboard">
<table>
<tr style='vertical-align: top'>
<td style='width: 300px; font-size: 0.9em;'>
<div id="control1"></div>
</td>
<td style='width: 600px'>
<div style="float: left;" id="chart1"></div>
</td>
</tr>
</table>
</div>
</body>
</html>
I want to access the categoryfilter using an if condition in my javascript (assume I have 5 category filters bound each other). That is, I want something like
function myfunction() {
var whereClauses = [];
if (categorypicker1.statechange) {
whereClauses.push("something1 = '" + categorypicker1.changedstate + "'")
}
if (categorypicker2.statechange) {
whereClauses.push("something2 = '" + categorypicker2.changedstate + "'")
}
whereClause = whereClauses.join(" AND ");
//do something...
}
But I unable to get the correct format. So what is the correct format to use if condition?
Make the
categoryPicker
object accessible outside thedrawVisualization
function, and then use thegetState().selectedValues
value:Aside from a "statechange" event handler, there is no way to tell when a control has changed. If you need to execute some code when a control changes, this is how you would do it:
If, however, so you want to execute some code when any of the related controls change, then you can create a function and pass the function to the event handler:
If the function needs to know which control changed, then you can do something like this: