2014-10-05 38 views
0

我正在嘗試使用Google Visualization將交互式圖表整合到GWT應用程序中。我不想連接到Google App引擎。我還想使用GWT API,而不是在我的代碼中嵌入javaScript。我試圖在gwt-google-apis中遵循示例SimpleViz和HelloAjaxLoader,但圖表無法加載。我已將ajaxloader和可視化jar添加到我的類路徑中,並在gwt.xml中繼承。在以下代碼中,將顯示「onModuleLoad」中的警報,但不是「正在運行」。如何在GWT和AjaxLoader中使用Google可視化圖表

我的代碼:

public class SimpleViz implements EntryPoint { 
    private VerticalPanel container; 
    private ImagePieChart chart; 

public void onModuleLoad() { 
    Window.alert("In onModuleLoad"); 
    container = new VerticalPanel(); 
    RootPanel.get().add(container);  
    container.getElement().getStyle().setPropertyPx("margin", 10); 
    container.setSpacing(10); 
    container.add(new Label("Pie Chart Example")); 

    AjaxLoader.init();  
    AjaxLoader.loadApi("visualization", "1", new Runnable() { 

     public void run() { 
      Window.alert("In run"); 
      chart=new ImagePieChart(); 
      container.add(chart); 
      draw(); 
     } 
    }, null); 

} 


private void draw() { 
    // Prepare the data 
    DataTable dataTable = DataTable.create(); 
    dataTable.addColumn(ColumnType.STRING, "Task"); 
    dataTable.addColumn(ColumnType.NUMBER, "Hours per Day"); 
    dataTable.addRows(5); 
    dataTable.setValue(0, 0, "Work"); 
    dataTable.setValue(0, 1, 11); 
    dataTable.setValue(1, 0, "Sleep"); 
    dataTable.setValue(1, 1, 7); 
    dataTable.setValue(2, 0, "Watch TV"); 
    dataTable.setValue(2, 1, 3); 
    dataTable.setValue(3, 0, "Eat"); 
    dataTable.setValue(3, 1, 2); 
    dataTable.setValue(4, 0, "Commute"); 
    dataTable.setValue(4, 1, 1); 

    // Set options 
    ImagePieChart.Options options = ImagePieChart.Options.create(); 
    options.setBackgroundColor("#f0f0f0"); 
    options.setIs3D(false); 
    options.setTitle("So, how was your day?"); 

    // Draw the chart 
    chart.draw(dataTable, options); 
    } 

} 

作爲後續問題,是這些庫的電流和良好的維護或者我應該尋找不同的解決方案?

回答

0

這就是我如何做到的(它有效)。

在你的模塊gwt.xml文件中添加以下行:

<inherits name="com.google.gwt.visualization.Visualization"/> 

此行繼承gwt-visualization.jar

然後在你的代碼:

Runnable onLoadCallback = new Runnable() { 
    @Override 
    public void run() { 
     draw(); 
    } 
}; 
VisualizationUtils.loadVisualizationApi(onLoadCallback, CoreChart.PACKAGE); 

你不需要AjaxLoader。

+0

謝謝。當我連接到互聯網時使用VisualizationUtils。在沒有連接到Google網站的情況下,是否有任何方法使用可視化? – Alicia 2014-10-06 15:34:58

+0

請參閱:https://developers.google.com/chart/interactive/faq#offline和同一列表中的下一個問題。 – 2014-10-06 16:31:50

+0

再次感謝。瞭解。 – Alicia 2014-10-06 23:01:52

相關問題