1 9 10 package org.eclipse.birt.chart.examples.report.api; 11 12 import java.io.BufferedInputStream ; 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 17 import org.eclipse.birt.chart.model.ChartWithoutAxes; 18 import org.eclipse.birt.chart.model.attribute.DataPointComponent; 19 import org.eclipse.birt.chart.model.attribute.DataPointComponentType; 20 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl; 21 import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl; 22 import org.eclipse.birt.chart.model.attribute.impl.DataPointComponentImpl; 23 import org.eclipse.birt.chart.model.attribute.impl.GradientImpl; 24 import org.eclipse.birt.chart.model.attribute.impl.JavaNumberFormatSpecifierImpl; 25 import org.eclipse.birt.chart.model.component.Series; 26 import org.eclipse.birt.chart.model.component.impl.SeriesImpl; 27 import org.eclipse.birt.chart.model.data.BaseSampleData; 28 import org.eclipse.birt.chart.model.data.DataFactory; 29 import org.eclipse.birt.chart.model.data.OrthogonalSampleData; 30 import org.eclipse.birt.chart.model.data.Query; 31 import org.eclipse.birt.chart.model.data.SampleData; 32 import org.eclipse.birt.chart.model.data.SeriesDefinition; 33 import org.eclipse.birt.chart.model.data.impl.QueryImpl; 34 import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl; 35 import org.eclipse.birt.chart.model.impl.ChartWithoutAxesImpl; 36 import org.eclipse.birt.chart.model.type.PieSeries; 37 import org.eclipse.birt.chart.model.type.impl.PieSeriesImpl; 38 import org.eclipse.birt.report.model.api.CellHandle; 39 import org.eclipse.birt.report.model.api.DataItemHandle; 40 import org.eclipse.birt.report.model.api.DesignElementHandle; 41 import org.eclipse.birt.report.model.api.DesignEngine; 42 import org.eclipse.birt.report.model.api.ElementFactory; 43 import org.eclipse.birt.report.model.api.ExtendedItemHandle; 44 import org.eclipse.birt.report.model.api.GridHandle; 45 import org.eclipse.birt.report.model.api.ImageHandle; 46 import org.eclipse.birt.report.model.api.LabelHandle; 47 import org.eclipse.birt.report.model.api.PropertyHandle; 48 import org.eclipse.birt.report.model.api.ReportDesignHandle; 49 import org.eclipse.birt.report.model.api.RowHandle; 50 import org.eclipse.birt.report.model.api.ScriptDataSetHandle; 51 import org.eclipse.birt.report.model.api.ScriptDataSourceHandle; 52 import org.eclipse.birt.report.model.api.SessionHandle; 53 import org.eclipse.birt.report.model.api.StructureFactory; 54 import org.eclipse.birt.report.model.api.StyleHandle; 55 import org.eclipse.birt.report.model.api.TableGroupHandle; 56 import org.eclipse.birt.report.model.api.TableHandle; 57 import org.eclipse.birt.report.model.api.activity.SemanticException; 58 import org.eclipse.birt.report.model.api.command.ContentException; 59 import org.eclipse.birt.report.model.api.command.NameException; 60 import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants; 61 import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn; 62 import org.eclipse.birt.report.model.api.elements.structures.EmbeddedImage; 63 import org.eclipse.birt.report.model.api.elements.structures.SortKey; 64 import org.eclipse.birt.report.model.api.extension.ExtendedElementException; 65 import org.eclipse.birt.report.model.api.metadata.IMetaDataDictionary; 66 67 import com.ibm.icu.util.ULocale; 68 69 public class SalesReport 70 { 71 72 ReportDesignHandle reportDesignHandle = null; 73 74 ElementFactory elementFactory = null; 75 76 IMetaDataDictionary dict = null; 77 78 ComputedColumn cs1, cs2, cs3 = null; 79 80 public static void main( String [] args ) throws SemanticException, 81 IOException 82 { 83 new SalesReport( ).createReport( ); 84 } 85 86 void createReport( ) throws SemanticException, IOException 87 { 88 SessionHandle session = new DesignEngine( null ).newSessionHandle( (ULocale) null ); 90 91 reportDesignHandle = session.createDesign( ); 93 94 elementFactory = reportDesignHandle.getElementFactory( ); 96 97 dict = new DesignEngine( null ).getMetaData( ); 98 99 createMasterPages( ); 100 createDataSources( ); 101 createDataSets( ); 102 createStyles( ); 103 createBody( ); 104 105 String outputPath = "output"; File outputFolder = new File ( outputPath ); 107 if ( !outputFolder.exists( ) && !outputFolder.mkdir( ) ) 108 { 109 throw new IOException ( "Can not create the output folder" ); } 111 reportDesignHandle.saveAs( outputPath + "/" + "SalesReport.rptdesign" ); } 113 114 private void createDataSources( ) throws SemanticException 115 { 116 ScriptDataSourceHandle dataSourceHandle = elementFactory.newScriptDataSource( "Data Source" ); reportDesignHandle.getDataSources( ).add( dataSourceHandle ); 118 } 119 120 private void createDataSets( ) throws SemanticException 121 { 122 ScriptDataSetHandle dataSetHandle = elementFactory.newScriptDataSet( "Data Set" ); dataSetHandle.setDataSource( "Data Source" ); 126 dataSetHandle.setOpen( "i=0;" + "sourcedata = new Array( new Array(3), new Array(3), new Array(3), " + "new Array(3), new Array(3), new Array(3));" + "sourcedata[0][0] = 10; " + "sourcedata[0][1] = \"Chris Kwai\";" + "sourcedata[0][2] = 2413;" 134 + "sourcedata[1][0] = 10; " + "sourcedata[1][1] = \"Ice Bella\";" + "sourcedata[1][2] = 2304;" 138 + "sourcedata[2][0] = 10; " + "sourcedata[2][1] = \"Nola Dicci\";" + "sourcedata[2][2] = 1998;" 142 + "sourcedata[3][0] = 11; " + "sourcedata[3][1] = \"Chris Kwai\";" + "sourcedata[3][2] = 2087;" 146 + "sourcedata[4][0] = 11; " + "sourcedata[4][1] = \"Ice Bella\";" + "sourcedata[4][2] = 2502;" 150 + "sourcedata[5][0] = 11; " + "sourcedata[5][1] = \"Nola Dicci\";" + "sourcedata[5][2] = 2011;" ); 154 dataSetHandle.setFetch( "if ( i < 6 ){" + "row[\"Month\"] = sourcedata[i][0];" + "row[\"Product\"] = sourcedata[i][1];" + "row[\"Amount\"] = sourcedata[i][2];" + "i++;" + "return true;}" + "else return false;" ); 162 cs1 = StructureFactory.createComputedColumn( ); 164 cs1.setName( "Month" ); cs1.setExpression( "row[\"Month\"]" ); cs1.setDataType( "integer" ); 168 cs2 = StructureFactory.createComputedColumn( ); 169 cs2.setName( "Product" ); cs2.setExpression( "row[\"Product\"]" ); cs2.setDataType( "string" ); 173 cs3 = StructureFactory.createComputedColumn( ); 174 cs3.setName( "Amount" ); cs3.setExpression( "row[\"Amount\"]" ); cs3.setDataType( "integer" ); 178 PropertyHandle computedSet = dataSetHandle.getPropertyHandle( ScriptDataSetHandle.COMPUTED_COLUMNS_PROP ); 179 computedSet.addItem( cs1 ); 180 computedSet.addItem( cs2 ); 181 computedSet.addItem( cs3 ); 182 183 reportDesignHandle.getDataSets( ).add( dataSetHandle ); 184 } 185 186 private void createMasterPages( ) throws ContentException, NameException 187 { 188 DesignElementHandle simpleMasterPage = elementFactory.newSimpleMasterPage( "Master Page" ); reportDesignHandle.getMasterPages( ).add( simpleMasterPage ); 190 } 191 192 private void createStyles( ) throws SemanticException 193 { 194 StyleHandle labelStyle = elementFactory.newStyle( "Label" ); labelStyle.setProperty( StyleHandle.FONT_WEIGHT_PROP, 196 DesignChoiceConstants.FONT_WEIGHT_BOLD ); 197 labelStyle.setProperty( StyleHandle.FONT_FAMILY_PROP, "Arial Black" ); labelStyle.setProperty( StyleHandle.COLOR_PROP, "#008000" ); 200 StyleHandle dataStyle = elementFactory.newStyle( "Data" ); dataStyle.setProperty( StyleHandle.FONT_WEIGHT_PROP, 202 DesignChoiceConstants.FONT_WEIGHT_BOLD ); 203 dataStyle.setProperty( StyleHandle.FONT_FAMILY_PROP, "Century" ); dataStyle.setProperty( StyleHandle.COLOR_PROP, "#009B9B" ); 206 reportDesignHandle.getStyles( ).add( labelStyle ); 207 reportDesignHandle.getStyles( ).add( dataStyle ); 208 } 209 210 private byte[] load( String fileName ) throws IOException 211 { 212 InputStream is = null; 213 214 is = new BufferedInputStream ( this.getClass( ) 215 .getResourceAsStream( fileName ) ); 216 byte data[] = null; 217 if ( is != null ) 218 { 219 try 220 { 221 data = new byte[is.available( )]; 222 is.read( data ); 223 } 224 catch ( IOException e1 ) 225 { 226 throw e1; 227 } 228 } 229 return data; 230 } 231 232 private void createBody( ) throws SemanticException 233 { 234 235 GridHandle grid1 = elementFactory.newGridItem( "grid1", 3, 1 ); grid1.setWidth( "100%" ); reportDesignHandle.getBody( ).add( grid1 ); 239 240 RowHandle row = (RowHandle) grid1.getRows( ).get( 0 ); 242 243 CellHandle cell = (CellHandle) row.getCells( ).get( 0 ); 245 cell.setProperty( StyleHandle.TEXT_ALIGN_PROP, 246 DesignChoiceConstants.TEXT_ALIGN_CENTER ); 247 try 248 { 249 EmbeddedImage image1 = StructureFactory.createEmbeddedImage( ); 250 image1.setType( DesignChoiceConstants.IMAGE_TYPE_IMAGE_JPEG ); 251 image1.setData( load( "logo1.jpg" ) ); image1.setName( "logo1" ); reportDesignHandle.addImage( image1 ); 254 ImageHandle imageHandle = elementFactory.newImage( "handle" ); imageHandle.setSource( DesignChoiceConstants.IMAGE_REF_TYPE_EMBED ); 256 imageHandle.setImageName( "logo1" ); cell.getContent( ).add( imageHandle ); 258 } 259 catch ( IOException e ) 260 { 261 e.printStackTrace( ); 262 } 263 264 cell = (CellHandle) row.getCells( ).get( 1 ); 265 cell.setProperty( StyleHandle.TEXT_ALIGN_PROP, 266 DesignChoiceConstants.TEXT_ALIGN_CENTER ); 267 try 268 { 269 EmbeddedImage image2 = StructureFactory.createEmbeddedImage( ); 270 image2.setType( DesignChoiceConstants.IMAGE_TYPE_IMAGE_JPEG ); 271 image2.setData( load( "logo2.jpg" ) ); image2.setName( "logo2" ); reportDesignHandle.addImage( image2 ); 274 ImageHandle imageHandle2 = elementFactory.newImage( "imageHandle2" ); imageHandle2.setSource( DesignChoiceConstants.IMAGE_REF_TYPE_EMBED ); 276 imageHandle2.setImageName( "logo2" ); cell.getContent( ).add( imageHandle2 ); 278 } 279 catch ( IOException e ) 280 { 281 e.printStackTrace( ); 282 } 283 284 cell = (CellHandle) row.getCells( ).get( 2 ); 285 cell.setProperty( StyleHandle.TEXT_ALIGN_PROP, 286 DesignChoiceConstants.TEXT_ALIGN_CENTER ); 287 try 288 { 289 EmbeddedImage image3 = StructureFactory.createEmbeddedImage( ); 290 image3.setType( DesignChoiceConstants.IMAGE_TYPE_IMAGE_JPEG ); 291 image3.setData( load( "logo3.jpg" ) ); image3.setName( "logo3" ); reportDesignHandle.addImage( image3 ); 294 ImageHandle imageHandle3 = elementFactory.newImage( "imageHandle3" ); imageHandle3.setSource( DesignChoiceConstants.IMAGE_REF_TYPE_EMBED ); 296 imageHandle3.setImageName( "logo3" ); cell.getContent( ).add( imageHandle3 ); 298 } 299 catch ( IOException e ) 300 { 301 e.printStackTrace( ); 302 } 303 304 GridHandle grid2 = elementFactory.newGridItem( "grid2", 2, 1 ); grid2.setWidth( "100%" ); reportDesignHandle.getBody( ).add( grid2 ); 308 309 row = (RowHandle) grid2.getRows( ).get( 0 ); 311 312 cell = (CellHandle) row.getCells( ).get( 0 ); 314 cell.setProperty( StyleHandle.TEXT_ALIGN_PROP, 315 DesignChoiceConstants.TEXT_ALIGN_RIGHT ); 316 cell.setProperty( StyleHandle.VERTICAL_ALIGN_PROP, 317 DesignChoiceConstants.VERTICAL_ALIGN_MIDDLE ); 318 319 TableHandle table = elementFactory.newTableItem( null, 3, 1, 1, 1 ); 320 table.setProperty( StyleHandle.TEXT_ALIGN_PROP, 321 DesignChoiceConstants.TEXT_ALIGN_CENTER ); 322 table.setWidth( "80%" ); table.setProperty( TableHandle.DATA_SET_PROP, "Data Set" ); 325 PropertyHandle computedSet = table.getColumnBindings( ); 326 cs1.setExpression( "dataSetRow[\"Month\"]" ); computedSet.addItem( cs1 ); 328 cs2.setExpression( "dataSetRow[\"Product\"]" ); computedSet.addItem( cs2 ); 330 cs3.setExpression( "dataSetRow[\"Amount\"]" ); computedSet.addItem( cs3 ); 332 333 SortKey key = StructureFactory.createSortKey( ); 335 key.setKey( "row[\"Month\"]" ); key.setDirection( "asc" ); PropertyHandle sort = table.getPropertyHandle( TableHandle.SORT_PROP ); 338 sort.addItem( key ); 339 340 RowHandle header = (RowHandle) table.getHeader( ).get( 0 ); 342 343 CellHandle tcell = (CellHandle) header.getCells( ).get( 0 ); 344 LabelHandle label = elementFactory.newLabel( null ); 345 label.setText( "Product" ); label.setStyleName( "Label" ); tcell.getContent( ).add( label ); 348 349 tcell = (CellHandle) header.getCells( ).get( 1 ); 350 label = elementFactory.newLabel( null ); 351 label.setText( "Month" ); label.setStyleName( "Label" ); tcell.getContent( ).add( label ); 354 355 tcell = (CellHandle) header.getCells( ).get( 2 ); 356 label = elementFactory.newLabel( null ); 357 label.setText( "Amount" ); label.setStyleName( "Label" ); tcell.getContent( ).add( label ); 360 361 TableGroupHandle group = elementFactory.newTableGroup( ); 363 group.setKeyExpr( "row[\"Product\"]" ); table.getGroups( ).add( group ); 365 366 RowHandle groupHeader = elementFactory.newTableRow( 3 ); 367 tcell = (CellHandle) groupHeader.getCells( ).get( 0 ); 368 tcell.setDrop( DesignChoiceConstants.DROP_TYPE_DETAIL ); 369 DataItemHandle data = elementFactory.newDataItem( null ); 370 data.setStyleName( "Data" ); data.setResultSetColumn( cs2.getName( ) ); 372 tcell.getContent( ).add( data ); 373 group.getHeader( ).add( groupHeader ); 374 375 RowHandle groupFooter = elementFactory.newTableRow( 3 ); 376 tcell = (CellHandle) groupFooter.getCells( ).get( 0 ); 377 tcell.setProperty( StyleHandle.BORDER_BOTTOM_COLOR_PROP, "#FF8000" ); tcell.setProperty( StyleHandle.BORDER_BOTTOM_STYLE_PROP, 379 DesignChoiceConstants.LINE_STYLE_SOLID ); 380 tcell.setProperty( StyleHandle.BORDER_BOTTOM_WIDTH_PROP, 381 DesignChoiceConstants.LINE_WIDTH_THIN ); 382 tcell = (CellHandle) groupFooter.getCells( ).get( 1 ); 383 tcell.setProperty( StyleHandle.BORDER_BOTTOM_COLOR_PROP, "#FF8000" ); tcell.setProperty( StyleHandle.BORDER_BOTTOM_STYLE_PROP, 385 DesignChoiceConstants.LINE_STYLE_SOLID ); 386 tcell.setProperty( StyleHandle.BORDER_BOTTOM_WIDTH_PROP, 387 DesignChoiceConstants.LINE_WIDTH_THIN ); 388 tcell = (CellHandle) groupFooter.getCells( ).get( 2 ); 389 tcell.setProperty( StyleHandle.BORDER_BOTTOM_COLOR_PROP, "#FF8000" ); tcell.setProperty( StyleHandle.BORDER_BOTTOM_STYLE_PROP, 391 DesignChoiceConstants.LINE_STYLE_SOLID ); 392 tcell.setProperty( StyleHandle.BORDER_BOTTOM_WIDTH_PROP, 393 DesignChoiceConstants.LINE_WIDTH_THIN ); 394 group.getFooter( ).add( groupFooter ); 395 396 RowHandle detail = (RowHandle) table.getDetail( ).get( 0 ); 398 tcell = (CellHandle) detail.getCells( ).get( 1 ); 399 data = elementFactory.newDataItem( null ); 400 data.setStyleName( "Data" ); data.setResultSetColumn( cs1.getName( ) ); 402 tcell.getContent( ).add( data ); 403 404 tcell = (CellHandle) detail.getCells( ).get( 2 ); 405 data = elementFactory.newDataItem( null ); 406 data.setStyleName( "Data" ); data.setResultSetColumn( cs3.getName( ) ); 408 tcell.getContent( ).add( data ); 409 410 cell.getContent( ).add( table ); 411 412 cell = (CellHandle) row.getCells( ).get( 1 ); 413 cell.setProperty( StyleHandle.TEXT_ALIGN_PROP, 414 DesignChoiceConstants.TEXT_ALIGN_CENTER ); 415 cell.getContent( ).add( createPieChart( ) ); 416 } 417 418 private ExtendedItemHandle createPieChart( ) 419 { 420 ExtendedItemHandle eih = elementFactory.newExtendedItem( null, "Chart" ); 422 try 423 { 424 eih.setHeight( "288pt" ); eih.setWidth( "252pt" ); eih.setProperty( ExtendedItemHandle.DATA_SET_PROP, "Data Set" ); PropertyHandle computedSet = eih.getColumnBindings( ); 428 cs1.setExpression( "dataSetRow[\"Month\"]" ); computedSet.addItem( cs1 ); 430 cs2.setExpression( "dataSetRow[\"Product\"]" ); computedSet.addItem( cs2 ); 432 cs3.setExpression( "dataSetRow[\"Amount\"]" ); computedSet.addItem( cs3 ); 434 } 435 catch ( SemanticException e ) 436 { 437 e.printStackTrace( ); 438 } 439 440 ChartWithoutAxes cwoaPie = ChartWithoutAxesImpl.create( ); 441 cwoaPie.setType( "Pie Chart" ); cwoaPie.setSubType( "Standard Pie Chart" ); cwoaPie.getTitle( ).setVisible( false ); 444 cwoaPie.getBlock( ).setBounds( BoundsImpl.create( 0, 0, 252, 288 ) ); 445 cwoaPie.getBlock( ).getOutline( ).setVisible( true ); 446 cwoaPie.getBlock( ) 447 .setBackground( GradientImpl.create( ColorDefinitionImpl.create( 204, 448 254, 449 204 ), 450 ColorDefinitionImpl.create( 254, 226, 240 ), 451 -35, 452 false ) ); 453 cwoaPie.getPlot( ) 454 .getClientArea( ) 455 .setBackground( ColorDefinitionImpl.TRANSPARENT( ) ); 456 cwoaPie.getLegend( ).setBackground( ColorDefinitionImpl.TRANSPARENT( ) ); 457 cwoaPie.getLegend( ) 458 .getClientArea( ) 459 .setBackground( ColorDefinitionImpl.TRANSPARENT( ) ); 460 461 SampleData sd = DataFactory.eINSTANCE.createSampleData( ); 462 BaseSampleData sdBase = DataFactory.eINSTANCE.createBaseSampleData( ); 463 sdBase.setDataSetRepresentation( "Category-A, Category-B" ); sd.getBaseSampleData( ).add( sdBase ); 465 466 OrthogonalSampleData sdOrthogonal = DataFactory.eINSTANCE.createOrthogonalSampleData( ); 467 sdOrthogonal.setDataSetRepresentation( "4,12" ); sdOrthogonal.setSeriesDefinitionIndex( 0 ); 469 sd.getOrthogonalSampleData( ).add( sdOrthogonal ); 470 471 cwoaPie.setSampleData( sd ); 472 473 Series seCategory = SeriesImpl.create( ); 474 Query query = QueryImpl.create( "row[\"Product\"]" ); seCategory.getDataDefinition( ).add( query ); 476 477 SeriesDefinition series = SeriesDefinitionImpl.create( ); 478 series.getSeries( ).add( seCategory ); 479 cwoaPie.getSeriesDefinitions( ).add( series ); 480 481 PieSeries ps = (PieSeries) PieSeriesImpl.create( ); 482 Query query2 = QueryImpl.create( "row[\"Amount\"]" ); ps.getDataDefinition( ).add( query2 ); 484 485 SeriesDefinition seGroup = SeriesDefinitionImpl.create( ); 486 Query query1 = QueryImpl.create( "row[\"Month\"]" ); seGroup.setQuery( query1 ); 488 series.getSeriesPalette( ).update( -2 ); 489 series.getSeriesDefinitions( ).add( seGroup ); 490 seGroup.getSeries( ).add( ps ); 491 492 DataPointComponent dpc = DataPointComponentImpl.create( DataPointComponentType.ORTHOGONAL_VALUE_LITERAL, 493 JavaNumberFormatSpecifierImpl.create( "###,###" ) ); ps.getDataPoint( ).getComponents( ).clear( ); 495 ps.getDataPoint( ).getComponents( ).add( dpc ); 496 ps.getLabel( ).setVisible( true ); 497 498 try 499 { 500 eih.getReportItem( ).setProperty( "chart.instance", cwoaPie ); } 503 catch ( ExtendedElementException e ) 504 { 505 e.printStackTrace( ); 506 } 507 508 return eih; 509 } 510 } | Popular Tags |