我正在制作谷歌可视化中的第一张图表, 试图制作一个简单的条形图
我从示例中得到了这个:
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是什么,如果你可以指导我如何解决这个问题
谢谢
答案 0 :(得分: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;
}