2011-06-08 66 views
1

我在學習如何在GWT項目中創建線圖。以下是我的代碼:GWT線圖問題

package testproject2.client; 

import com.google.gwt.core.client.EntryPoint; 
import com.google.gwt.user.client.ui.RootPanel; 
import com.google.gwt.user.client.ui.VerticalPanel; 
import com.google.gwt.visualization.client.AbstractDataTable; 
import com.google.gwt.visualization.client.DataTable; 
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType; 
import com.google.gwt.visualization.client.VisualizationUtils; 
import com.google.gwt.visualization.client.visualizations.LineChart; 
import com.google.gwt.visualization.client.visualizations.LineChart.Options; 

public class TestProject2 implements EntryPoint { 

    VerticalPanel vPanel= new VerticalPanel(); 
    public void onModuleLoad() 
    { 
     Runnable onLoadCallback = new Runnable() 
     { 
      public void run() 
      { 
       AbstractDataTable data = createLineTable(); 
       Options options=createLineOptions(); 
       LineChart pie = new LineChart(data, options); 
       vPanel.add(pie); 
      } 
     }; 
     VisualizationUtils.loadVisualizationApi(onLoadCallback, LineChart.PACKAGE); 
     RootPanel.get().add(vPanel); 
    } 

    private LineChart.Options createLineOptions() 
    { 
      Options options = Options.create(); 
      options.setWidth(400); 
      options.setHeight(240); 
      options.setTitle("NFL Picks"); 
      return options; 
    } 
    private AbstractDataTable createLineTable() 
    { 
      DataTable data = DataTable.create(); 
      data.addColumn(ColumnType.NUMBER,"Week Number"); 
      data.addColumn(ColumnType.NUMBER,"Num Correct"); 
      data.addRows(3); 
      data.setValue(0, 0, "Week Number"); 
      data.setValue(0, 1, "Num Correct"); 
      data.setValue(1, 0, 1); 
      data.setValue(1, 1, 2); 
      data.setValue(1, 1, 13); 
      data.setValue(1, 2, 12); 
      return data; 
    } 
} 

不過,我收到以下錯誤:

10:48:57.874 [ERROR] [testproject2] Uncaught exception escaped 
com.google.gwt.core.client.JavaScriptException: (Error): Type mismatch. Value Week Number does not match type number in column index 0 
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:237) 
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:132) 
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561) 
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid(ModuleSpace.java:289) 
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:107) 
    at com.google.gwt.visualization.client.DataTable$.setValue$(DataTable.java) 
    at testproject2.client.TestProject2.createLineTable(TestProject2.java:46) 
    at testproject2.client.TestProject2.access$0(TestProject2.java:40) 
    at testproject2.client.TestProject2$1.run(TestProject2.java:22) 
    at com.google.gwt.ajaxloader.client.ExceptionHelper.runProtected(ExceptionHelper.java:36) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103) 
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71) 
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:167) 
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:281) 
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:531) 
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:352) 
    at java.lang.Thread.run(Unknown Source) 

我加入GWT Visualization API的模塊中的類路徑。有人能指出我在這裏的錯誤嗎?提前感謝。

回答

3

貌似錯誤是由這一行造成的:

data.setValue(0, 0, "Week Number"); 

雖然它也將所致,data.setValue(0, 1, "Num Correct");

你打電話時,

data.addColumn(ColumnType.NUMBER,"Week Number"); 
data.addColumn(ColumnType.NUMBER,"Num Correct"); 

您是說列中的類型將始終爲NUMBER類型,但是,然後,您嘗試在第0行中放入一個字符串 - 「週數」 - 嘗試刪除設置爲V的兩行代碼到「Week Number」和「Num Correct」,然後相應地更新您的指數。

  DataTable data = DataTable.create(); 
      data.addColumn(ColumnType.NUMBER,"Week Number"); 
      data.addColumn(ColumnType.NUMBER,"Num Correct"); 
      data.addRows(3); 
      data.setValue(0, 0, 1); 
      data.setValue(0, 1, 2); 
      data.setValue(0, 1, 13); // This is overwriting the previous column 
      data.setValue(0, 2, 12); // This is setting value for a column you didn't add 
      return data; 

您可能需要查閱API文檔以獲取創建折線圖的示例。 記住:data.SetValue(行,列,數據)

http://code.google.com/apis/chart/interactive/docs/gallery/linechart.html

+0

添加列以這種方式應該把名字寫在表的頂部。你不想使用ColumnType.String,除非你打算在每一行中放置一個字符串。由於您打算將星期數(整數)用作數據點,因此ColumnType.Number是正確的選擇。 – alykat 2011-06-08 15:18:56