1

我正在用谷歌可视化制作我的第一个图表,试图制作一个简单的条形图

我从这个例子中得到了这个:

        VerticalPanel vp = new VerticalPanel();
        Options options = Options.create();
        options.setHeight(240);
        options.setTitle("Company Performance");
        options.setWidth(400);
        options.set3D(true);

        DataTable data = DataTable.create();
        data.addColumn(ColumnType.STRING, "Year");
        data.addColumn(ColumnType.NUMBER, "Sales");
        data.addColumn(ColumnType.NUMBER, "Expenses");
        data.addRows(4);
        data.setValue(0, 0, "2004");


        VisualizationWidget<BarChart, BarChart.Options> widget = 
          BarChart.createWidget(data, options);
        BarChart viz = widget.getVisualization();
        Label status = new Label();
        viz.addSelectHandler(new SelectionDemo(viz, status));
        vp.add(status);
        vp.add(widget);

这条线

                  VisualizationWidget<BarChart, BarChart.Options> widget = 
          BarChart.createWidget(data, options);

               VisualizationWidget cannot be resolved to a type

编译错误:

    [ERROR] Line 150:  VisualizationWidget cannot be resolved to a type
  [ERROR] Line 151:  The method createWidget(DataTable, PieChart.Options) is undefined for the type BarChart
  [ERROR] Line 154:  SelectionDemo cannot be resolved to a type

我无法理解这个 VisualizationWidget 是什么,如果你能指导我如何解决这个问题

谢谢

4

1 回答 1

3

它表明您可能忘记在项目中添加 Visualization jar 的依赖项。您可以尝试手动将依赖项添加到项目中的 .gwt.xml 文件中:

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

在您的.html文件中添加以下代码:

   <script type="text/javascript" src="http://www.google.com/jsapi"></script>
   <script type="text/javascript">
    google.load("visualization", "1", {'packages' : ["corechart"] });
    </script>

代码

@Override
    public void onModuleLoad() {


          VerticalPanel vp = new VerticalPanel();
            final Options options = Options.create();
            options.setHeight(240);
            options.setTitle("Company Performance");
            options.setWidth(400);
            options.set3D(true);

             DataTable data = DataTable.create();
            data.addColumn(ColumnType.STRING, "Year");
            data.addColumn(ColumnType.NUMBER, "Sales");
            data.addColumn(ColumnType.NUMBER, "Expenses");
            data.addRows(4);
            data.setValue(0, 0, "2004");

            BarChart barChart=new BarChart(data, options);

            vp.add(barChart);
            RootPanel.get().add(vp);


            Runnable onLoadCallback = new Runnable() {
                public void run() {
                  Panel panel = RootPanel.get();

                  // Create a pie chart visualization.
                  BarChart pie = new BarChart(createTable(), options);
                  panel.add(pie);
                }
              };


              VisualizationUtils.loadVisualizationApi(onLoadCallback, BarChart.PACKAGE);

    }
      private AbstractDataTable createTable() {
            DataTable data = DataTable.create();
            data.addColumn(ColumnType.STRING, "Task");
            data.addColumn(ColumnType.NUMBER, "Hours per Day");
            data.addRows(2);
            data.setValue(0, 0, "Work");
            data.setValue(0, 1, 14);
            data.setValue(1, 0, "Sleep");
            data.setValue(1, 1, 10);
            return data;
          }
于 2012-09-24T05:55:57.080 回答