1 9 10 package org.eclipse.birt.chart.examples.report.api; 11 12 import java.io.File ; 13 import java.io.IOException ; 14 import java.util.ArrayList ; 15 16 import org.eclipse.birt.chart.model.ChartWithAxes; 17 import org.eclipse.birt.chart.model.attribute.AxisType; 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.LineStyle; 21 import org.eclipse.birt.chart.model.attribute.Orientation; 22 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl; 23 import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl; 24 import org.eclipse.birt.chart.model.attribute.impl.DataPointComponentImpl; 25 import org.eclipse.birt.chart.model.attribute.impl.JavaDateFormatSpecifierImpl; 26 import org.eclipse.birt.chart.model.attribute.impl.JavaNumberFormatSpecifierImpl; 27 import org.eclipse.birt.chart.model.component.Axis; 28 import org.eclipse.birt.chart.model.component.Series; 29 import org.eclipse.birt.chart.model.component.impl.SeriesImpl; 30 import org.eclipse.birt.chart.model.data.BaseSampleData; 31 import org.eclipse.birt.chart.model.data.DataFactory; 32 import org.eclipse.birt.chart.model.data.OrthogonalSampleData; 33 import org.eclipse.birt.chart.model.data.Query; 34 import org.eclipse.birt.chart.model.data.SampleData; 35 import org.eclipse.birt.chart.model.data.SeriesDefinition; 36 import org.eclipse.birt.chart.model.data.impl.NumberDataElementImpl; 37 import org.eclipse.birt.chart.model.data.impl.QueryImpl; 38 import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl; 39 import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl; 40 import org.eclipse.birt.chart.model.type.BarSeries; 41 import org.eclipse.birt.chart.model.type.StockSeries; 42 import org.eclipse.birt.chart.model.type.impl.BarSeriesImpl; 43 import org.eclipse.birt.chart.model.type.impl.StockSeriesImpl; 44 import org.eclipse.birt.report.model.api.CellHandle; 45 import org.eclipse.birt.report.model.api.DesignElementHandle; 46 import org.eclipse.birt.report.model.api.DesignEngine; 47 import org.eclipse.birt.report.model.api.ElementFactory; 48 import org.eclipse.birt.report.model.api.ExtendedItemHandle; 49 import org.eclipse.birt.report.model.api.GridHandle; 50 import org.eclipse.birt.report.model.api.LabelHandle; 51 import org.eclipse.birt.report.model.api.MasterPageHandle; 52 import org.eclipse.birt.report.model.api.PropertyHandle; 53 import org.eclipse.birt.report.model.api.ReportDesignHandle; 54 import org.eclipse.birt.report.model.api.RowHandle; 55 import org.eclipse.birt.report.model.api.ScriptDataSetHandle; 56 import org.eclipse.birt.report.model.api.ScriptDataSourceHandle; 57 import org.eclipse.birt.report.model.api.SessionHandle; 58 import org.eclipse.birt.report.model.api.StructureFactory; 59 import org.eclipse.birt.report.model.api.StyleHandle; 60 import org.eclipse.birt.report.model.api.activity.SemanticException; 61 import org.eclipse.birt.report.model.api.command.ContentException; 62 import org.eclipse.birt.report.model.api.command.NameException; 63 import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants; 64 import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn; 65 import org.eclipse.birt.report.model.api.extension.ExtendedElementException; 66 import org.eclipse.birt.report.model.api.metadata.IMetaDataDictionary; 67 68 import com.ibm.icu.util.ULocale; 69 70 public class StockReport 71 { 72 73 ReportDesignHandle reportDesignHandle = null; 74 75 ElementFactory elementFactory = null; 76 77 StructureFactory structFactory = null; 78 79 IMetaDataDictionary dict = null; 80 81 ComputedColumn cs1, cs2, cs3, cs4, cs5, cs6 = null; 82 83 public static void main( String [] args ) throws SemanticException, 84 IOException 85 { 86 new StockReport( ).createReport( ); 87 } 88 89 void createReport( ) throws SemanticException, IOException 90 { 91 SessionHandle session = new DesignEngine( null ).newSessionHandle( (ULocale) null ); 93 94 reportDesignHandle = session.createDesign( ); 96 97 elementFactory = reportDesignHandle.getElementFactory( ); 99 100 structFactory = new StructureFactory( ); 101 102 dict = new DesignEngine( null ).getMetaData( ); 103 104 createMasterPages( ); 105 createDataSources( ); 106 createDataSets( ); 107 createBody( ); 108 109 String outputPath = "output"; File outputFolder = new File ( outputPath ); 111 if ( !outputFolder.exists( ) && !outputFolder.mkdir( ) ) 112 { 113 throw new IOException ( "Can not create the output folder" ); } 115 reportDesignHandle 116 .saveAs( outputPath + "/" + "StockAnalysis.rptdesign" ); } 118 119 private void createDataSources( ) throws SemanticException 120 { 121 ScriptDataSourceHandle dataSourceHandle = elementFactory 122 .newScriptDataSource( "Data Source" ); reportDesignHandle.getDataSources( ).add( dataSourceHandle ); 124 } 125 126 private void createDataSets( ) throws SemanticException 127 { 128 ScriptDataSetHandle dataSetHandle = elementFactory 130 .newScriptDataSet( "Data Set" ); dataSetHandle.setDataSource( "Data Source" ); 133 dataSetHandle 135 .setOpen( "i=0;" + "sourcedata = new Array( new Array(6), new Array(6), new Array(6), new Array(6)," + "new Array(6), new Array(6), new Array(6), new Array(6));" + "sourcedata[0][0] = \"3/1/2005\"; " + "sourcedata[0][1] = 2.77;" + "sourcedata[0][2] = 2.73;" + "sourcedata[0][3] = 2.69; " + "sourcedata[0][4] = 2.71;" + "sourcedata[0][5] = 341900;" 145 + "sourcedata[1][0] = \"3/2/2005\"; " + "sourcedata[1][1] = 2.8;" + "sourcedata[1][2] = 2.64;" + "sourcedata[1][3] = 2.6; " + "sourcedata[1][4] = 2.71;" + "sourcedata[1][5] = 249900;" 152 + "sourcedata[2][0] = \"3/3/2005\"; " + "sourcedata[2][1] = 2.6;" + "sourcedata[2][2] = 2.28;" + "sourcedata[2][3] = 2.13; " + "sourcedata[2][4] = 2.59;" + "sourcedata[2][5] = 394800;" 159 + "sourcedata[3][0] = \"3/4/2005\"; " + "sourcedata[3][1] = 2.87;" + "sourcedata[3][2] = 2.87;" + "sourcedata[3][3] = 2.03; " + "sourcedata[3][4] = 2.21;" + "sourcedata[3][5] = 358200;" 166 + "sourcedata[4][0] = \"3/5/2005\"; " + "sourcedata[4][1] = 2.48;" + "sourcedata[4][2] = 2.26;" + "sourcedata[4][3] = 2.16; " + "sourcedata[4][4] = 2.39;" + "sourcedata[4][5] = 339000;" 173 + "sourcedata[5][0] = \"3/6/2005\"; " + "sourcedata[5][1] = 2.98;" + "sourcedata[5][2] = 2.86;" + "sourcedata[5][3] = 2.04; " + "sourcedata[5][4] = 2.19;" + "sourcedata[5][5] = 221000;" 180 + "sourcedata[6][0] = \"3/7/2005\"; " + "sourcedata[6][1] = 2.87;" + "sourcedata[6][2] = 2.17;" + "sourcedata[6][3] = 2.14; " + "sourcedata[6][4] = 2.62;" + "sourcedata[6][5] = 183600;" 187 + "sourcedata[7][0] = \"3/8/2005\"; " + "sourcedata[7][1] = 2.8;" + "sourcedata[7][2] = 2.78;" + "sourcedata[7][3] = 2.65; " + "sourcedata[7][4] = 2.66;" + "sourcedata[7][5] = 194800;" ); 194 dataSetHandle.setFetch( "if ( i < 8 ){" + "row[\"Date\"] = sourcedata[i][0];" + "row[\"High\"] = sourcedata[i][1];" + "row[\"Close\"] = sourcedata[i][2];" + "row[\"Low\"] = sourcedata[i][3];" + "row[\"Open\"] = sourcedata[i][4];" + "row[\"Volume\"] = sourcedata[i][5];" + "i++;" + "return true;}" + "else return false;" ); 204 cs1 = StructureFactory.createComputedColumn( ); 206 cs1.setName( "Date" ); cs1.setExpression( "row[\"Date\"]" ); cs1.setDataType( "date-time" ); 210 cs2 = StructureFactory.createComputedColumn( ); 211 cs2.setName( "High" ); cs2.setExpression( "row[\"High\"]" ); cs2.setDataType( "float" ); 215 cs3 = StructureFactory.createComputedColumn( ); 216 cs3.setName( "Close" ); cs3.setExpression( "row[\"Close\"]" ); cs3.setDataType( "float" ); 220 cs4 = StructureFactory.createComputedColumn( ); 221 cs4.setName( "Low" ); cs4.setExpression( "row[\"Low\"]" ); cs4.setDataType( "float" ); 225 cs5 = StructureFactory.createComputedColumn( ); 226 cs5.setName( "Open" ); cs5.setExpression( "row[\"Open\"]" ); cs5.setDataType( "float" ); 230 cs6 = StructureFactory.createComputedColumn( ); 231 cs6.setName( "Volume" ); cs6.setExpression( "row[\"Volume\"]" ); cs6.setDataType( "integer" ); 235 PropertyHandle computedSet = dataSetHandle.getPropertyHandle( ScriptDataSetHandle.COMPUTED_COLUMNS_PROP ); 236 computedSet.addItem( cs1 ); 237 computedSet.addItem( cs2 ); 238 computedSet.addItem( cs3 ); 239 computedSet.addItem( cs4 ); 240 computedSet.addItem( cs5 ); 241 computedSet.addItem( cs6 ); 242 243 reportDesignHandle.getDataSets( ).add( dataSetHandle ); 244 } 245 246 private void createMasterPages( ) throws ContentException, NameException 247 { 248 DesignElementHandle simpleMasterPage = elementFactory 249 .newSimpleMasterPage( "Master Page" ); try 251 { 252 simpleMasterPage.setProperty( MasterPageHandle.LEFT_MARGIN_PROP, 253 "0.2in" ); simpleMasterPage.setProperty( MasterPageHandle.RIGHT_MARGIN_PROP, 255 "0.2in" ); } 257 catch ( SemanticException e ) 258 { 259 e.printStackTrace( ); 260 } 261 reportDesignHandle.getMasterPages( ).add( simpleMasterPage ); 262 } 263 264 private void createBody( ) throws SemanticException 265 { 266 GridHandle mainGrid = elementFactory.newGridItem( "main", 1, 3 ); mainGrid.setWidth( "100%" ); reportDesignHandle.getBody( ).add( mainGrid ); 269 270 RowHandle row1 = (RowHandle) mainGrid.getRows( ).get( 0 ); 272 row1.setProperty( StyleHandle.BACKGROUND_COLOR_PROP, "#FEFBE9" ); 274 CellHandle row1Cell = (CellHandle) row1.getCells( ).get( 0 ); 276 277 LabelHandle label = elementFactory.newLabel( null ); 279 label.setText( "Corporation Stock" ); label.setProperty( StyleHandle.FONT_SIZE_PROP, 281 DesignChoiceConstants.FONT_SIZE_X_LARGE ); 282 label.setProperty( StyleHandle.FONT_FAMILY_PROP, "Arial Black" ); label.setProperty( StyleHandle.COLOR_PROP, "#6E6E6E" ); label.setProperty( StyleHandle.PADDING_BOTTOM_PROP, "0.5in" ); label.setProperty( StyleHandle.TEXT_ALIGN_PROP, 286 DesignChoiceConstants.TEXT_ALIGN_CENTER ); 287 288 row1Cell.getContent( ).add( label ); 289 290 RowHandle row2 = (RowHandle) mainGrid.getRows( ).get( 1 ); 292 293 CellHandle row2Cell1 = (CellHandle) row2.getCells( ).get( 0 ); 294 row2Cell1.getContent( ).add( createStockChart( ) ); 295 296 RowHandle row3 = (RowHandle) mainGrid.getRows( ).get( 2 ); 298 299 CellHandle row3Cell1 = (CellHandle) row3.getCells( ).get( 0 ); 301 row3Cell1.getContent( ).add( createVolumeChart( ) ); 302 } 303 304 private ExtendedItemHandle createStockChart( ) 305 { 306 ExtendedItemHandle eih = elementFactory.newExtendedItem( null, "Chart" ); 308 try 309 { 310 eih.setHeight( "175pt" ); eih.setWidth( "450pt" ); eih.setProperty( ExtendedItemHandle.DATA_SET_PROP, "Data Set" ); PropertyHandle computedSet = eih.getColumnBindings( ); 314 cs1.setExpression( "dataSetRow[\"Date\"]" ); computedSet.addItem( cs1 ); 316 cs2.setExpression( "dataSetRow[\"High\"]" ); computedSet.addItem( cs2 ); 318 cs3.setExpression( "dataSetRow[\"Close\"]" ); computedSet.addItem( cs3 ); 320 cs4.setExpression( "dataSetRow[\"Low\"]" ); computedSet.addItem( cs4 ); 322 cs5.setExpression( "dataSetRow[\"Open\"]" ); computedSet.addItem( cs5 ); 324 cs6.setExpression( "dataSetRow[\"Volume\"]" ); computedSet.addItem( cs6 ); 326 } 327 catch ( SemanticException e ) 328 { 329 e.printStackTrace( ); 330 } 331 332 ChartWithAxes cwaStock = ChartWithAxesImpl.create( ); 333 cwaStock.setType( "Stock Chart" ); cwaStock.setSubType( "Standard Stock Chart" ); cwaStock.getTitle( ).setVisible( false ); 336 cwaStock.getLegend( ).setVisible( false ); 337 cwaStock.setOrientation( Orientation.VERTICAL_LITERAL ); 338 cwaStock.getBlock( ).setBounds( BoundsImpl.create( 0, 0, 450, 175 ) ); 339 cwaStock.getPlot( ).getClientArea( ).getOutline( ).setVisible( true ); 340 cwaStock.getPlot( ).getClientArea( ).setBackground( 341 ColorDefinitionImpl.create( 254, 251, 233 ) ); 342 343 Axis xAxisPrimary = cwaStock.getPrimaryBaseAxes( )[0]; 344 xAxisPrimary.setCategoryAxis( true ); 345 xAxisPrimary.getTitle( ).getCaption( ).setValue( "Trading Date" ); xAxisPrimary.getMajorGrid( ).getLineAttributes( ).setStyle( 347 LineStyle.DOTTED_LITERAL ); 348 xAxisPrimary.getMajorGrid( ).getLineAttributes( ).setVisible( true ); 349 xAxisPrimary.getMajorGrid( ).getLineAttributes( ).setColor( 350 ColorDefinitionImpl.GREY( ) ); 351 xAxisPrimary.setType( AxisType.DATE_TIME_LITERAL ); 352 xAxisPrimary.setFormatSpecifier( JavaDateFormatSpecifierImpl 353 .create( "MM/dd/yyyy" ) ); 355 Axis yAxisPrimary = cwaStock.getPrimaryOrthogonalAxis( xAxisPrimary ); 356 yAxisPrimary.getTitle( ).getCaption( ).setValue( "Price" ); yAxisPrimary.getMajorGrid( ).getLineAttributes( ).setStyle( 358 LineStyle.DOTTED_LITERAL ); 359 yAxisPrimary.getMajorGrid( ).getLineAttributes( ).setVisible( true ); 360 yAxisPrimary.getMajorGrid( ).getLineAttributes( ).setColor( 361 ColorDefinitionImpl.GREY( ) ); 362 yAxisPrimary.getScale( ).setMin( NumberDataElementImpl.create( 2.0 ) ); 363 yAxisPrimary.getScale( ).setMax( NumberDataElementImpl.create( 3.0 ) ); 364 yAxisPrimary.getScale( ).setStep( 0.2 ); 365 366 SampleData sd = DataFactory.eINSTANCE.createSampleData( ); 367 BaseSampleData sdBase = DataFactory.eINSTANCE.createBaseSampleData( ); 368 sdBase.setDataSetRepresentation( "01/25/2005" ); sd.getBaseSampleData( ).add( sdBase ); 370 371 OrthogonalSampleData sdOrthogonal = DataFactory.eINSTANCE 372 .createOrthogonalSampleData( ); 373 sdOrthogonal.setDataSetRepresentation( "H5.3 L1.3 O4.5 C3.4" ); sdOrthogonal.setSeriesDefinitionIndex( 0 ); 375 sd.getOrthogonalSampleData( ).add( sdOrthogonal ); 376 377 cwaStock.setSampleData( sd ); 378 379 Series seCategory = SeriesImpl.create( ); 380 Query query = QueryImpl.create( "row[\"Date\"]" ); seCategory.getDataDefinition( ).add( query ); 382 383 SeriesDefinition sdX = SeriesDefinitionImpl.create( ); 384 sdX.getSeries( ).add( seCategory ); 385 xAxisPrimary.getSeriesDefinitions( ).add( sdX ); 386 387 StockSeries ss = (StockSeries) StockSeriesImpl.create( ); 388 ss.setSeriesIdentifier( "Stock Price" ); Query query1 = QueryImpl.create( "row[\"High\"]" ); Query query2 = QueryImpl.create( "row[\"Low\"]" ); Query query3 = QueryImpl.create( "row[\"Open\"]" ); Query query4 = QueryImpl.create( "row[\"Close\"]" ); ArrayList list = new ArrayList ( ); 394 list.add( query1 ); 395 list.add( query2 ); 396 list.add( query3 ); 397 list.add( query4 ); 398 ss.getDataDefinition( ).addAll( list ); 399 400 SeriesDefinition sdY = SeriesDefinitionImpl.create( ); 401 sdY.getSeriesPalette( ).update( 402 ColorDefinitionImpl.create( 168, 225, 253 ) ); 403 yAxisPrimary.getSeriesDefinitions( ).add( sdY ); 404 sdY.getSeries( ).add( ss ); 405 406 try 407 { 408 eih.getReportItem( ).setProperty( "chart.instance", cwaStock ); } 411 catch ( ExtendedElementException e ) 412 { 413 e.printStackTrace( ); 414 } 415 416 return eih; 417 } 418 419 private ExtendedItemHandle createVolumeChart( ) 420 { 421 ExtendedItemHandle eih = elementFactory.newExtendedItem( null, "Chart" ); 423 try 424 { 425 eih.setHeight( "175pt" ); eih.setWidth( "450pt" ); eih.setProperty( ExtendedItemHandle.DATA_SET_PROP, "Data Set" ); PropertyHandle computedSet = eih.getColumnBindings( ); 429 cs1.setExpression( "dataSetRow[\"Date\"]" ); computedSet.addItem( cs1 ); 431 cs2.setExpression( "dataSetRow[\"High\"]" ); computedSet.addItem( cs2 ); 433 cs3.setExpression( "dataSetRow[\"Close\"]" ); computedSet.addItem( cs3 ); 435 cs4.setExpression( "dataSetRow[\"Low\"]" ); computedSet.addItem( cs4 ); 437 cs5.setExpression( "dataSetRow[\"Open\"]" ); computedSet.addItem( cs5 ); 439 cs6.setExpression( "dataSetRow[\"Volume\"]" ); computedSet.addItem( cs6 ); 441 } 442 catch ( SemanticException e ) 443 { 444 e.printStackTrace( ); 445 } 446 447 ChartWithAxes cwaBar = ChartWithAxesImpl.create( ); 448 cwaBar.setType( "Bar Chart" ); cwaBar.setSubType( "Side-by-side" ); cwaBar.getTitle( ).setVisible( false ); 451 cwaBar.getLegend( ).setVisible( false ); 452 cwaBar.setOrientation( Orientation.VERTICAL_LITERAL ); 453 cwaBar.getBlock( ).setBounds( BoundsImpl.create( 0, 0, 450, 175 ) ); 454 cwaBar.getPlot( ).getClientArea( ).getOutline( ).setVisible( true ); 455 cwaBar.getPlot( ).getClientArea( ).setBackground( 456 ColorDefinitionImpl.create( 254, 251, 233 ) ); 457 458 Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes( )[0]; 459 xAxisPrimary.setCategoryAxis( true ); 460 xAxisPrimary.getTitle( ).getCaption( ).setValue( "Trading Date" ); xAxisPrimary.getMajorGrid( ).getLineAttributes( ).setStyle( 462 LineStyle.DOTTED_LITERAL ); 463 xAxisPrimary.getMajorGrid( ).getLineAttributes( ).setVisible( true ); 464 xAxisPrimary.getMajorGrid( ).getLineAttributes( ).setColor( 465 ColorDefinitionImpl.GREY( ) ); 466 xAxisPrimary.setType( AxisType.DATE_TIME_LITERAL ); 467 xAxisPrimary.setFormatSpecifier( JavaDateFormatSpecifierImpl 468 .create( "MM/dd/yyyy" ) ); 470 Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary ); 471 yAxisPrimary.getTitle( ).getCaption( ).setValue( "Volume" ); yAxisPrimary.getMajorGrid( ).getLineAttributes( ).setStyle( 473 LineStyle.DOTTED_LITERAL ); 474 yAxisPrimary.getMajorGrid( ).getLineAttributes( ).setVisible( true ); 475 yAxisPrimary.getMajorGrid( ).getLineAttributes( ).setColor( 476 ColorDefinitionImpl.GREY( ) ); 477 478 SampleData sd = DataFactory.eINSTANCE.createSampleData( ); 479 BaseSampleData sdBase = DataFactory.eINSTANCE.createBaseSampleData( ); 480 sdBase.setDataSetRepresentation( "01/25/2005" ); sd.getBaseSampleData( ).add( sdBase ); 482 483 OrthogonalSampleData sdOrthogonal = DataFactory.eINSTANCE 484 .createOrthogonalSampleData( ); 485 sdOrthogonal.setDataSetRepresentation( "5" ); sdOrthogonal.setSeriesDefinitionIndex( 0 ); 487 sd.getOrthogonalSampleData( ).add( sdOrthogonal ); 488 489 cwaBar.setSampleData( sd ); 490 491 Series seCategory = SeriesImpl.create( ); 492 Query query = QueryImpl.create( "row[\"Date\"]" ); seCategory.getDataDefinition( ).add( query ); 494 495 SeriesDefinition sdX = SeriesDefinitionImpl.create( ); 496 sdX.getSeries( ).add( seCategory ); 497 xAxisPrimary.getSeriesDefinitions( ).add( sdX ); 498 499 BarSeries bs = (BarSeries) BarSeriesImpl.create( ); 500 Query query2 = QueryImpl.create( "row[\"Volume\"]" ); bs.getDataDefinition( ).add( query2 ); 502 503 SeriesDefinition sdY = SeriesDefinitionImpl.create( ); 504 sdY.getSeriesPalette( ).update( 505 ColorDefinitionImpl.create( 168, 225, 253 ) ); 506 sdY.getSeries( ).add( bs ); 507 yAxisPrimary.getSeriesDefinitions( ).add( sdY ); 508 509 DataPointComponent dpc = DataPointComponentImpl.create( 510 DataPointComponentType.ORTHOGONAL_VALUE_LITERAL, 511 JavaNumberFormatSpecifierImpl.create( "###,###" ) ); bs.getDataPoint( ).getComponents( ).clear( ); 513 bs.getDataPoint( ).getComponents( ).add( dpc ); 514 bs.getLabel( ).setVisible( true ); 515 516 try 517 { 518 eih.getReportItem( ).setProperty( "chart.instance", cwaBar ); } 521 catch ( ExtendedElementException e ) 522 { 523 e.printStackTrace( ); 524 } 525 526 return eih; 527 } 528 } | Popular Tags |