KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > birt > chart > examples > report > api > StockReport


1 /*******************************************************************************
2  * Copyright (c) 2004 Actuate Corporation. All rights reserved. This program and
3  * the accompanying materials are made available under the terms of the Eclipse
4  * Public License v1.0 which accompanies this distribution, and is available at
5  * http://www.eclipse.org/legal/epl-v10.html
6  *
7  * Contributors: Actuate Corporation - initial API and implementation
8  ******************************************************************************/

9
10 package org.eclipse.birt.chart.examples.report.api;
11
12 import java.io.File JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.util.ArrayList JavaDoc;
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 JavaDoc[] args ) throws SemanticException,
84             IOException JavaDoc
85     {
86         new StockReport( ).createReport( );
87     }
88
89     void createReport( ) throws SemanticException, IOException JavaDoc
90     {
91         // A session handle for all open reports
92
SessionHandle session = new DesignEngine( null ).newSessionHandle( (ULocale) null );
93
94         // Create a new report
95
reportDesignHandle = session.createDesign( );
96
97         // Element factory is used to create instances of BIRT elements.
98
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 JavaDoc outputPath = "output";//$NON-NLS-1$
110
File JavaDoc outputFolder = new File JavaDoc( outputPath );
111         if ( !outputFolder.exists( ) && !outputFolder.mkdir( ) )
112         {
113             throw new IOException JavaDoc( "Can not create the output folder" );//$NON-NLS-1$
114
}
115         reportDesignHandle
116                 .saveAs( outputPath + "/" + "StockAnalysis.rptdesign" );//$NON-NLS-1$//$NON-NLS-2$
117
}
118
119     private void createDataSources( ) throws SemanticException
120     {
121         ScriptDataSourceHandle dataSourceHandle = elementFactory
122                 .newScriptDataSource( "Data Source" );//$NON-NLS-1$
123
reportDesignHandle.getDataSources( ).add( dataSourceHandle );
124     }
125
126     private void createDataSets( ) throws SemanticException
127     {
128         // Data Set
129
ScriptDataSetHandle dataSetHandle = elementFactory
130                 .newScriptDataSet( "Data Set" );//$NON-NLS-1$
131
dataSetHandle.setDataSource( "Data Source" );//$NON-NLS-1$
132

133         // Set open( ) in code
134
dataSetHandle
135                 .setOpen( "i=0;"//$NON-NLS-1$
136
+ "sourcedata = new Array( new Array(6), new Array(6), new Array(6), new Array(6),"//$NON-NLS-1$
137
+ "new Array(6), new Array(6), new Array(6), new Array(6));"//$NON-NLS-1$
138
+ "sourcedata[0][0] = \"3/1/2005\"; "//$NON-NLS-1$
139
+ "sourcedata[0][1] = 2.77;"//$NON-NLS-1$
140
+ "sourcedata[0][2] = 2.73;"//$NON-NLS-1$
141
+ "sourcedata[0][3] = 2.69; "//$NON-NLS-1$
142
+ "sourcedata[0][4] = 2.71;"//$NON-NLS-1$
143
+ "sourcedata[0][5] = 341900;"//$NON-NLS-1$
144

145                         + "sourcedata[1][0] = \"3/2/2005\"; "//$NON-NLS-1$
146
+ "sourcedata[1][1] = 2.8;"//$NON-NLS-1$
147
+ "sourcedata[1][2] = 2.64;"//$NON-NLS-1$
148
+ "sourcedata[1][3] = 2.6; "//$NON-NLS-1$
149
+ "sourcedata[1][4] = 2.71;"//$NON-NLS-1$
150
+ "sourcedata[1][5] = 249900;"//$NON-NLS-1$
151

152                         + "sourcedata[2][0] = \"3/3/2005\"; "//$NON-NLS-1$
153
+ "sourcedata[2][1] = 2.6;"//$NON-NLS-1$
154
+ "sourcedata[2][2] = 2.28;"//$NON-NLS-1$
155
+ "sourcedata[2][3] = 2.13; "//$NON-NLS-1$
156
+ "sourcedata[2][4] = 2.59;"//$NON-NLS-1$
157
+ "sourcedata[2][5] = 394800;"//$NON-NLS-1$
158

159                         + "sourcedata[3][0] = \"3/4/2005\"; "//$NON-NLS-1$
160
+ "sourcedata[3][1] = 2.87;"//$NON-NLS-1$
161
+ "sourcedata[3][2] = 2.87;"//$NON-NLS-1$
162
+ "sourcedata[3][3] = 2.03; "//$NON-NLS-1$
163
+ "sourcedata[3][4] = 2.21;"//$NON-NLS-1$
164
+ "sourcedata[3][5] = 358200;"//$NON-NLS-1$
165

166                         + "sourcedata[4][0] = \"3/5/2005\"; "//$NON-NLS-1$
167
+ "sourcedata[4][1] = 2.48;"//$NON-NLS-1$
168
+ "sourcedata[4][2] = 2.26;"//$NON-NLS-1$
169
+ "sourcedata[4][3] = 2.16; "//$NON-NLS-1$
170
+ "sourcedata[4][4] = 2.39;"//$NON-NLS-1$
171
+ "sourcedata[4][5] = 339000;"//$NON-NLS-1$
172

173                         + "sourcedata[5][0] = \"3/6/2005\"; "//$NON-NLS-1$
174
+ "sourcedata[5][1] = 2.98;"//$NON-NLS-1$
175
+ "sourcedata[5][2] = 2.86;"//$NON-NLS-1$
176
+ "sourcedata[5][3] = 2.04; "//$NON-NLS-1$
177
+ "sourcedata[5][4] = 2.19;"//$NON-NLS-1$
178
+ "sourcedata[5][5] = 221000;"//$NON-NLS-1$
179

180                         + "sourcedata[6][0] = \"3/7/2005\"; "//$NON-NLS-1$
181
+ "sourcedata[6][1] = 2.87;"//$NON-NLS-1$
182
+ "sourcedata[6][2] = 2.17;"//$NON-NLS-1$
183
+ "sourcedata[6][3] = 2.14; "//$NON-NLS-1$
184
+ "sourcedata[6][4] = 2.62;"//$NON-NLS-1$
185
+ "sourcedata[6][5] = 183600;"//$NON-NLS-1$
186

187                         + "sourcedata[7][0] = \"3/8/2005\"; "//$NON-NLS-1$
188
+ "sourcedata[7][1] = 2.8;"//$NON-NLS-1$
189
+ "sourcedata[7][2] = 2.78;"//$NON-NLS-1$
190
+ "sourcedata[7][3] = 2.65; "//$NON-NLS-1$
191
+ "sourcedata[7][4] = 2.66;"//$NON-NLS-1$
192
+ "sourcedata[7][5] = 194800;" );//$NON-NLS-1$
193

194         // Set fetch( ) in code
195
dataSetHandle.setFetch( "if ( i < 8 ){"//$NON-NLS-1$
196
+ "row[\"Date\"] = sourcedata[i][0];"//$NON-NLS-1$
197
+ "row[\"High\"] = sourcedata[i][1];"//$NON-NLS-1$
198
+ "row[\"Close\"] = sourcedata[i][2];"//$NON-NLS-1$
199
+ "row[\"Low\"] = sourcedata[i][3];"//$NON-NLS-1$
200
+ "row[\"Open\"] = sourcedata[i][4];"//$NON-NLS-1$
201
+ "row[\"Volume\"] = sourcedata[i][5];" + "i++;"//$NON-NLS-1$//$NON-NLS-2$
202
+ "return true;}" + "else return false;" );//$NON-NLS-1$//$NON-NLS-2$
203

204         // Set computed columns
205
cs1 = StructureFactory.createComputedColumn( );
206         cs1.setName( "Date" );//$NON-NLS-1$
207
cs1.setExpression( "row[\"Date\"]" );//$NON-NLS-1$
208
cs1.setDataType( "date-time" );//$NON-NLS-1$
209

210         cs2 = StructureFactory.createComputedColumn( );
211         cs2.setName( "High" );//$NON-NLS-1$
212
cs2.setExpression( "row[\"High\"]" );//$NON-NLS-1$
213
cs2.setDataType( "float" );//$NON-NLS-1$
214

215         cs3 = StructureFactory.createComputedColumn( );
216         cs3.setName( "Close" );//$NON-NLS-1$
217
cs3.setExpression( "row[\"Close\"]" );//$NON-NLS-1$
218
cs3.setDataType( "float" );//$NON-NLS-1$
219

220         cs4 = StructureFactory.createComputedColumn( );
221         cs4.setName( "Low" );//$NON-NLS-1$
222
cs4.setExpression( "row[\"Low\"]" );//$NON-NLS-1$
223
cs4.setDataType( "float" );//$NON-NLS-1$
224

225         cs5 = StructureFactory.createComputedColumn( );
226         cs5.setName( "Open" );//$NON-NLS-1$
227
cs5.setExpression( "row[\"Open\"]" );//$NON-NLS-1$
228
cs5.setDataType( "float" );//$NON-NLS-1$
229

230         cs6 = StructureFactory.createComputedColumn( );
231         cs6.setName( "Volume" );//$NON-NLS-1$
232
cs6.setExpression( "row[\"Volume\"]" );//$NON-NLS-1$
233
cs6.setDataType( "integer" );//$NON-NLS-1$
234

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" );//$NON-NLS-1$
250
try
251         {
252             simpleMasterPage.setProperty( MasterPageHandle.LEFT_MARGIN_PROP,
253                     "0.2in" );//$NON-NLS-1$
254
simpleMasterPage.setProperty( MasterPageHandle.RIGHT_MARGIN_PROP,
255                     "0.2in" );//$NON-NLS-1$
256
}
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 );//$NON-NLS-1$
267
mainGrid.setWidth( "100%" );//$NON-NLS-1$
268
reportDesignHandle.getBody( ).add( mainGrid );
269
270         // First Grid Row
271
RowHandle row1 = (RowHandle) mainGrid.getRows( ).get( 0 );
272         row1.setProperty( StyleHandle.BACKGROUND_COLOR_PROP, "#FEFBE9" );//$NON-NLS-1$
273

