我正在尝试使用应用脚本构建一个Google图表信息中心 基本上,我在3个谷歌电子表格中有3个数据源,并希望创建一个显示3个单独图表的单板,每个图表由自己的数据表提供。
我当前的脚本位于我只引用一个数据源的位置: var ss = SpreadsheetApp.openById('0AgQXvGX4FXy6dGRrOEZ3djY0M3dIUTNoQkV4TS00MEE')
最终,我想为tableChart使用一个源,为ColumnChart和一个LineChart使用另一个源。
如何实现这一目标?
function doGet() {
var ss = SpreadsheetApp.openById('0AgQXvGX4FXy6dGRrOEZ3djY0M3dIUTNoQkV4TS00MEE')
var data = ss.getDataRange();
var InstallDateFilter = Charts.newCategoryFilter()
.setFilterColumnIndex(0)
.setAllowMultiple(false)
.build();
var PlatformFilter = Charts.newCategoryFilter()
.setFilterColumnIndex(1)
.build();
var tableChart = Charts.newTableChart()
.setDimensions(600, 1000)
.enablePaging(30)
.setDataViewDefinition(Charts.newDataViewDefinition()
.setColumns([0,1,2,3,4]))
.build();
var ColumnChart = Charts.newColumnChart()
.setDimensions(600, 600)
.setColors(["green", "red"])
.setTitle('Installs')
.setXAxisTitle('Platform')
.setDataViewDefinition(Charts.newDataViewDefinition()
.setColumns([1,4]))
.setLegendPosition(Charts.Position.BOTTOM)
.build();
var LineChart = Charts.newLineChart()
.setDimensions(1000, 600)
.setTitle('ARPI')
.setXAxisTitle('InstallDate')
.setDataViewDefinition(Charts.newDataViewDefinition()
.setColumns([0,2]))
.setCurveStyle(Charts.CurveStyle.SMOOTH)
.setPointStyle(Charts.PointStyle.MEDIUM)
.setLegendPosition(Charts.Position.BOTTOM)
.build();
var dashboard = Charts.newDashboardPanel()
.setDataTable(data)
.bind ( [InstallDateFilter, PlatformFilter], [tableChart] )
.bind ( [InstallDateFilter, PlatformFilter], [ColumnChart] )
.bind ( [InstallDateFilter, PlatformFilter], [LineChart] )
.build();
var app = UiApp.createApplication();
var filterPanel = app.createVerticalPanel();
var chartPanel = app.createHorizontalPanel();
filterPanel.add(InstallDateFilter) .add(PlatformFilter).setSpacing(10);
chartPanel .add(tableChart) .add(ColumnChart) .add(LineChart) .setSpacing(10);
dashboard.add(app.createHorizontalPanel()
.add(filterPanel).add(chartPanel));
app.add(dashboard);
return app;
}
答案 0 :(得分:1)
使用具有不同基础数据的图表的仪表板是没有意义的。仪表板用于以不同方式显示相同的数据。如果您想要每个图表的过滤器,请制作3个仪表板,或者不要使用仪表板并填充每个图表数据,这样做有很多示例。
答案 1 :(得分:0)
您可以使用不同的数据集创建不同的仪表板。
以下是代码示例:
function doGet() {
var data1 = getFirstDataSet();
var genderFilter = Charts.newCategoryFilter().setFilterColumnLabel("Gender").build();
var tableChart = Charts.newTableChart().setDataViewDefinition(Charts.newDataViewDefinition().setColumns([0, 1, 2])).build();
var dashboard1 = Charts.newDashboardPanel().setDataTable(data1).bind(genderFilter, tableChart).build();
var data2 = getSecondDataSet();
var ageFilter = Charts.newNumberRangeFilter().setFilterColumnLabel("Age").build();
var pieChart = Charts.newPieChart().setDataViewDefinition(Charts.newDataViewDefinition().setColumns([0, 2])).build();
var dashboard2 = Charts.newDashboardPanel().setDataTable(data2).bind(ageFilter, pieChart).build();
var uiApp = UiApp.createApplication();
dashboard1.add(uiApp.createHorizontalPanel().setSpacing(70).add(genderFilter).add(tableChart));
dashboard2.add(uiApp.createHorizontalPanel().setSpacing(70).add(ageFilter).add(pieChart));
uiApp.add(dashboard1);
uiApp.add(dashboard2);
return uiApp;
}
function getFirstDataSet() {
var data = Charts.newDataTable()
.addColumn(Charts.ColumnType.STRING, "Name")
.addColumn(Charts.ColumnType.STRING, "Gender")
.addColumn(Charts.ColumnType.NUMBER, "Donuts eaten")
.addRow(["Michael", "Male", 5])
.addRow(["Elisa", "Female", 7])
.addRow(["Robert", "Male", 3])
.addRow(["John", "Male", 2])
.addRow(["Jessica", "Female", 6])
.addRow(["Aaron", "Male", 1])
.addRow(["Margareth", "Female", 8])
.addRow(["Miranda", "Female", 6])
.build();
return data;
}
function getSecondDataSet() {
var data = Charts.newDataTable()
.addColumn(Charts.ColumnType.STRING, "Name")
.addColumn(Charts.ColumnType.NUMBER, "Age")
.addColumn(Charts.ColumnType.NUMBER, "Donuts eaten")
.addRow(["Michael", 12, 5])
.addRow(["Elisa", 20, 7])
.addRow(["Robert", 7, 3])
.addRow(["John", 54, 2])
.addRow(["Jessica", 22, 6])
.addRow(["Aaron", 3, 1])
.addRow(["Margareth", 42, 8])
.addRow(["Miranda", 33, 6])
.build();
return data;
}