开发者

GWT linechart problem

I'm trying to learn how to create a linechart within a GWT project. Following is my code:

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=cr开发者_JAVA百科eateLineOptions();
              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; 
  } 
}

However, I am getting the following error:

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)

I added GWT Visualization API module in classpath. Can someone please point out what I am dong wrong here? Thank so much in advance.


Looks like the error is being caused by this line:

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

although it would also be caused by, data.setValue(0, 1, "Num Correct");

When you call,

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

You are saying that the type in the columns will always be type NUMBER, however, you are then trying to put a String - "Week Number" - in row 0. Try removing the two lines of code where you setValue to "Week Number" and "Num Correct" and then updating your indices accordingly.

          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; 

you may want to consult the API documentation for an example on creating the line chart. Remember: data.SetValue(row, col, data)

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