274         // Cell (1st Row)
275
CellHandle row1Cell = (CellHandle) row1.getCells( ).get( 0 );
276
277         // Title label
278
LabelHandle label = elementFactory.newLabel( null );
279         label.setText( "Corporation Stock" );//$NON-NLS-1$
280
label.setProperty( StyleHandle.FONT_SIZE_PROP,
281                 DesignChoiceConstants.FONT_SIZE_X_LARGE );
282         label.setProperty( StyleHandle.FONT_FAMILY_PROP, "Arial Black" );//$NON-NLS-1$
283
label.setProperty( StyleHandle.COLOR_PROP, "#6E6E6E" );//$NON-NLS-1$
284
label.setProperty( StyleHandle.PADDING_BOTTOM_PROP, "0.5in" );//$NON-NLS-1$
285
label.setProperty( StyleHandle.TEXT_ALIGN_PROP,
286                 DesignChoiceConstants.TEXT_ALIGN_CENTER );
287
288         row1Cell.getContent( ).add( label );
289
290         // Second Grid Row
291
RowHandle row2 = (RowHandle) mainGrid.getRows( ).get( 1 );
292
293         CellHandle row2Cell1 = (CellHandle) row2.getCells( ).get( 0 );
294         row2Cell1.getContent( ).add( createStockChart( ) );
295
296         // Third Grid Row
297
RowHandle row3 = (RowHandle) mainGrid.getRows( ).get( 2 );
298
299         // 1st Cell (3nd Row)
300
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" );//$NON-NLS-1$
307

