Introduction
ZK Pivottable is a ZK addon component that can display data in summarized view as MS-Excel's pivottable. In this post, we will try to display some fake data in pivottable.
The Composer
TestComposer.java
package test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import org.zkoss.pivot.PivotField;
import org.zkoss.pivot.impl.TabularPivotModel;
import org.zkoss.zk.ui.select.SelectorComposer;
/**
* Tested with ZK 6.0.1 CE and ZK Pivottable 2.0.0
*
*/
@SuppressWarnings("rawtypes")
public class TestComposer extends SelectorComposer {
/**
* generated serial version UID
*/
private static final long serialVersionUID = -2897873399288955635L;
private TabularPivotModel _pivotModel;
/**
* Get pivottable's model
* @return TabularPivotModel the pivottable's model
* @throws Exception
*/
public TabularPivotModel getPivotModel () throws Exception {
if (_pivotModel == null) {
_pivotModel = new TabularPivotModel(getData(), getColumns());
// assign rows, the order matches to the level of row node field
_pivotModel.setFieldType("Row_Level_001", PivotField.Type.ROW);
_pivotModel.setFieldType("Row_Level_002", PivotField.Type.ROW);
_pivotModel.setFieldType("Row_Level_003", PivotField.Type.ROW);
_pivotModel.setFieldType("Row_Level_004", PivotField.Type.ROW);
// assign columns, the order matches to the level of column node field
_pivotModel.setFieldType("Column_Level_001", PivotField.Type.COLUMN);
_pivotModel.setFieldType("Column_Level_002", PivotField.Type.COLUMN);
// assign datas, the order matches to the order of data field
_pivotModel.setFieldType("Data_Field_001", PivotField.Type.DATA);
_pivotModel.setFieldType("Data_Field_002", PivotField.Type.DATA);
_pivotModel.setFieldType("Data_Field_003", PivotField.Type.DATA);
}
return _pivotModel;
}
/**
* prepare the data for pivottable's model
* The order of object put into data list matches
* the order of column name's order
* @return
* @throws Exception
*/
public List<List<Object>> getData() throws Exception {
List<List<Object>> result = new ArrayList<List<Object>>();
Random r = new Random();
for (int i = 0; i < 10000; i++) {
List<Object> data = new ArrayList<Object>();
data.add("Row_Level_001 - " + (r.nextInt(10) + 1));
data.add("Row_Level_002 - " + (r.nextInt(10) + 1));
data.add("Row_Level_003 - " + (r.nextInt(10) + 1));
data.add("Row_Level_004 - " + (r.nextInt(10) + 1));
data.add("Column_Level_001 - " + (r.nextInt(10) + 1));
data.add("Column_Level_002 - " + (r.nextInt(10) + 1));
data.add(r.nextInt(10000));
data.add(r.nextDouble() * 10000.0);
data.add(r.nextInt(100));
result.add(data);
}
return result;
}
/**
* prepare columns name for pivottable's model
* @return
*/
public List<String> getColumns() {
return Arrays.asList(new String[]{
"Row_Level_001", "Row_Level_002", "Row_Level_003", "Row_Level_004",
"Column_Level_001", "Column_Level_002",
"Data_Field_001", "Data_Field_002", "Data_Field_003"
});
}
}
Here only simply add some fake data / columns to pivot model.
The order of data will matches to the order of column automatically.
The ZUL Page
index.zul
<zk>
<!-- Tested with ZK 6.0.1 CE and ZK Pivottable 2.0.0 -->
<!-- window, apply a SelectorComposer -->
<window id="win" xmlns:w="client"
apply="test.TestComposer">
<!-- pivottable, get model from window's composer -->
<pivottable id="pivottable" model="${win$composer.pivotModel}" />
</window>
</zk>
The Result
View the demo flash on line
http://screencast.com/t/C2CxL4aeNHmZ
You can find the flash file at github:
https://github.com/benbai123/ZK_Practice/blob/master/Components/demos/addon/DisplayDataInPivottable.swf
Reference
http://books.zkoss.org/wiki/ZK_Pivottable_Essentials
Download
The full project is at github
https://github.com/benbai123/ZK_Practice/tree/master/Components/projects/Addon_Practice/PivottableTest/DisplayDataInPivottable
No comments:
Post a Comment