Hide Empty Rows and Columns
This table JavaScript can be used to hide rows or columns for tables and charts where the primary statistic for the table is always 0% or NaN. The primary statistic is the first statistic on the Statistics - Cells list for the table. A note is added to the footer when any rows or columns have been removed.
As significance testing is conducted prior to the application of the Table JavaScript, the significance highlighting from the cells in the original table will be shown.
// Returns true if all elements of the array are zero
function arrayElementsAllZero(array) {
var non_zero = array.filter(function(x) { return x > 0; } );
return non_zero.length == 0;
}
// Returns true if all elements of the array are NaN
function arrayElementsAllNaN(array) {
var not_nan = array.filter(function(x) { return !isNaN(x); } );
return not_nan.length == 0;
}
// Obtain the primary statistics to check
var primary_statistic = table.availableStatistics[0];
var values = table.get(primary_statistic);
// Check to see if the primary statistic is a % statistic.
var is_percentage = primary_statistic.indexOf('%') != -1;
// Check for empty rows
var empty_rows = [];
for (var j = 0; j < table.numberRows; j++) {
var current_row_values = values[j];
if ( (is_percentage && arrayElementsAllZero(current_row_values)) || arrayElementsAllNaN(current_row_values))
empty_rows.push(j);
}
// Check for empty columns
var empty_columns = [];
for (var j = 0; j < table.numberColumns; j++) {
var current_column_values = [];
for (var k = 0; k < table.numberRows; k++)
current_column_values.push(values[k][j]);
if ((is_percentage && arrayElementsAllZero(current_column_values)) || arrayElementsAllNaN(current_column_values))
empty_columns.push(j);
}
// Remove empty rows
if (empty_rows.length > 0)
for (var j = empty_rows.length - 1; j > -1; j--) {
table.deleteRow(empty_rows[j]);
if (typeof right_table !== 'undefined') {
if (right_table.numberRows == 1)
right_table.deleteColumn(empty_rows[j]);
else
right_table.deleteRow(empty_rows[j]);
}
}
// Remove empty columns
if (empty_columns.length > 0)
for (var j = empty_columns.length - 1; j > -1; j--) {
table.deleteColumn(empty_columns[j]);
if (typeof below_table !== 'undefined') {
if (below_table.numberColumns == 1)
below_table.deleteRow(empty_columns[j]);
else
below_table.deleteColumn(empty_columns[j]);
}
}
// Add a note to the footer
if (empty_rows.length > 0 || empty_columns.length > 0) {
var current_footers = table.extraFooters;
current_footers.push("Some empty rows or columns have been removed");
table.extraFooters = current_footers;
}
Note that the code above also removes the empty columns and rows from below_table and right_table, which correspond to the tables for the Statistics - Below and Statistics - Right respectively. These tables must be addressed separately - they do not automatically update when the main table is changed. The two functions deleteColumnComplete() and deleteRowComplete() can be used to address the extra tables automatically whenever they are shown on the table.
See Also
- Table JavaScript and Plot JavaScript for an explanation of how to run this code.
- Table JavaScript and Plot JavaScript Reference for technical information.
- Table JavaScript and Plot JavaScript Examples Library for other examples.
- JavaScript for information about the JavaScript programming language.
- QScript for tools for automating projects using JavaScript.
- JavaScript Variables for detail on how to create new variables in the Variables and Questions tab using JavaScript.