GWT线图问题

时间:2011-06-08 14:52:50

标签: 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)

我在classpath中添加了GWT Visualization API模块。有谁可以请指出我在这里错了什么?非常感谢提前。

1 个答案:

答案 0 :(得分: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行中放置一个字符串 - “周数”。尝试删除您将setValue设置为“的两行代码”周数“和”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(row,col,data)

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