在Google图表(表格是图表类型)中,如何确定列中的行是否为空,然后隐藏该列?
例如:
[Boys] [Girls]
[ 1 ] [ ]
[ 3 ] [ ]
[ 3 ] [ ]
女孩列为空,因此隐藏或删除它,导致表格图表仅显示 Boys 列。
[Boys]
[ 1 ]
[ 3 ]
[ 3 ]
以下是我的代码示例:
var datatable = new google.visualization.DataTable();
var rows = [
{
"Year": 2014,
"Period": 3,
"Segment": "*",
"AnswerOption": 0,
"AnswerValue": 5,
"ResultCount": 2,
"ResultAverage": 33.3333
},
{
"Year": 2014,
"Period": 4,
"Segment": "*",
"AnswerOption": 0,
"AnswerValue": 5,
"ResultCount": 5,
"ResultAverage": 100.00
}
]
datatable.addColumn('string', 'Year');
datatable.addColumn('number', 'Period');
datatable.addColumn('string', 'Segment');
datatable.addColumn('string', 'AnswerOption');
datatable.addColumn('number', 'AnswerValue');
datatable.addColumn('number', 'ResultCount');
datatable.addColumn('number', 'ResultAverage');
for(var j=0; j < rows.length; j++) {
datatable.addRows([
[
String(rows[j].Year),
rows[j].Period,
rows[j].Segment,
rows[j].AnswerOption,
rows[j].AnswerValue,
rows[j].ResultCount,
rows[j].ResultAverage,
]
]);
}
在此示例中,所有AnswerOption
均为0
,因此Google图表会显示一个空单元格。
答案 0 :(得分:1)
您可以解析DataTable并构造一个仅包含非空列的DataView:
// assumes you have a DataTable "data"
var columns = [];
for (var i = 0; i < data.getNumberOfColumns(); i++) {
for (var j = 0; j < data.getNumberOfRows(); j++) {
if (data.getValue(j, i) !== null) {
// add the column to the array if we find a value in it
columns.push(i);
// break because we don't need to search this column any more
break;
}
}
}
var view = new google.visualization.DataView(data);
view.setColumns(columns);
然后使用view
绘制表格,而不是使用DataTable。