when the spreadsheet loads,
use data table method getDistinctValues(index)
on the column
this will return a distinct list of the column values
then split each value on comma and build a unique list of the choices
use the choices for the values
option on the filter
then on the filter's 'statechange'
event,
use data table method getFilteredRows()
to find the row indexes
which contain the filter's selected choice(s)
use the row indexes to set a view on the chart
see following working snippet, a table chart is used...
google.charts.load('current', {
packages:['controls', 'corechart', 'table']
}).then(function () {
var sheet = 'https://docs.google.com/spreadsheets/d/1DOTezFuzpH8wzeh25Cgv9e9q577zd_HN1TiIxVQzbUQ/edit#gid=0';
var table = new google.visualization.ChartWrapper({
chartType: 'Table',
containerId: 'chart-table',
options: {
allowHtml: true
}
});
var filter = new google.visualization.ControlWrapper({
controlType: 'CategoryFilter',
containerId: 'filter-grade',
options: {
filterColumnIndex: 12,
matchType: 'any',
allowTyping: true,
ui: {
labelStacking: 'horizontal',
label: '',
caption: 'Filter by Grade Level(s)',
selectedValuesLayout: 'aside',
sortValues: false
}
}
});
new google.visualization.Query(sheet).send(function (response) {
var data = response.getDataTable();
var gradeAnswers = data.getDistinctValues(12);
var gradeChoices = [];
gradeAnswers.forEach(function (answer) {
if (answer !== null) {
var choices = answer.split(',');
choices.forEach(function (choice) {
choice = choice.trim();
if (gradeChoices.indexOf(choice) === -1) {
gradeChoices.push(choice);
}
});
}
});
gradeChoices.sort();
google.visualization.events.addListener(filter, 'statechange', function () {
var state = filter.getState().selectedValues;
table.setView(null);
if (state.length > 0) {
table.setView({
rows: data.getFilteredRows([{
column: 12,
test: function (value) {
var found = false;
if (value !== null) {
state.forEach(function (selected) {
if (value.indexOf(selected) > -1) {
found = true;
}
});
}
return found;
}
}])
});
}
table.draw();
});
filter.setDataTable(data);
filter.setOption('values', gradeChoices);
filter.draw();
table.setDataTable(data);
table.draw();
});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="filter-grade"></div>
<div id="chart-table"></div>
EDIT
you can set the view on the initial definition...
var table = new google.visualization.ChartWrapper({
chartType: 'Table',
containerId: 'chart-table',
options: {
allowHtml: true
},
view: {
columns: [0]
}
});
or use the setView
method...
table.setView({
columns: [0]
});
EDIT 2
var view = {
columns: [0]
};
if (state.length > 0) {
view.rows = data.getFilteredRows([{
column: 12,
test: function (value) {
var found = false;
if (value !== null) {
state.forEach(function (selected) {
if (value.indexOf(selected) > -1) {
found = true;
}
});
}
return found;
}
}]);
}
table.setView(view);
EDIT 3
you can combine multiple filters into an array,
before passing to getFilteredRows
var view = {
columns: [0]
};
var viewFilters = [];
if (stateGrade.length > 0) {
viewFilters.push({
column: 12,
test: function (value) {
var found = false;
if (value !== null) {
stateGrade.forEach(function (selected) {
if (value.indexOf(selected) > -1) {
found = true;
}
});
}
return found;
}
});
}
if (stateSubject.length > 0) {
viewFilters.push({
column: subjectIndex,
test: function (value) {
var found = false;
if (value !== null) {
stateSubject.forEach(function (selected) {
if (value.indexOf(selected) > -1) {
found = true;
}
});
}
return found;
}
});
}
view.rows = data.getFilteredRows(viewFilters);
table.setView(view);
values
property, as you have done, from all the rows in the table -- you would also need to unbind and handle the filter manually on the'statechange'
event – WhiteHat