308         try
309         {
310             eih.setHeight( "175pt" );//$NON-NLS-1$
311
eih.setWidth( "450pt" );//$NON-NLS-1$
312
eih.setProperty( ExtendedItemHandle.DATA_SET_PROP, "Data Set" );//$NON-NLS-1$
313
PropertyHandle computedSet = eih.getColumnBindings( );
314             cs1.setExpression( "dataSetRow[\"Date\"]" );//$NON-NLS-1$
315
computedSet.addItem( cs1 );
316             cs2.setExpression( "dataSetRow[\"High\"]" );//$NON-NLS-1$
317
computedSet.addItem( cs2 );
318             cs3.setExpression( "dataSetRow[\"Close\"]" );//$NON-NLS-1$
319
computedSet.addItem( cs3 );
320             cs4.setExpression( "dataSetRow[\"Low\"]" );//$NON-NLS-1$
321
computedSet.addItem( cs4 );
322             cs5.setExpression( "dataSetRow[\"Open\"]" );//$NON-NLS-1$
323
computedSet.addItem( cs5 );
324             cs6.setExpression( "dataSetRow[\"Volume\"]" );//$NON-NLS-1$
325
computedSet.addItem( cs6 );
326         }
327         catch ( SemanticException e )
328         {
329             e.printStackTrace( );
330         }
331
332         ChartWithAxes cwaStock = ChartWithAxesImpl.create( );
333         cwaStock.setType( "Stock Chart" );//$NON-NLS-1$
334
cwaStock.setSubType( "Standard Stock Chart" );//$NON-NLS-1$
335
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" );//$NON-NLS-1$
346
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" ) );//$NON-NLS-1$
354

