|
|
|
Re: create a report in code [message #518314 is a reply to message #518211] |
Wed, 03 March 2010 17:52 |
|
I did a webinar on the DE-API
http://www.birt-exchange.com/be/news-events/resources/?artic leid=17456
Take a look at these examples from the webinar:
http://www.birt-exchange.org/devshare/designing-birt-reports /409-deapi-examples-from-birt-exchange-webinar/#description
I also did one on the chart api:
http://www.birt-exchange.com/be/info/chartingapi-websem/
You may want to look over the list:
http://www.birt-exchange.com/be/news-events/webinars/archive d-webinars/
There are also plenty of examples in the dev share at birt-exchange
Attached is a simple example that creates a chart:
Jason
import java.io.IOException;
import java.util.Iterator;
import org.eclipse.birt.chart.model.Chart;
import org.eclipse.birt.chart.model.ChartWithAxes;
import org.eclipse.birt.chart.model.attribute.AxisType;
import org.eclipse.birt.chart.model.attribute.DataType;
import org.eclipse.birt.chart.model.attribute.GroupingUnitType;
import org.eclipse.birt.chart.model.attribute.IntersectionType;
import org.eclipse.birt.chart.model.attribute.SortOption;
import org.eclipse.birt.chart.model.attribute.TickStyle;
import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionI mpl;
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.SeriesGrouping;
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.Plot;
import org.eclipse.birt.chart.model.type.LineSeries;
import org.eclipse.birt.chart.model.type.impl.LineSeriesImpl;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.core.script.ScriptExpression;
import org.eclipse.birt.data.engine.api.querydefn.ComputedColumn;
import org.eclipse.birt.report.model.api.ComputedColumnHandle;
import org.eclipse.birt.report.model.api.DataSetHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignFileException;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.ExtendedItemHandle;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
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.ScriptDataSetHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.SimpleMasterPageHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.activity.SemanticException ;
import org.eclipse.birt.report.model.api.command.ContentException;
import org.eclipse.birt.report.model.api.command.NameException;
import
org.eclipse.birt.report.model.api.elements.structures.Proper tyBinding;
import com.ibm.icu.util.ULocale;
public class SimpleChart
{
private ReportDesignHandle reportDesignHandle = null;
private ElementFactory elementFactory = null;
private OdaDataSourceHandle odaDataSourceHandle = null;
private String dataSourceName = "datasource";
private String dataSetName = "maindataset";
private SessionHandle sessionHandle =null;
private String query = "SELECT SIMS.ROLES.\"CREATOR\",
SIMS.ROLES.\"RID\" FROM SIMS.ROLES";
org.eclipse.birt.report.model.api.elements.structures.Comput edColumn
cs1, cs2 = null;
public static void main(String args[])
{
try {
new SimpleChart().createReport();
} catch (Exception e) {
e.printStackTrace();
}
}
public void createReport() throws SemanticException, IOException
{
System.out.println("Start");
init();
createMasterPages();
//createDataSources();
//createDataSets();
buildDataSource();
buildDataSet();
createBody();
String outputPath = "C:/work";
reportDesignHandle.saveAs("output/desample/simplechart.rptdesign ");
try
{
reportDesignHandle = sessionHandle.openDesign(
"output/desample/simplechart.rptdesign" );//$NON-NLS-1$
}
catch ( DesignFileException e )
{
// TODO Auto-generated catch block
e.printStackTrace( );
}
ExtendedItemHandle eih = (ExtendedItemHandle)
reportDesignHandle.getBody( )
.getContents( ).get( 0 );
for ( Iterator iter = eih.getColumnBindings( )
.iterator( ); iter.hasNext( ); )
{
ComputedColumnHandle cch = (ComputedColumnHandle) iter.next( );
//if( cch.getExpression().contains("_Sum") ){
//cch.drop();
//}else{
System.out.println("opened report "+cch.getExpression());
//}
}
//reportDesignHandle.save();
System.out.println("End");
}
private void init(){
DesignConfig config = new DesignConfig( );
config.setBIRTHome("C:/birt/birt-runtime-2_5_0/birt-runtime-2_5_0/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();
}
// we need a handle of session of design engine
sessionHandle = engine.newSessionHandle(ULocale.ENGLISH);
reportDesignHandle = sessionHandle.createDesign();
elementFactory = reportDesignHandle.getElementFactory();
}
private void createMasterPages() throws ContentException, NameException
{
SimpleMasterPageHandle simpleMasterPage =
elementFactory.newSimpleMasterPage("Master Page");
reportDesignHandle.getMasterPages().add(simpleMasterPage);
}
void buildDataSource( ) throws SemanticException
{
OdaDataSourceHandle dsHandle = elementFactory.newOdaDataSource(
"Data Source", "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", "" );
PropertyBinding pb = new PropertyBinding();
reportDesignHandle.getDataSources( ).add( dsHandle );
}
void buildDataSet( ) throws SemanticException
{
OdaDataSetHandle dsHandle = elementFactory.newOdaDataSet( dataSetName,
"org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" );
dsHandle.setDataSource( "Data Source" );
String qry = "Select PRODUCTCODE, QUANTITYORDERED from orderdetails
where ordernumber = 10104";
dsHandle.setQueryText( qry );
reportDesignHandle.getDataSets( ).add( dsHandle );
}
private void createDataSets() throws SemanticException
{
String dataSetType =
"org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet";
OdaDataSetHandle dataSetHandle = null;
dataSetHandle = elementFactory.newOdaDataSet(dataSetName,
dataSetType);
dataSetHandle.setDataSource(dataSourceName);
dataSetHandle.setQueryText(query);
reportDesignHandle.getDataSets().add(dataSetHandle);
}
private void createDataSources() throws SemanticException
{
// type of the data source
String extensionID = "org.eclipse.birt.report.data.oda.jdbc";
String driverClassName = "oracle.jdbc.driver.OracleDriver";
String driverURL = "jdbc:oracle:thin:@192.168.174.25:1521:BDEV";
String userName = "mferdous";
String userPassword = "anabase";
// now make a data source with the dataSourceName and extensionID.
odaDataSourceHandle =
elementFactory.newOdaDataSource(dataSourceName,
extensionID);
odaDataSourceHandle.setProperty("odaDriverClass", driverClassName);
odaDataSourceHandle.setProperty("odaURL", driverURL);
odaDataSourceHandle.setProperty("odaUser", userName);
odaDataSourceHandle.setProperty("odaPassword", userPassword);
reportDesignHandle.getDataSources().add(odaDataSourceHandle) ;
}
private void createBody() throws SemanticException
{
ExtendedItemHandle extendedItemHandle =
elementFactory.newExtendedItem("Simple Chart", "Chart");
extendedItemHandle.setWidth("700px");
extendedItemHandle.setHeight("500px");
extendedItemHandle.setProperty(ExtendedItemHandle.DATA_SET_P ROP,
dataSetName);
extendedItemHandle.setProperty("outputFormat","GIF");
Chart c = createChart();
extendedItemHandle.getReportItem().setProperty(
"chart.instance", c );
reportDesignHandle.getBody().add(extendedItemHandle);
//PropertyHandle computedSet = extendedItemHandle.getColumnBindings( );
//computedSet.clearValue();
cs1 = StructureFactory.createComputedColumn( );
cs1.setName( "PRODUCTCODE" );
cs1.setExpression( "dataSetRow[\"PRODUCTCODE\"]");
cs1.setDataType( "string" );
cs1.setAggregateOn(null);
cs2 = StructureFactory.createComputedColumn( );
cs2.setName( "QUANTITYORDERED" );
cs2.setExpression( "dataSetRow[\"QUANTITYORDERED\"]");
cs2.setDataType( "integer" );
extendedItemHandle.addColumnBinding(cs1, true);
extendedItemHandle.addColumnBinding(cs2, true);
}
private Chart createChart() {
ChartWithAxes cwaLine = ChartWithAxesImpl.create();
cwaLine.setType( "Line Chart" ); //$NON-NLS-1$
cwaLine.setSubType( "Overlay" ); //$NON-NLS-1$
cwaLine.getBlock().getBounds().setWidth(600);
cwaLine.getBlock().getBounds().setHeight(400);
// Plot
cwaLine.getBlock().setBackground( ColorDefinitionImpl.WHITE() );
Plot p = cwaLine.getPlot();
p.getClientArea().setBackground( ColorDefinitionImpl.create(
255, 255,
225 ) );
// Title
cwaLine.getTitle().getLabel().getCaption().setValue("Overlay
test Line Chart" );
cwaLine.getTitle().setVisible(true);
// Legend
cwaLine.getLegend().setVisible( true );
// X-Axis
Axis xAxisPrimary = cwaLine.getPrimaryBaseAxes()[0];
xAxisPrimary.setType( AxisType.TEXT_LITERAL );
//xAxisPrimary.getMajorGrid().setTickStyle(
TickStyle.BELOW_LITERAL );
//xAxisPrimary.getOrigin().setType( IntersectionType.MIN_LITERAL );
// Y-Axis
Axis yAxisPrimary = cwaLine.getPrimaryOrthogonalAxis(
xAxisPrimary );
yAxisPrimary.setType(AxisType.LINEAR_LITERAL);
//yAxisPrimary.getMajorGrid().setTickStyle(
TickStyle.RIGHT_LITERAL );
//yAxisPrimary.getLabel().getCaption().setValue("TEST");
yAxisPrimary.getLabel().setVisible(true);
// X-Series
Series seCategory = SeriesImpl.create( );
// seCategory.setDataSet( categoryValues );
// Set category expression.
seCategory.getDataDefinition( )
.add( QueryImpl.create( "row[\"PRODUCTCODE\"]" ) );
SeriesDefinition sdX = SeriesDefinitionImpl.create( );
sdX.getSeriesPalette( ).shift( 0 );
sdX.setSorting(SortOption.ASCENDING_LITERAL);
// Set default grouping.
SeriesGrouping grouping = sdX.getGrouping( );
grouping.getAggregateExpression();
grouping.setEnabled( true );
grouping.setGroupType( DataType.TEXT_LITERAL );
grouping.setGroupingUnit( GroupingUnitType.STRING_PREFIX_LITERAL );
grouping.setGroupingInterval( 1 );
grouping.setAggregateExpression( "Sum" ); // Set Count aggregation.
//$NON-NLS-1$
xAxisPrimary.getSeriesDefinitions( ).add( sdX );
sdX.getSeries( ).add( seCategory );
// Y-Series
LineSeries bs1 = (LineSeries) LineSeriesImpl.create( );
bs1.getDataDefinition( ).add( QueryImpl.create(
"row[\"QUANTITYORDERED\"]" ) );
bs1.getLabel( ).setVisible( true );
SeriesDefinition sdY = SeriesDefinitionImpl.create( );
sdY.getGrouping().setEnabled(false);
yAxisPrimary.getSeriesDefinitions( ).add( sdY );
sdY.getSeries( ).add( bs1 );
return cwaLine;
}
}
Carl Emmoth wrote:
> Robert Jenkin wrote on Tue, 02 March 2010 15:44
>> I like to know if it is possible (any sample helpful) to create a
>> report in java code, ie without a pre-existing report file.
>
>
> Yes it would be very good to see an example of a table with a chart for
> instance. There is simply no good documentation of the Chart API.
>
> Just a "Hello World"-chart that works would be great!
>
> Here is an example that is pretty good:
> http://wiki.eclipse.org/Java_-_Build_Dynamic_Table_%28BIRT%2 9
>
> And I'm trying right now to connect it to a chart but with no success.
> If there are any help, please advice!
>
>
|
|
|
|
Re: create a report in code [message #518639 is a reply to message #518599] |
Thu, 04 March 2010 11:50 |
|
Carl,
If would be good if you could add your example to Birt-Exchange.com dev
share.
Jason
Carl Emmoth wrote:
> All right I finally got it to work!
>
> http://pastebin.com/VUvyDsgW
>
> It took a long time to see the problems, but this is what the
> application does:
>
> 1) Fully configurable paths and database selection
> 2) Make charts (pie, line and 3d bars)
> 3) Save it to design, report and html
> 4) View the html in a JEditorPane
>
> The application will use the test database to retrieve data with some
> restrictions. Only the two first columns from the sql-selection will be
> used, and the first one must be in string-format.
>
> You need to include the follwing jar-files (included in birt engine,
> model and chart api):
> engineapi.jar
> coreapi.jar
> com.ibm.icu...jar
> modelapi.jar
> js.jar
> org.apache.commons.codec...jar
> org.eclipse.birt.chart.engine...jar
> org.eclipse.birt.chart.reportitem.source...jar
> org.eclipse.emf.ecore...jar
> org.eclipse.emf.common...jar
> org.eclipse.emf.ecore.xmi...jar
> scriptapi.jar
>
> Ignore the icons or make your own (png-files made in paint).
|
|
|
|
|
|
Re: create a report in code [message #527255 is a reply to message #527053] |
Wed, 14 April 2010 15:11 |
|
Bob,
You can set properties directly
data = elementFactory.newDataItem( null );
data.setProperty( StyleHandle.FONT_FAMILY_PROP,"Arial");
or modify the default report style property
reportDesignHandle.findStyle("report").setProperty(StyleHandle.FONT_FAMILY_PROP,
"Arial");
or use a new style. To create a style use the ElementFactory like:
private void createStyles( ) throws SemanticException
{
StyleHandle labelStyle = elementFactory.newStyle( "Label" );
labelStyle.setProperty( StyleHandle.FONT_WEIGHT_PROP,
DesignChoiceConstants.FONT_WEIGHT_BOLD );
labelStyle.setProperty( StyleHandle.FONT_FAMILY_PROP, "Arial Black" );
labelStyle.setProperty( StyleHandle.COLOR_PROP, "#008000" );
StyleHandle dataStyle = elementFactory.newStyle( "Data" );
dataStyle.setProperty( StyleHandle.FONT_WEIGHT_PROP,
DesignChoiceConstants.FONT_WEIGHT_BOLD );
dataStyle.setProperty( StyleHandle.FONT_FAMILY_PROP, "Century"
);//$NON-NLS-1$
dataStyle.setProperty( StyleHandle.COLOR_PROP, "#009B9B" );
reportDesignHandle.getStyles( ).add( labelStyle );
reportDesignHandle.getStyles( ).add( dataStyle );
}
Then set the style on an element like:
CellHandle tcell = (CellHandle) header.getCells( ).get( 0 );
LabelHandle label = elementFactory.newLabel( null );
label.setText( "Product" );
label.setStyleName( "Label" );
tcell.getContent( ).add( label );
Jason
Robert Jenkin wrote:
> I have been playing around with this sample and got it working.
> I figured out how to change to landscape and how to change the sizes of
> the data columns.
> I changed the output to pdf.
>
> now I am trying to figure out how to change the font used by the report,
> any ideas?
>
> thanks
>
> bob
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03422 seconds