google.charts.load('current', {
callback: drawChart,
packages:['controls']
});
function drawChart() {
var dataTableSales = google.visualization.arrayToDataTable([
['Sale Date', 'Sale Type'],
[new Date(2016, 0, 16), 'cash sale'],
[new Date(2016, 0, 16), 'cash sale'],
[new Date(2016, 0, 16), 'leased'],
[new Date(2016, 0, 16), 'leased'],
[new Date(2016, 0, 16), 'financed'],
[new Date(2017, 0, 16), 'cash sale'],
[new Date(2017, 0, 16), 'cash sale'],
[new Date(2017, 0, 16), 'cash sale'],
[new Date(2017, 0, 16), 'financed'],
[new Date(2016, 0, 17), 'cash sale'],
[new Date(2016, 0, 17), 'financed'],
[new Date(2016, 0, 17), 'cash sale'],
[new Date(2016, 0, 17), 'leased'],
[new Date(2016, 0, 17), 'financed'],
[new Date(2017, 0, 17), 'financed'],
[new Date(2017, 0, 17), 'financed'],
[new Date(2017, 0, 17), 'cash sale'],
[new Date(2017, 0, 17), 'financed'],
[new Date(2016, 0, 18), 'leased'],
[new Date(2016, 0, 18), 'cash sale'],
[new Date(2017, 0, 18), 'cash sale'],
[new Date(2017, 0, 18), 'cash sale']
]);
dataTableSales.sort({column: 0});
var dataByYear = google.visualization.data.group(
dataTableSales,
[{
column: 0,
type: 'string',
modifier: function (value) {
return value.getFullYear().toString();
}
}, 1],
[{
column: 1,
type: 'number',
aggregation: google.visualization.data.count
}]
);
var dataAll = google.visualization.data.group(
dataTableSales,
[1],
[{
column: 1,
type: 'number',
aggregation: google.visualization.data.count
}]
);
var yearPicker = new google.visualization.ControlWrapper({
controlType: 'CategoryFilter',
containerId: 'categoryFilter_div',
dataTable: dataByYear,
options: {
filterColumnIndex: 0,
ui: {
allowTyping: false,
allowMultiple: false,
caption: 'All Years',
label: '',
labelStacking: 'vertical'
},
useFormattedValue: true
}
});
google.visualization.events.addListener(yearPicker, 'statechange', function() {
if (yearPicker.getState().selectedValues.length > 0) {
pieChart.setView({
columns: [1, 2],
rows: dataByYear.getFilteredRows([{
column: 0,
value: yearPicker.getState().selectedValues[0]
}])
});
pieChart.setDataTable(dataByYear);
} else {
pieChart.setView(null);
pieChart.setDataTable(dataAll);
}
pieChart.draw();
});
yearPicker.draw();
var pieChart = new google.visualization.ChartWrapper({
chartType: 'PieChart',
containerId: 'chart_div',
dataTable: dataAll,
options: {
height: 300
}
});
pieChart.draw();
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="categoryFilter_div"></div>
<div id="chart_div"></div>