在GWT 2.5自定义单元格中渲染Google图表(我使用的是UiRenderer)

时间:2012-10-07 19:15:46

标签: gwt cell google-visualization gwt-celltable

我正在使用GWT 2.5 RC1和GAE 1.7.2。 我使用UiRenderer Rendering HTML for Cells创建了一个自定义单元格。
我想使用Google Chart Tools API为GWT Getting Started Using the Google Chart Tools with GWT呈现图表
下面是render方法的片段,它不起作用。

有人知道,如何在自定义单元格中呈现Google图表?

如果他/她给我一些指示或示例代码,我将不胜感激。 提前谢谢。

    @Override
public void render(com.google.gwt.cell.client.Cell.Context context, Value value, final SafeHtmlBuilder sb) {
    final HTMLPanel test = new HTMLPanel("");
    Runnable onLoadCallback = new Runnable() {
    public void run() {
        //Panel panel = RootPanel.get();

        // Create a pie chart visualization.
        PieChart pie = new PieChart(createTable(), createOptions());

        pie.addSelectHandler(GoogleCharts.createSelectHandler(pie));
        test.add(pie);
        }
    };
    // Load the visualization api, passing the onLoadCallback to be called
    // when loading is done.
    VisualizationUtils.loadVisualizationApi(onLoadCallback, PieChart.PACKAGE);

    sb.appendHtmlConstant(test.getElement().getInnerHTML());
    renderer.render(sb);
}

1 个答案:

答案 0 :(得分:0)

看起来你有竞争条件。当你调用sb.appendHTMLConstant时,test元素可能是空的,因为尚未触发onLoadCallback。如果将最后两行移动到回调的末尾,则会删除竞争条件。

@Override
public void render(com.google.gwt.cell.client.Cell.Context context, Value value, final SafeHtmlBuilder sb) {
    final HTMLPanel test = new HTMLPanel("");

    Runnable onLoadCallback = new Runnable() {
        @Override
        public void run() {
            // Create a pie chart visualization.
            PieChart pie = new PieChart(createTable(), createOptions());
            pie.addSelectHandler(GoogleCharts.createSelectHandler(pie));
            test.add(pie);

            sb.appendHtmlConstant(test.getElement().getInnerHTML());
            renderer.render(sb);
        }
    };

    // Load the visualization api, passing the onLoadCallback to be called
    // when loading is done.
    VisualizationUtils.loadVisualizationApi(onLoadCallback, PieChart.PACKAGE);
}