无法在多个选项卡(GWT TabPanel)上获取多个图表(Google Visualization)
我需要显示未知数量的选项卡,每个选项卡都有未知数量的图表(Google 可视化)。我创建了“Tab”和“Graph”类,Tab 包含一个 ArrayList。
TabWrappers 扩展了 FlexTable,目前为空。目前它是一个占位符,但如果我使用 FlexTable 而不是 TabWrapper,行为不会改变。
下面的代码减去添加 Tab2 的部分,非常适合创建 1 个填充图形的选项卡。添加第二个选项卡时,两个选项卡都会显示并正确命名,但都没有图表。
public class SomeClass {
...
DataTable data = response.getDataTable();
DataView result;
Options options = createOptions();
ArrayList<Tab> displayTab = new ArrayList<Tab>();
Tab t;
ArrayList<Graph> graphList = new ArrayList<Graph>();
Graph g;
t = new Tab();
g = new Graph();
result = DataView.create(data);
result.setRows(new int[]{0, 2, 4, 6});
g.setGraphType(new PieChart(result, options));
graphList.add(g);
g = new Graph();
result = DataView.create(data);
result.setRows(new int[]{1, 3, 5, 7});
g.setGraphType(new PieChart(result, options));
graphList.add(g);
g = new Graph();
result = DataView.create(data);
g.setGraphType(new PieChart(result, options));
graphList.add(g);
t.setTabName("Tab1");
t.setGraphs(graphList);
displayTab.add(t);
// Add a 2nd tab
t = new Tab();
t.setTabName("Tab2");
t.setGraphs(graphList);
displayTab.add(t);
TabWrapper tabWrapper;
for (Tab tX : displayTab){
int row = 0, col = 0, maxCol = 2;
tabWrapper = new TabWrapper();
for (Graph gX : tX.getGraphs()) {
col = tX.getGraphs().indexOf(gX) - (row * maxCol);
tabWrapper.setWidget(row, col, gX.getGraphType().asWidget());
if (++col == maxCol) {
row++;
}
}
tabPanel.add(tabWrapper, tX.getTabName());
}
...
}
I need to display an unknown quantity of tabs each with an unknown quantity of graphs (Google Visualizations). I have created "Tab" and "Graph" classes and Tab contains an ArrayList.
TabWrappers extends FlexTable and is currently empty. It's a place holder at the moment, but the behavior does not change if I use FlexTable rather than TabWrapper.
The code below, minus the section that adds Tab2 works perfectly for creating 1 tab populated with graphs. When adding the 2nd tab both tabs are displayed and named correctly but neither have graphs.
public class SomeClass {
...
DataTable data = response.getDataTable();
DataView result;
Options options = createOptions();
ArrayList<Tab> displayTab = new ArrayList<Tab>();
Tab t;
ArrayList<Graph> graphList = new ArrayList<Graph>();
Graph g;
t = new Tab();
g = new Graph();
result = DataView.create(data);
result.setRows(new int[]{0, 2, 4, 6});
g.setGraphType(new PieChart(result, options));
graphList.add(g);
g = new Graph();
result = DataView.create(data);
result.setRows(new int[]{1, 3, 5, 7});
g.setGraphType(new PieChart(result, options));
graphList.add(g);
g = new Graph();
result = DataView.create(data);
g.setGraphType(new PieChart(result, options));
graphList.add(g);
t.setTabName("Tab1");
t.setGraphs(graphList);
displayTab.add(t);
// Add a 2nd tab
t = new Tab();
t.setTabName("Tab2");
t.setGraphs(graphList);
displayTab.add(t);
TabWrapper tabWrapper;
for (Tab tX : displayTab){
int row = 0, col = 0, maxCol = 2;
tabWrapper = new TabWrapper();
for (Graph gX : tX.getGraphs()) {
col = tX.getGraphs().indexOf(gX) - (row * maxCol);
tabWrapper.setWidget(row, col, gX.getGraphType().asWidget());
if (++col == maxCol) {
row++;
}
}
tabPanel.add(tabWrapper, tX.getTabName());
}
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在 GWT 中使用选项卡时,每个选项卡中包含的面板似乎是延迟加载的,并且在用户单击每个选项卡之前并未完全设置。
特别是,选项卡容器的宽度和高度为零(错误日志可能会给出有关宽度为零的容器的错误),因此图形绘制将失败并留下空白空间。
您需要做的(无论如何可能是一个好的实践)是延迟加载选项卡的内容,以便仅在选项卡完全设置时才加载图表。这可以通过删除调用 t.setGraphs(...) 并添加一个执行此操作的选择处理程序来完成:
以便仅在选择选项卡时添加和绘制图形。
您可能还需要调用
在选择处理程序就位后,
以强制选择第一个选项卡。我遇到了与您描述的相同的问题,这解决了它。
When you use tabs in GWT, the panels held in each tab seem to be lazy loaded and aren't completely set up until the user clicks on each tab.
In particular, the tab containers will have zero widths and heights (the error logs will probably be giving an error about containers having zero width) so the graph drawing will fail and leave an empty space.
What you need to do (and is probably good practice anyway) is to lazy load the contents of the tabs too so that the graph is loaded only when the tab is fully set up. This can be done by removing the call the t.setGraphs(...) and adding a selection handler that does it instead:
so that graphs are added and drawn only when the tab is selected.
You'll probably also want to call
to force a selection of the first tab after the selection handler is in place.
I had the same issues that you describe and this resolved it.