KickJava   Java API By Example, From Geeks To Geeks.

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


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
15 import org.eclipse.birt.chart.model.Chart;
16 import org.eclipse.birt.chart.model.DialChart;
17 import org.eclipse.birt.chart.model.attribute.LegendItemType;
18 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
19 import org.eclipse.birt.chart.model.component.Series;
20 import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
21 import org.eclipse.birt.chart.model.data.BaseSampleData;
22 import org.eclipse.birt.chart.model.data.DataFactory;
23 import org.eclipse.birt.chart.model.data.OrthogonalSampleData;
24 import org.eclipse.birt.chart.model.data.Query;
25 import org.eclipse.birt.chart.model.data.SampleData;
26 import org.eclipse.birt.chart.model.data.SeriesDefinition;
27 import org.eclipse.birt.chart.model.data.impl.QueryImpl;
28 import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
29 import org.eclipse.birt.chart.model.impl.DialChartImpl;
30 import org.eclipse.birt.chart.model.type.DialSeries;
31 import org.eclipse.birt.chart.model.type.impl.DialSeriesImpl;
32 import org.eclipse.birt.report.model.api.DesignElementHandle;
33 import org.eclipse.birt.report.model.api.DesignEngine;
34 import org.eclipse.birt.report.model.api.ElementFactory;
35 import org.eclipse.birt.report.model.api.ExtendedItemHandle;
36 import org.eclipse.birt.report.model.api.PropertyHandle;
37 import org.eclipse.birt.report.model.api.ReportDesignHandle;
38 import org.eclipse.birt.report.model.api.ScriptDataSetHandle;
39 import org.eclipse.birt.report.model.api.ScriptDataSourceHandle;
40 import org.eclipse.birt.report.model.api.SessionHandle;
41 import org.eclipse.birt.report.model.api.StructureFactory;
42 import org.eclipse.birt.report.model.api.activity.SemanticException;
43 import org.eclipse.birt.report.model.api.command.ContentException;
44 import org.eclipse.birt.report.model.api.command.NameException;
45 import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn;
46 import org.eclipse.birt.report.model.api.extension.ExtendedElementException;
47 import org.eclipse.birt.report.model.api.metadata.IMetaDataDictionary;
48
49 import com.ibm.icu.util.ULocale;
50
51 public class MeterChartExample
52 {
53
54     ReportDesignHandle reportDesignHandle = null;
55
56     ElementFactory elementFactory = null;
57
58     IMetaDataDictionary dict = null;
59
60     ComputedColumn cs1, cs2, cs3 = null;
61
62     public static void main( String JavaDoc[] args ) throws SemanticException,
63             IOException JavaDoc
64     {
65         new MeterChartExample( ).createReport( );
66     }
67
68     void createReport( ) throws SemanticException, IOException JavaDoc
69     {
70         // A session handle for all open reports
71
SessionHandle session = new DesignEngine( null ).newSessionHandle( (ULocale) null );
72
73         // Create a new report
74
reportDesignHandle = session.createDesign( );
75
76         // Element factory is used to create instances of BIRT elements.
77
elementFactory = reportDesignHandle.getElementFactory( );
78
79         dict = new DesignEngine( null ).getMetaData( );
80
81         createMasterPages( );
82         createDataSources( );
83         createDataSets( );
84         createBody( );
85
86         String JavaDoc outputPath = "output";//$NON-NLS-1$
87
File JavaDoc outputFolder = new File JavaDoc( outputPath );
88         if ( !outputFolder.exists( ) && !outputFolder.mkdir( ) )
89         {
90             throw new IOException JavaDoc( "Can not create the output folder" );//$NON-NLS-1$
91
}
92         reportDesignHandle.saveAs( outputPath
93                 + "/" + "MeterChartExample.rptdesign" );//$NON-NLS-1$//$NON-NLS-2$
94
}
95
96     private void createDataSources( ) throws SemanticException
97     {
98         ScriptDataSourceHandle dataSourceHandle = elementFactory.newScriptDataSource( "Data Source" );//$NON-NLS-1$
99
reportDesignHandle.getDataSources( ).add( dataSourceHandle );
100     }
101
102     private void createDataSets( ) throws SemanticException
103     {
104         // Data Set
105
ScriptDataSetHandle dataSetHandle = elementFactory.newScriptDataSet( "Data Set" );//$NON-NLS-1$
106
dataSetHandle.setDataSource( "Data Source" );//$NON-NLS-1$
107

108         // Set open( ) in code
109
dataSetHandle.setOpen( "i=0;"//$NON-NLS-1$
110
+ "sourcedata = new Array( new Array(3), new Array(3), new Array(3), new Array(3));"//$NON-NLS-1$
111

112                 + "sourcedata[0][0] = 10; "//$NON-NLS-1$
113
+ "sourcedata[0][1] = \"Ice Bella\";"//$NON-NLS-1$
114
+ "sourcedata[0][2] = 304;"//$NON-NLS-1$
115

116                 + "sourcedata[1][0] = 10; "//$NON-NLS-1$
117
+ "sourcedata[1][1] = \"Nola Dicci\";"//$NON-NLS-1$
118
+ "sourcedata[1][2] = 258;"//$NON-NLS-1$
119

120                 + "sourcedata[2][0] = 11; "//$NON-NLS-1$
121
+ "sourcedata[2][1] = \"Ice Bella\";"//$NON-NLS-1$
122
+ "sourcedata[2][2] = 202;"//$NON-NLS-1$
123

124                 + "sourcedata[3][0] = 11; "//$NON-NLS-1$
125
+ "sourcedata[3][1] = \"Nola Dicci\";"//$NON-NLS-1$
126
+ "sourcedata[3][2] = 181;" );//$NON-NLS-1$
127

128         // Set fetch( ) in code
129
dataSetHandle.setFetch( "if ( i < 4 ){"//$NON-NLS-1$
130
+ "row[\"Month\"] = sourcedata[i][0];"//$NON-NLS-1$
131
+ "row[\"Product\"] = sourcedata[i][1];"//$NON-NLS-1$
132
+ "row[\"Amount\"] = sourcedata[i][2];"//$NON-NLS-1$
133
+ "i++;"//$NON-NLS-1$
134
+ "return true;}" + "else return false;" );//$NON-NLS-1$//$NON-NLS-2$
135

136         // Set computed columns
137
cs1 = StructureFactory.createComputedColumn( );
138         cs1.setName( "Month" );//$NON-NLS-1$
139
cs1.setExpression( "row[\"Month\"]" );//$NON-NLS-1$
140
cs1.setDataType( "integer" );//$NON-NLS-1$
141

142         cs2 = StructureFactory.createComputedColumn( );
143         cs2.setName( "Product" );//$NON-NLS-1$
144
cs2.setExpression( "row[\"Product\"]" );//$NON-NLS-1$
145
cs2.setDataType( "string" );//$NON-NLS-1$
146

147         cs3 = StructureFactory.createComputedColumn( );
148         cs3.setName( "Amount" );//$NON-NLS-1$
149
cs3.setExpression( "row[\"Amount\"]" );//$NON-NLS-1$
150
cs3.setDataType( "integer" );//$NON-NLS-1$
151

152         PropertyHandle computedSet = dataSetHandle.getPropertyHandle( ScriptDataSetHandle.COMPUTED_COLUMNS_PROP );
153         computedSet.addItem( cs1 );
154         computedSet.addItem( cs2 );
155         computedSet.addItem( cs3 );
156
157         reportDesignHandle.getDataSets( ).add( dataSetHandle );
158     }
159
160     private void createMasterPages( ) throws ContentException, NameException
161     {
162         DesignElementHandle simpleMasterPage = elementFactory.newSimpleMasterPage( "Master Page" );//$NON-NLS-1$
163
reportDesignHandle.getMasterPages( ).add( simpleMasterPage );
164     }
165
166     private void createBody( ) throws SemanticException
167     {
168         ExtendedItemHandle eih = elementFactory.newExtendedItem( null, "Chart" );//$NON-NLS-1$
169

170         try
171         {
172             eih.setHeight( "288pt" );//$NON-NLS-1$
173
eih.setWidth( "252pt" );//$NON-NLS-1$
174
eih.setProperty( ExtendedItemHandle.DATA_SET_PROP, "Data Set" );//$NON-NLS-1$
175
}
176         catch ( SemanticException e )
177         {
178             e.printStackTrace( );
179         }
180
181         PropertyHandle computedSet = eih.getColumnBindings( );
182         cs1.setExpression( "dataSetRow[\"Month\"]" );//$NON-NLS-1$
183
computedSet.addItem( cs1 );
184         cs2.setExpression( "dataSetRow[\"Product\"]" );//$NON-NLS-1$
185
computedSet.addItem( cs2 );
186         cs3.setExpression( "dataSetRow[\"Amount\"]" );//$NON-NLS-1$
187
computedSet.addItem( cs3 );
188
189         reportDesignHandle.getBody( ).add( eih );
190
191         try
192         {
193             // Add chart instance to IReportItem
194
eih.getReportItem( ).setProperty( "chart.instance", createMeterChart( ) );//$NON-NLS-1$
195
}
196         catch ( ExtendedElementException e )
197         {
198             e.printStackTrace( );
199         }
200     }
201
202     private Chart createMeterChart( )
203     {
204         DialChart dChart = (DialChart) DialChartImpl.create( );
205         dChart.setDialSuperimposition( true );
206         dChart.setType( "Meter Chart" );//$NON-NLS-1$
207
dChart.setSubType( "Superimposed Meter Chart" );//$NON-NLS-1$
208
dChart.getBlock( ).setBounds( BoundsImpl.create( 0, 0, 252, 288 ) );
209         dChart.getLegend( ).setItemType( LegendItemType.SERIES_LITERAL );
210
211         SampleData sd = DataFactory.eINSTANCE.createSampleData( );
212         BaseSampleData sdBase = DataFactory.eINSTANCE.createBaseSampleData( );
213         sdBase.setDataSetRepresentation( "A, B, C" );//$NON-NLS-1$
214
sd.getBaseSampleData( ).add( sdBase );
215
216         OrthogonalSampleData sdOrthogonal = DataFactory.eINSTANCE.createOrthogonalSampleData( );
217         sdOrthogonal.setDataSetRepresentation( "5, 4, 12" );//$NON-NLS-1$
218
sdOrthogonal.setSeriesDefinitionIndex( 0 );
219         sd.getOrthogonalSampleData( ).add( sdOrthogonal );
220
221         dChart.setSampleData( sd );
222
223         Series seCategory = SeriesImpl.create( );
224         Query query1 = QueryImpl.create( "row[\"Product\"]" );//$NON-NLS-1$
225
seCategory.getDataDefinition( ).add( query1 );
226
227         SeriesDefinition series = SeriesDefinitionImpl.create( );
228         series.getSeries( ).add( seCategory );
229         dChart.getSeriesDefinitions( ).add( series );
230
231         DialSeries seDial = (DialSeries) DialSeriesImpl.create( );
232         Query query2 = QueryImpl.create( "row[\"Amount\"]" );//$NON-NLS-1$
233
seDial.getDataDefinition( ).add( query2 );
234
235         SeriesDefinition seGroup = SeriesDefinitionImpl.create( );
236         Query query3 = QueryImpl.create( "row[\"Month\"]" );//$NON-NLS-1$
237
seGroup.setQuery( query3 );
238         seGroup.getSeriesPalette( ).update( -2 );
239         series.getSeriesDefinitions( ).add( seGroup );
240         seGroup.getSeries( ).add( seDial );
241
242         return dChart;
243     }
244 }
245
Popular Tags