代码改变世界

Google Visualization For GWT

2009-09-29 09:58  小寇子  阅读(683)  评论(0)    收藏  举报

Getting Started with Visualizations

Assumptions

下载 Google Visualization http://code.google.com/p/gwt-google-apis/downloads/
1:Calling the Ajax Loader
    Options 是JavaScriptObject的子类,不能用new实例化。用Options.create();
3.Creating a DataTable
    所有的Visualization的数据都是由DataTable或 DataView 提供。DataTable是一个2维的表。同样,DataTable不能用new,而用 DataTable.create()来实例化。每一列由枚举DataTable.ColumnType来定义数据类型。
4.Creating a DataView
可以用把一个DataView绑定到DataTable,可以显示一个DataTable不同列的数据。
DataView result = DataView.create(data);
result.setColumns(
new int[]{0, 2});//绑定DataTable的第一列和第二列

5.Issuing a Query
class  Query 可以让你请求一个数据源(例如 Google Docs里的文件),用 Query.send() 发出请求,用 Query.Callback来指定请求完成后的函数。如果请求没有错误可以用QueryResponse.getDataTable() 来获取DataTable来创建一个Visualization。
private SelectHandler createSelectHandler(final PieChart chart) {
return new SelectHandler() {
@Override
public void onSelect(SelectEvent event) {
String message
= "";

// May be multiple selections.
JsArray<Selection> selections = chart.getSelections();

for (int i = 0; i < selections.length(); i++) {
// add a new line for each selection
message += i == 0 ? "" : "\n";

Selection selection
= selections.get(i);

if (selection.isCell()) {
// isCell() returns true if a cell has been selected.

// getRow() returns the row number of the selected cell.
int row = selection.getRow();
// getColumn() returns the column number of the selected cell.
int column = selection.getColumn();
message
+= "cell " + row + ":" + column + " selected";
}
else if (selection.isRow()) {
// isRow() returns true if an entire row has been selected.

// getRow() returns the row number of the selected row.
int row = selection.getRow();
message
+= "row " + row + " selected";
}
else {
// unreachable
message += "Pie chart selections should be either row selections or cell selections.";
message
+= " Other visualizations support column selections as well.";
}
}

Window.;
}
};
}

5.Creating a SelectHandler
很多Visualition支持SelectHandler 和 SelectEvent
private SelectHandler createSelectHandler(final PieChart chart) {
return new SelectHandler() {
@Override
public void onSelect(SelectEvent event) {
String message
= "";

// May be multiple selections.
JsArray<Selection> selections = chart.getSelections();

for (int i = 0; i < selections.length(); i++) {
// add a new line for each selection
message += i == 0 ? "" : "\n";

Selection selection
= selections.get(i);

if (selection.isCell()) {
// isCell() returns true if a cell has been selected.

// getRow() returns the row number of the selected cell.
int row = selection.getRow();
// getColumn() returns the column number of the selected cell.
int column = selection.getColumn();
message
+= "cell " + row + ":" + column + " selected";
}
else if (selection.isRow()) {
// isRow() returns true if an entire row has been selected.

// getRow() returns the row number of the selected row.
int row = selection.getRow();
message
+= "row " + row + " selected";
}
else {
// unreachable
message += "Pie chart selections should be either row selections or cell selections.";
message
+= " Other visualizations support column selections as well.";
}
}

Window.;
}
};
}