使用BIRT API创建交叉报表2
https://code.google.com/p/my-open-source/source/browse/code/birt/CreateDataCube.java?spec=svn4&r=4
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
package tes.birt; import java.io.IOException; import org.eclipse.birt.core.framework.Platform; import org.eclipse.birt.report.item.crosstab.core.ICrosstabConstants; import org.eclipse.birt.report.item.crosstab.core.de.CrosstabCellHandle; import org.eclipse.birt.report.item.crosstab.core.de.CrosstabReportItemHandle; import org.eclipse.birt.report.item.crosstab.core.de.DimensionViewHandle; import org.eclipse.birt.report.item.crosstab.core.de.LevelViewHandle; import org.eclipse.birt.report.item.crosstab.core.de.MeasureViewHandle; import org.eclipse.birt.report.item.crosstab.core.util.CrosstabExtendedItemFactory; import org.eclipse.birt.report.model.api.ComputedColumnHandle; import org.eclipse.birt.report.model.api.DataItemHandle; import org.eclipse.birt.report.model.api.DataSetHandle; import org.eclipse.birt.report.model.api.DesignConfig; import org.eclipse.birt.report.model.api.DesignElementHandle; import org.eclipse.birt.report.model.api.ElementFactory; import org.eclipse.birt.report.model.api.ExtendedItemHandle; import org.eclipse.birt.report.model.api.IDesignEngine; import org.eclipse.birt.report.model.api.IDesignEngineFactory; import org.eclipse.birt.report.model.api.LabelHandle; import org.eclipse.birt.report.model.api.OdaDataSetHandle; import org.eclipse.birt.report.model.api.OdaDataSourceHandle; import org.eclipse.birt.report.model.api.ReportDesignHandle; import org.eclipse.birt.report.model.api.ReportItemHandle; import org.eclipse.birt.report.model.api.SessionHandle; import org.eclipse.birt.report.model.api.StructureFactory; import org.eclipse.birt.report.model.api.activity.SemanticException; import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants; import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn; import org.eclipse.birt.report.model.api.extension.IReportItem; import org.eclipse.birt.report.model.api.olap.MeasureGroupHandle; import org.eclipse.birt.report.model.api.olap.MeasureHandle; import org.eclipse.birt.report.model.api.olap.TabularCubeHandle; import org.eclipse.birt.report.model.api.olap.TabularDimensionHandle; import org.eclipse.birt.report.model.api.olap.TabularHierarchyHandle; import org.eclipse.birt.report.model.api.olap.TabularLevelHandle; import org.eclipse.birt.report.model.elements.interfaces.ICubeModel; import com.ibm.icu.util.ULocale; /** * Simple BIRT Design Engine API (DEAPI) demo. */ public class CreateDataCube { ReportDesignHandle designHandle = null; ElementFactory designFactory = null; StructureFactory structFactory = null; public static void main(String[] args) { try { CreateDataCube de = new CreateDataCube(); de.buildReport(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SemanticException e) { // TODO Auto-generated catch block e.printStackTrace(); } } void buildDataSource() throws SemanticException { OdaDataSourceHandle dsHandle = designFactory.newOdaDataSource( "Data Source", "org.eclipse.birt.report.data.oda.jdbc"); dsHandle.setProperty("odaDriverClass", "org.eclipse.birt.report.data.oda.sampledb.Driver"); dsHandle.setProperty("odaURL", "jdbc:classicmodels:sampledb"); dsHandle.setProperty("odaUser", "ClassicModels"); dsHandle.setProperty("odaPassword", ""); designHandle.getDataSources().add(dsHandle); } void buildDataSet() throws SemanticException { OdaDataSetHandle dsHandle = designFactory.newOdaDataSet("ds", "org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet"); dsHandle.setDataSource("Data Source"); String qry = "Select * from customers"; dsHandle.setQueryText(qry); designHandle.getDataSets().add(dsHandle); } void buidCube() throws SemanticException { // 使用引擎工厂创建TabularCube 指定名称 创建多维数据集 名称为 "MyCube" TabularCubeHandle cubeHandle = designHandle.getElementFactory() .newTabularCube("MyCube"); // 获取第一个dataset 对象 填充到多维数据集 "MyCube" cubeHandle .setDataSet((DataSetHandle) designHandle.getDataSets().get(0)); // 把多维数据集加入引擎 designHandle.getCubes().add(cubeHandle); // measure group 创建多为数据集的度量 名称为"testMeasureGroup" MeasureGroupHandle measureGroupHandle = designHandle .getElementFactory().newTabularMeasureGroup("testMeasureGroup"); // designHandle.getElementFactory().newTabularMeasureGroup("testMeasureGroup" // ); cubeHandle.setProperty(ICubeModel.MEASURE_GROUPS_PROP, measureGroupHandle);// 设置为度量的多为数据集 // cubeHandle.add(ICubeModel.MEASURE_GROUPS_PROP, measureGroupHandle); // measure measureGroupHandle.add(MeasureGroupHandle.MEASURES_PROP, designFactory.newTabularMeasure(null)); MeasureHandle measure = (MeasureHandle) measureGroupHandle.getContent( MeasureGroupHandle.MEASURES_PROP, 0); // 设置 Measure measure.setName("CREDITLIMIT"); // 设置表达式 measure.setMeasureExpression("dataSetRow['CREDITLIMIT']"); // 设置计算函数 measure.setFunction(DesignChoiceConstants.MEASURE_FUNCTION_SUM); // 是否根据其他度量计算 measure.setCalculated(false); // 设置数据类型DesignChoiceConstants.COLUMN_DATA_TYPE_FLOAT measure.setDataType(DesignChoiceConstants.COLUMN_DATA_TYPE_FLOAT); // dimension 设置 未指定name TabularDimensionHandle dimension = designFactory .newTabularDimension(null); cubeHandle.add(TabularCubeHandle.DIMENSIONS_PROP, dimension); dimension.setTimeType(false); // dimension.setDefaultHierarchy( factory // .newTabularHierarchy( "testDefaultHierarchy" ) ); // hierarchy dimension.add(TabularDimensionHandle.HIERARCHIES_PROP, designFactory.newTabularHierarchy(null)); TabularHierarchyHandle hierarchy = (TabularHierarchyHandle) dimension .getContent(TabularDimensionHandle.HIERARCHIES_PROP, 0); // hierarchy.setName( namePrix + hierarchy.getName( ) ); hierarchy.setDataSet((DataSetHandle) designHandle.getDataSets().get(0)); // 添加组信息 hierarchy.add(TabularHierarchyHandle.LEVELS_PROP, designFactory.newTabularLevel(dimension, null)); TabularLevelHandle level = (TabularLevelHandle) hierarchy.getContent( TabularHierarchyHandle.LEVELS_PROP, 0); level.setName("testlevel"); level.setColumnName("CUSTOMERNAME"); level.setDataType(DesignChoiceConstants.COLUMN_DATA_TYPE_STRING); // 创建 交叉报表 ExtendedItemHandle xtab = CrosstabExtendedItemFactory .createCrosstabReportItem(designHandle, cubeHandle, "MyCrosstab"); // /xtab.setWidth(500); // xtab.setHeight(500); // 添加到body内 designHandle.getBody().add(xtab); IReportItem reportItem = xtab.getReportItem(); CrosstabReportItemHandle xtabHandle = (CrosstabReportItemHandle) reportItem; // ICrosstabConstants.COLUMN_AXIS_TYPE // ICrosstabConstants.ROW_AXIS_TYPE // xtabHandle.getMeasure(index) // DimensionViewHandle dvh = xtabHandle.insertDimension(dimension, // ICrosstabConstants.COLUMN_AXIS_TYPE, 0); DimensionViewHandle dvh = xtabHandle.insertDimension(dimension, ICrosstabConstants.ROW_AXIS_TYPE, 0); LevelViewHandle levelViewHandle = dvh.insertLevel(level, 0); CrosstabCellHandle cellHandle = levelViewHandle.getCell(); DesignElementHandle eii = xtabHandle.getModelHandle(); ComputedColumn bindingColumn = StructureFactory.newComputedColumn(eii, level.getName()); ComputedColumnHandle bindingHandle = ((ReportItemHandle) eii) .addColumnBinding(bindingColumn, false); bindingColumn.setDataType(level.getDataType()); String exp = "dimension['" + dimension.getName() + "']['" + level.getName() + "']"; bindingColumn.setExpression(exp); // xtabHandle.addGrandTotal(axisType, measureList, functionList) DataItemHandle dataHandle = designFactory.newDataItem(level.getName()); dataHandle.setResultSetColumn(bindingHandle.getName()); cellHandle.addContent(dataHandle); } void buildReport() throws IOException, SemanticException { DesignConfig config = new DesignConfig(); config.setBIRTHome("E:/birt-runtime-3_7_1/ReportEngine"); IDesignEngine engine = null; try { Platform.startup(config); IDesignEngineFactory factory = (IDesignEngineFactory) Platform .createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY); engine = factory.createDesignEngine(config); } catch (Exception ex) { ex.printStackTrace(); } SessionHandle session = engine.newSessionHandle(ULocale.ENGLISH); try { // open a design or a template designHandle = session.createDesign(); designFactory = designHandle.getElementFactory(); buildDataSource(); buildDataSet(); buidCube(); // Save the design and close it. output/desample/ designHandle.saveAs("output/birtsample/cubetest.rptdesign"); designHandle.close(); Platform.shutdown(); System.out.println("Finished"); } catch (Exception e) { e.printStackTrace(); } } } |
