1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
| import java.io.IOException;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.logging.Level;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.chart.model.ChartWithAxes;
import org.eclipse.birt.chart.model.Chart;
import org.eclipse.birt.chart.model.attribute.Anchor;
import org.eclipse.birt.chart.model.attribute.AxisType;
import org.eclipse.birt.chart.model.attribute.IntersectionType ;
import org.eclipse.birt.chart.model.attribute.Position;
import org.eclipse.birt.chart.model.attribute.TickStyle;
import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
import org.eclipse.birt.chart.model.attribute.impl.GradientImpl ;
import org.eclipse.birt.chart.model.component.Axis;
import org.eclipse.birt.chart.model.component.Series;
import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
import org.eclipse.birt.chart.model.data.Query ;
import org.eclipse.birt.chart.model.data.SeriesDefinition;
import org.eclipse.birt.chart.model.data.impl.QueryImpl;
import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl ;
import org.eclipse.birt.chart.model.layout.Legend;
import org.eclipse.birt.chart.model.layout.Plot;
import org.eclipse.birt.chart.model.type.BarSeries;
import org.eclipse.birt.chart.model.type.impl.BarSeriesImpl ;
import org.eclipse.birt.chart.reportitem.ChartReportItemImpl;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.GridHandle;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
import org.eclipse.birt.report.model.api.ImageHandle;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.OdaDataSetHandle;
import org.eclipse.birt.report.model.api.OdaDataSourceHandle;
import org.eclipse.birt.report.model.api.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.TableHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn;
import org.eclipse.birt.report.model.api.ExtendedItemHandle;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.elements.interfaces.IStyleModel;
import org.eclipse.birt.report.model.api.extension.ExtendedElementException;
import org.eclipse.birt.report.model.metadata.MetaDataDictionary;
import org.eclipse.birt.chart.model.data.TextDataSet;
import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
import org.eclipse.birt.chart.model.data.impl.TextDataSetImpl;
import org.eclipse.birt.chart.model.data.NumberDataSet;
import com.ibm.icu.util.ULocale;
/**
* Dynamic Table BIRT Design Engine API (DEAPI) demo.
*/
public class BarChart
{
ReportDesignHandle designHandle = null;
ElementFactory designFactory = null;
StructureFactory structFactory = null;
//String[] cols = {"PROD","SUMA"};
String[] cols = {"CITY", "CUSTOMERNUMBER"};
OdaDataSetHandle dsHandle = null;
MetaDataDictionary dict;
public static void main( String[] args )
{
try
{
BarChart bc = new BarChart();
bc.buildReport( );
}
catch ( IOException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch ( SemanticException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void buildDataSource( ) throws SemanticException
{
OdaDataSourceHandle dsHandle = designFactory.newOdaDataSource(
"ChartDataSource", "org.eclipse.birt.report.data.oda.jdbc" );
dsHandle.setProperty( "odaDriverClass",
"org.eclipse.birt.report.data.oda.sampledb.Driver" );
dsHandle.setProperty( "odaURL", "jdbc:classicmodels:sampledb" );
dsHandle.setProperty( "odaUser", "ClassicModels" );
dsHandle.setProperty( "odaPassword", "" );
designHandle.getDataSources( ).add( dsHandle );
}
void buildDataSet( ) throws SemanticException
{
dsHandle = designFactory.newOdaDataSet( "ChartData",
"org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" );
dsHandle.setDataSource( "ChartDataSource" );
//String qry = "SELECT Products.ProductLine AS PROD, sum(OrderDetails.QuantityOrdered) AS SUMA FROM OrderDetails, Products WHERE Products.ProductCode=OrderDetails.ProductCode GROUP BY Products.ProductLine ORDER BY Products.ProductLine";
String qry = "SELECT CITY, CUSTOMERNUMBER FROM CUSTOMERS WHERE CUSTOMERNUMBER < 125";
dsHandle.setQueryText( qry );
designHandle.getDataSets( ).add( dsHandle );
}
void buildReport( ) throws IOException, SemanticException
{
//Configure the Engine and start the Platform
DesignConfig config = new DesignConfig( );
//config.setProperty("BIRT_HOME", "C:/Program Files/birtruntime/birt-runtime-2_1_2/ReportEngine");
config.setProperty("BIRT_HOME", "C:/Archivos de programa/birt-runtime-2_1_2/ReportEngine");
IDesignEngine engine = null;
try{
Platform.startup( config );
IDesignEngineFactory factory = (IDesignEngineFactory) Platform.createFactoryObject( IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
engine = factory.createDesignEngine( config );
}catch( Exception ex){
ex.printStackTrace();
}
SessionHandle session = engine.newSessionHandle( ULocale.ENGLISH ) ;
try{
//open a design or a template
//designHandle = session.openDesign("D:/stage/Report.rptdesign");
designHandle = session.createDesign();
designFactory = designHandle.getElementFactory( );
DesignElementHandle simpleMasterPage = designFactory.newSimpleMasterPage( "Master Page" );//$NON-NLS-1$
designHandle.getMasterPages( ).add( simpleMasterPage );
buildDataSource();
buildDataSet();
createBody();
createTable();
// Save the design and close it.
designHandle.saveAs( "E:/stage/MyChart.rptdesign" ); //$NON-NLS-1$
designHandle.close( );
Platform.shutdown();
System.out.println("Finished");
}catch (Exception e){
e.printStackTrace();
}
}
private void createBody() throws SemanticException{
//TableHandle table = designFactory.newTableItem(null,1,1,1,1);
//table.setWidth("100%");
//table.setDataSet(designHandle.findDataSet("ChartData"));
//designHandle.getBody().add(table);
//designHandle.getBody().add(createMultiYSeriesChart());
//RowHandle rowHandle = (RowHandle) table.getDetail( ).get( 0 );
//RowHandle rowHandle = (RowHandle)table.getDetail().get(0);
//CellHandle cellHandle = (CellHandle)rowHandle.getCells().get(0);
//cellHandle.getContent().add(createMultiYSeriesChart( ));
designHandle.getBody().add(createMultiYSeriesChart());
}
protected ExtendedItemHandle /*Chart*/ createMultiYSeriesChart( )
{
ExtendedItemHandle eih = designFactory.newExtendedItem ( null, "Chart" );//$NON-NLS-1$
try
{
eih.setHeight( "7.51in" );//$NON-NLS-1$
eih.setWidth( "4.447in" );//$NON-NLS-1$
//eih.setDataSet(dsHandle);
eih.setProperty ( ExtendedItemHandle.DATA_SET_PROP, "ChartData" );//$NON-NLS-1$
}
catch ( SemanticException e )
{
e.printStackTrace( );
}
ChartWithAxes cwaBar = ChartWithAxesImpl.create( );
cwaBar.setType("Bar Chart");
cwaBar.setSubType("Side-by-side");
// Plot
cwaBar.getBlock( ).setBackground( ColorDefinitionImpl.WHITE( ) );
Plot p = cwaBar.getPlot( );
p.getClientArea( )
.setBackground( GradientImpl.create( ColorDefinitionImpl.create( 225,
225,
255 ),
ColorDefinitionImpl.create( 255, 255, 225 ),
-35,
false ) );
p.getOutline( ).setVisible( true );
// Title
cwaBar.getTitle( )
.getLabel( )
.getCaption( )
.setValue( "Bar Chart with Multiple Y Series" );//$NON-NLS-1$
// Legend
Legend lg = cwaBar.getLegend( );
lg.getText( ).getFont( ).setSize( 16 );
lg.getText( ).getFont( ).setBold( true );
lg.getInsets( ).set( 10, 5, 0, 0 );
lg.setAnchor( Anchor.NORTH_LITERAL );
lg.setVisible(true);
// X-Axis
Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes( )[0];
xAxisPrimary.setType( AxisType.TEXT_LITERAL );
xAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.BELOW_LITERAL );
xAxisPrimary.getOrigin( ).setType( IntersectionType.VALUE_LITERAL );
xAxisPrimary.getTitle( ).getCaption( ).setValue( "Eje X" ); //$NON-NLS-1$
xAxisPrimary.setLabelPosition ( Position.BELOW_LITERAL );
xAxisPrimary.setTitlePosition( Position.BELOW_LITERAL );
// Y-Axis
Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary );
yAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.LEFT_LITERAL );
yAxisPrimary.getTitle( )
.getCaption( )
.setValue( "Eje Y" );//$NON-NLS-1$
// Data Set
// TextDataSet categoryValues = TextDataSetImpl.create( new String[]{
// "Europe", "Asia", "North America"} );//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
// NumberDataSet orthoValues1 = NumberDataSetImpl.create( new double[]{
// 26.17, 34.21, 21.5
// } );
// NumberDataSet orthoValues2 = NumberDataSetImpl.create( new double[]{
// 4.81, 3.55, -5.26
// } );
// X-Series
Series seCategory = SeriesImpl.create( );
Query query = QueryImpl.create( "row[\"" + (String)cols[0] + "\"]" );//$NON-NLS-1$
seCategory.getDataDefinition( ).add( query );
//seCategory.setDataSet( categoryValues );
SeriesDefinition sdX = SeriesDefinitionImpl.create( );
xAxisPrimary.getSeriesDefinitions( ).add( sdX );
sdX.getSeries( ).add( seCategory );
// Y-Series (1)
BarSeries bs = (BarSeries) BarSeriesImpl.create( );
bs.setSeriesIdentifier( "Sales" );//$NON-NLS-1$
Query query1 = QueryImpl.create( "row[\"" + (String)cols[1] + "\"]" );//$NON-NLS-1$
bs.getDataDefinition( ).add( query1 );
/*SeriesDefinition seGroup = SeriesDefinitionImpl.create( );
Query query3 = QueryImpl.create( "row[\"" + (String)cols[1] + "\"]" );//$NON-NLS-1$
seGroup.setQuery( query3 );
*/
//bs.setDataSet( orthoValues1 );
bs.setRiserOutline( null );
bs.getLabel( ).setVisible( true );
bs.setLabelPosition( Position.INSIDE_LITERAL );
SeriesDefinition sdY1 = SeriesDefinitionImpl.create( );
sdY1.getSeriesPalette( ).update( -2 );
yAxisPrimary.getSeriesDefinitions( ).add( sdY1 );
sdY1.getSeries( ).add( bs );
// Y-Series (2)
// BarSeries bs2 = (BarSeries) BarSeriesImpl.create( );
// bs2.setSeriesIdentifier( "Net Profit" );//$NON-NLS-1$
// Query query2 = QueryImpl.create( "row[\"customerNumber\"]" );//$NON-NLS-1$
// bs2.getDataDefinition( ).add( query2 );
// bs2.setRiserOutline( null );
// bs2.getLabel( ).setVisible( true );
// bs2.setLabelPosition( Position.INSIDE_LITERAL );
// SeriesDefinition sdY2 = SeriesDefinitionImpl.create( );
// sdY2.getSeriesPalette( ).update( -3 );
// yAxisPrimary.getSeriesDefinitions( ).add( sdY2 );
// sdY2.getSeries( ).add( bs2 );
ChartReportItemImpl crii = null;
try
{
//Add ChartReportItemImpl to ExtendedItemHandle
crii = (ChartReportItemImpl) eih.getReportItem( );
//Add chart instance to ChartReportItemImpl
crii.setProperty( "chart.instance", cwaBar );//$NON-NLS-1$
}
catch ( ExtendedElementException e )
{
e.printStackTrace( );
}
return eih;
}
void createTable(){
try{
TableHandle table = designFactory.newTableItem( "table", cols.length);
table.setWidth( "100%" );
table.setDataSet( designHandle.findDataSet( "ChartData" ) );
PropertyHandle computedSet = table.getColumnBindings( );
ComputedColumn cs1 = null;
for( int i=0; i < cols.length; i++){
cs1 = StructureFactory.createComputedColumn();
cs1.setName((String)cols[i]);
cs1.setExpression("dataSetRow[\"" + (String)cols[i] + "\"]");
computedSet.addItem(cs1);
}
// table header
RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 );
tableheader.setProperty( StyleHandle.BACKGROUND_COLOR_PROP, "Gray" );
tableheader.setProperty(IStyleModel.TEXT_ALIGN_PROP,DesignChoiceConstants.BACKGROUND_POSITION_CENTER);
for( int i=0; i < cols.length; i++){
LabelHandle label1 = designFactory.newLabel( (String)cols[i] );
label1.setText((String)cols[i]);
CellHandle cell = (CellHandle) tableheader.getCells( ).get( i );
cell.getContent( ).add( label1 );
}
// table detail
RowHandle tabledetail = (RowHandle) table.getDetail( ).get( 0 );
for( int i=0; i < cols.length; i++){
CellHandle cell = (CellHandle) tabledetail.getCells( ).get( i );
DataItemHandle data = designFactory.newDataItem( "data_"+(String)cols[i] );
data.setResultSetColumn( (String)cols[i]);
cell.getContent( ).add( data );
}
designHandle.getBody( ).add( table );
}catch(Exception e){
System.out.println("Exception at createTable: " + e);
}
}
}//Fin class |
Partager