Both Sort and Filter View

223 views
Skip to first unread message

AM

unread,
Jul 4, 2014, 5:21:02 PM7/4/14
to google-visua...@googlegroups.com
Is there a way to both sort and filter the data in a view?


    var view18 = new google.visualization.DataView(dt);
    view18.setRows(dt.getFilteredRows([{'column': 2, 'value': 'Male'}]));
    view18.setRows(dt.getSortedRows(3));
    Chart18.setDataTable(view18);
    Chart18.draw();

I'm finding that this only allows for the the filter.

Andrew Gallant

unread,
Jul 4, 2014, 7:34:40 PM7/4/14
to google-visua...@googlegroups.com
You can do this with a single view; you need to get the sorted rows from the view and then translate them back into the table row indices.


var view18 = new google.visualization.DataView(dt);
view18.setRows(
dt.getFilteredRows([{'column': 2, 'value': 'Male'}]));
var sortedViewRows = view18.getSortedRows(3);
var sortedTableRows = [];
for (var i = 0; i < sortedViewRows.length; i++) {
    sortedTableRows.push(view18.getTableRowIndex(sortedViewRows[i]));
}
view18.setRows(sortedTableRows);


Using two views makes for cleaner code, though:

var filterView = new google.visualization.DataView(dt);
filterView.setRows(dt.getFilteredRows([{'column': 2, 'value': 'Male'}]));
var view18 = new google.visualization.DataView(filterView);
view18.setRows(filterView.getSortedRows(3));

AM

unread,
Jul 8, 2014, 4:10:51 PM7/8/14
to google-visua...@googlegroups.com
This was very very helpful.  Thanks much Andrew!

AM

unread,
Jul 12, 2014, 4:43:12 PM7/12/14
to google-visua...@googlegroups.com
What if I needed to filter the data for the first chart?

If I already have that chart in in the dashboard initialization:

var dashboard = new google.visualization.Dashboard(document.getElementById('chart_div1')).bind([categoryPicker1],[Chart1,Chart4]).draw(data1);

I can't then do this procedure of creating new views based on the first table.  I would need to filter it first before that dashboard draw?

Would I just do a temp variable like var view1?

Thanks for any feedback.

Andrew Gallant

unread,
Jul 12, 2014, 6:23:35 PM7/12/14
to google-visua...@googlegroups.com
I would create a DataView based on your original DataTable, and sort that.  The pass the view to the Dashboard instead of the DataTable.
Reply all
Reply to author
Forward
0 new messages