显示具有GWT可视化的多个图

时间:2011-11-17 20:57:16

标签: java gwt google-visualization runnable asynccallback

这段代码(下面)用于在屏幕上显示1..n LineCharts而不会发生意外。我想知道它的效率如何。我每次都打电话给VisualizationsUtils.loadVisualizationApi(onLoadCallback, LineChart.PACKAGE)。必须以这种方式完成吗?

public void getData(List<GraphWrapper> graphWrapperList) {
    for (GraphWrapper graphWrapper : graphWrapperList) {
        populateResources.populateResourcesService(graphWrapper.getSeriesWrapperList(),
                new AsyncCallback<GraphWrapper>() {
                    public void onFailure(Throwable caught) {
                        displayDialogBox("*** An Error Occurred ***", caught.toString());
                    }

                    public void onSuccess(final GraphWrapper response) {
                        Runnable onLoadCallback = new Runnable() {
                            private Widget chart;

                            public void run() {
                                this.chart = new LineChart(createTable(response), createOptions(response));
                                graphPanel.add(this.chart);
                            }
                        };
                        VisualizationUtils.loadVisualizationApi(onLoadCallback, LineChart.PACKAGE);
                    }
                });
    }
}

2 个答案:

答案 0 :(得分:2)

通过加载LineChart.PACKAGE,您只能使用单一类型的图表(LineChart)。由于您一次只能加载一个“包”,因此您仍然坚持这一决定。试试这个:

VisualizationUtils.loadVisualizationApi(onLoadCallback, CoreChart.PACKAGE);

使用CoreChart包,您可以加载CoreChart(Line,Bar,Pie,Area,Column和Scatter)子项的任何内容。您还可以加载任何数量/组合的图表。此外,您不需要每次都调用loadVisualizationApi(...)方法,而只需要第一次创建图表。此后每个人都将使用该库。

答案 1 :(得分:1)

我认为加载LineChart包一次就足够了。您是否尝试在回调中运行循环?

public void getData(List<GraphWrapper> graphWrapperList) {
Runnable onLoadCallback = new Runnable() {
    public void run() {
            for (GraphWrapper graphWrapper : graphWrapperList) {
                populateResources.populateResourcesService(graphWrapper.getSeriesWrapperList(),
                new AsyncCallback<GraphWrapper>() {
                        public void onFailure(Throwable caught) {
                            displayDialogBox("*** An Error Occurred ***", caught.toString());
                        }
                        public void onSuccess(final GraphWrapper response) {
                            private Widget chart;
                            this.chart = new LineChart(createTable(response), createOptions(response));
                            graphPanel.add(this.chart);                  
                        }
                });
            }
        }
    };
VisualizationUtils.loadVisualizationApi(onLoadCallback, LineChart.PACKAGE);
}