355         Axis yAxisPrimary = cwaStock.getPrimaryOrthogonalAxis( xAxisPrimary );
356         yAxisPrimary.getTitle( ).getCaption( ).setValue( "Price" );//$NON-NLS-1$
357
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" );//$NON-NLS-1$
369
sd.getBaseSampleData( ).add( sdBase );
370
371         OrthogonalSampleData sdOrthogonal = DataFactory.eINSTANCE
372                 .createOrthogonalSampleData( );
373         sdOrthogonal.setDataSetRepresentation( "H5.3 L1.3 O4.5 C3.4" );//$NON-NLS-1$
374
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\"]" );//$NON-NLS-1$
381
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" );//$NON-NLS-1$
389
Query query1 = QueryImpl.create( "row[\"High\"]" );//$NON-NLS-1$
390
Query query2 = QueryImpl.create( "row[\"Low\"]" );//$NON-NLS-1$
391
Query query3 = QueryImpl.create( "row[\"Open\"]" );//$NON-NLS-1$
392
Query query4 = QueryImpl.create( "row[\"Close\"]" );//$NON-NLS-1$
393
ArrayList JavaDoc list = new ArrayList JavaDoc( );
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             // Add chart instance to IReportItem
409
eih.getReportItem( ).setProperty( "chart.instance", cwaStock );//$NON-NLS-1$
410
}
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" );//$NON-NLS-1$
422

423         try
424         {
425             eih.setHeight( "175pt" );//$NON-NLS-1$
426
eih.setWidth( "450pt" );//$NON-NLS-1$
427
eih.setProperty( ExtendedItemHandle.DATA_SET_PROP, "Data Set" );//$NON-NLS-1$
428
PropertyHandle computedSet = eih.getColumnBindings( );
429             cs1.setExpression( "dataSetRow[\"Date\"]" );//$NON-NLS-1$
430
computedSet.addItem( cs1 );
431             cs2.setExpression( "dataSetRow[\"High\"]" );//$NON-NLS-1$
432
computedSet.addItem( cs2 );
433             cs3.setExpression( "dataSetRow[\"Close\"]" );//$NON-NLS-1$
434
computedSet.addItem( cs3 );
435             cs4.setExpression( "dataSetRow[\"Low\"]" );//$NON-NLS-1$
436
computedSet.addItem( cs4 );
437             cs5.setExpression( "dataSetRow[\"Open\"]" );//$NON-NLS-1$
438
computedSet.addItem( cs5 );
439             cs6.setExpression( "dataSetRow[\"Volume\"]" );//$NON-NLS-1$
440
computedSet.addItem( cs6 );
441         }
442         catch ( SemanticException e )
443         {
444             e.printStackTrace( );
445         }
446
447         ChartWithAxes cwaBar = ChartWithAxesImpl.create( );
448         cwaBar.setType( "Bar Chart" );//$NON-NLS-1$
449
cwaBar.setSubType( "Side-by-side" );//$NON-NLS-1$
450
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" );//$NON-NLS-1$
461
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" ) );//$NON-NLS-1$
469

470         Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary );
471         yAxisPrimary.getTitle( ).getCaption( ).setValue( "Volume" );//$NON-NLS-1$
472
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" );//$NON-NLS-1$
481
sd.getBaseSampleData( ).add( sdBase );
482
483         OrthogonalSampleData sdOrthogonal = DataFactory.eINSTANCE
484                 .createOrthogonalSampleData( );
485         sdOrthogonal.setDataSetRepresentation( "5" );//$NON-NLS-1$
486
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\"]" );//$NON-NLS-1$
493
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\"]" );//$NON-NLS-1$
501
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( "###,###" ) );//$NON-NLS-1$
512
bs.getDataPoint( ).getComponents( ).clear( );
513         bs.getDataPoint( ).getComponents( ).add( dpc );
514         bs.getLabel( ).setVisible( true );
515
516         try
517         {
518             // Add chart instance to IReportItem
519
eih.getReportItem( ).setProperty( "chart.instance", cwaBar );//$NON-NLS-1$
520
}
521         catch ( ExtendedElementException e )
522         {
523             e.printStackTrace( );
524         }
525
526         return eih;
527     }
528 }
Popular Tags