1 9 10 package org.eclipse.birt.chart.examples.report.api; 11 12 import java.io.File ; 13 import java.io.IOException ; 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 [] args ) throws SemanticException, 63 IOException 64 { 65 new MeterChartExample( ).createReport( ); 66 } 67 68 void createReport( ) throws SemanticException, IOException 69 { 70 SessionHandle session = new DesignEngine( null ).newSessionHandle( (ULocale) null ); 72 73 reportDesignHandle = session.createDesign( ); 75 76 elementFactory = reportDesignHandle.getElementFactory( ); 78 79 dict = new DesignEngine( null ).getMetaData( ); 80 81 createMasterPages( ); 82 createDataSources( ); 83 createDataSets( ); 84 createBody( ); 85 86 String outputPath = "output"; File outputFolder = new File ( outputPath ); 88 if ( !outputFolder.exists( ) && !outputFolder.mkdir( ) ) 89 { 90 throw new IOException ( "Can not create the output folder" ); } 92 reportDesignHandle.saveAs( outputPath 93 + "/" + "MeterChartExample.rptdesign" ); } 95 96 private void createDataSources( ) throws SemanticException 97 { 98 ScriptDataSourceHandle dataSourceHandle = elementFactory.newScriptDataSource( "Data Source" ); reportDesignHandle.getDataSources( ).add( dataSourceHandle ); 100 } 101 102 private void createDataSets( ) throws SemanticException 103 { 104 ScriptDataSetHandle dataSetHandle = elementFactory.newScriptDataSet( "Data Set" ); dataSetHandle.setDataSource( "Data Source" ); 108 dataSetHandle.setOpen( "i=0;" + "sourcedata = new Array( new Array(3), new Array(3), new Array(3), new Array(3));" 112 + "sourcedata[0][0] = 10; " + "sourcedata[0][1] = \"Ice Bella\";" + "sourcedata[0][2] = 304;" 116 + "sourcedata[1][0] = 10; " + "sourcedata[1][1] = \"Nola Dicci\";" + "sourcedata[1][2] = 258;" 120 + "sourcedata[2][0] = 11; " + "sourcedata[2][1] = \"Ice Bella\";" + "sourcedata[2][2] = 202;" 124 + "sourcedata[3][0] = 11; " + "sourcedata[3][1] = \"Nola Dicci\";" + "sourcedata[3][2] = 181;" ); 128 dataSetHandle.setFetch( "if ( i < 4 ){" + "row[\"Month\"] = sourcedata[i][0];" + "row[\"Product\"] = sourcedata[i][1];" + "row[\"Amount\"] = sourcedata[i][2];" + "i++;" + "return true;}" + "else return false;" ); 136 cs1 = StructureFactory.createComputedColumn( ); 138 cs1.setName( "Month" ); cs1.setExpression( "row[\"Month\"]" ); cs1.setDataType( "integer" ); 142 cs2 = StructureFactory.createComputedColumn( ); 143 cs2.setName( "Product" ); cs2.setExpression( "row[\"Product\"]" ); cs2.setDataType( "string" ); 147 cs3 = StructureFactory.createComputedColumn( ); 148 cs3.setName( "Amount" ); cs3.setExpression( "row[\"Amount\"]" ); cs3.setDataType( "integer" ); 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" ); reportDesignHandle.getMasterPages( ).add( simpleMasterPage ); 164 } 165 166 private void createBody( ) throws SemanticException 167 { 168 ExtendedItemHandle eih = elementFactory.newExtendedItem( null, "Chart" ); 170 try 171 { 172 eih.setHeight( "288pt" ); eih.setWidth( "252pt" ); eih.setProperty( ExtendedItemHandle.DATA_SET_PROP, "Data Set" ); } 176 catch ( SemanticException e ) 177 { 178 e.printStackTrace( ); 179 } 180 181 PropertyHandle computedSet = eih.getColumnBindings( ); 182 cs1.setExpression( "dataSetRow[\"Month\"]" ); computedSet.addItem( cs1 ); 184 cs2.setExpression( "dataSetRow[\"Product\"]" ); computedSet.addItem( cs2 ); 186 cs3.setExpression( "dataSetRow[\"Amount\"]" ); computedSet.addItem( cs3 ); 188 189 reportDesignHandle.getBody( ).add( eih ); 190 191 try 192 { 193 eih.getReportItem( ).setProperty( "chart.instance", createMeterChart( ) ); } 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" ); dChart.setSubType( "Superimposed Meter Chart" ); 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" ); sd.getBaseSampleData( ).add( sdBase ); 215 216 OrthogonalSampleData sdOrthogonal = DataFactory.eINSTANCE.createOrthogonalSampleData( ); 217 sdOrthogonal.setDataSetRepresentation( "5, 4, 12" ); 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\"]" ); 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\"]" ); seDial.getDataDefinition( ).add( query2 ); 234 235 SeriesDefinition seGroup = SeriesDefinitionImpl.create( ); 236 Query query3 = QueryImpl.create( "row[\"Month\"]" ); 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 |