Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 18th, 2007, 06:34 AM
Registered User
 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can't display CHART with BIRT

Hi,
i'm trying to display a bar chart and a table into a report design with birt.
When I try to display the design with the Eclipse birt preview I'm only see the table.
Here is my class:

import java.io.IOException;

import org.eclipse.birt.report.engine.api.HTMLRenderOptio n;
import org.eclipse.birt.report.model.api.DesignElementHan dle;
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.Intersectio nType ;
import org.eclipse.birt.chart.model.attribute.Position;
import org.eclipse.birt.chart.model.attribute.TickStyle;
import org.eclipse.birt.chart.model.attribute.impl.ColorD efinitionImpl;
import org.eclipse.birt.chart.model.attribute.impl.Gradie ntImpl ;
import org.eclipse.birt.chart.model.component.Axis;
import org.eclipse.birt.chart.model.component.Series;
import org.eclipse.birt.chart.model.component.impl.Series Impl;
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.SeriesDefin itionImpl;
import org.eclipse.birt.chart.model.impl.ChartWithAxesImp l ;
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.BarSeriesIm pl ;
import org.eclipse.birt.chart.engine.*;
import org.eclipse.birt.chart.reportitem.ChartReportItemI mpl;
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.DesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFac tory;
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.OdaDataSourceHan dle;
import org.eclipse.birt.report.model.api.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHand le;
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.Semanti cException;
import org.eclipse.birt.report.model.api.elements.structu res.ComputedColumn;
import org.eclipse.birt.report.model.api.ExtendedItemHand le;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.birt.report.model.api.elements.DesignC hoiceConstants;
import org.eclipse.birt.report.model.elements.interfaces. IStyleModel;
import org.eclipse.birt.report.model.api.extension.Extend edElementException;
import org.eclipse.birt.report.model.metadata.MetaDataDic tionary;
import org.eclipse.birt.report.model.api.metadata.IMetaDa taDictionary;

import com.ibm.icu.util.ULocale;

/**
 * Dynamic Table BIRT Design Engine API (DEAPI) demo.
 */

public class DataCharts
{
    ReportDesignHandle designHandle = null;
    ElementFactory designFactory = null;
    IMetaDataDictionary dict = null;
    String[] cols = {"CITY", "CUSTOMERNUMBER"};

    public static void main( String[] args )
    {
        try
        {
            DataCharts bc = new DataCharts();

            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.Dri ver" );
        dsHandle.setProperty( "odaURL", "jdbc:classicmodels:sampledb" );
        dsHandle.setProperty( "odaUser", "ClassicModels" );
        dsHandle.setProperty( "odaPassword", "" );

        designHandle.getDataSources( ).add( dsHandle );

    }

    void buildDataSet( ) throws SemanticException
    {

        OdaDataSetHandle dsHandle = designFactory.newOdaDataSet( "ChartData",
                "org.eclipse.birt.report.data.oda.jdbc.JdbcSelectD ataSet" );
        dsHandle.setDataSource( "ChartDataSource" );
        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_FACTO RY );
            engine = factory.createDesignEngine( config );

        }catch( Exception ex){
             ex.printStackTrace();
        }

        SessionHandle session = engine.newSessionHandle( ULocale.ENGLISH ) ;
        //SessionHandle session = DesignEngine.newSession( (ULocale) null );

        try{
            //open a design or a template
            //designHandle = session.openDesign("D:/stage/Report.rptdesign");
            designHandle = session.createDesign();

            designFactory = designHandle.getElementFactory( );

            dict = new DesignEngine( null ).getMetaData( );

            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/CHART.rptdesign" ); //$NON-NLS-1$

            designHandle.close( );
            Platform.shutdown();

             System.out.println("Finished");
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    private void createBody() throws SemanticException{


        GridHandle grid = designFactory.newGridItem( null, 1 /* cols */, 1 /* row */ );
        designHandle.getBody( ).add( grid );

        // Note: Set the table width to 100% to prevent the label
        // from appearing too narrow in the layout view.

        grid.setWidth( "100%" ); //$NON-NLS-1$

        // Get the first row.

        RowHandle row = (RowHandle) grid.getRows( ).get( 0 );
        CellHandle cell = (CellHandle) row.getCells( ).get( 0 );
        cell.getContent( ).add(createMultiYSeriesChart());

    }

    protected ExtendedItemHandle 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.setProperty ( ExtendedItemHandle.DATA_SET_PROP, "ChartData" );//$NON-NLS-1$
        }
        catch ( SemanticException e )
        {
            e.printStackTrace( );
        }

        //Chart cwaBar = null;
        ChartWithAxes cwaBar = ChartWithAxesImpl.create( );
        cwaBar.setType("Bar Chart");
        cwaBar.setSubType("Side-by-side");

        // Plot
        cwaBar.getBlock( ).setBackground( ColorDefinitionImpl.WHITE( ) );
        //cwaBar.getBlock( ).getOutline( ).setVisible( true );
        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$

        // 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 );

        //bs.setDataSet( orthoValues1 );
        bs.setRiserOutline( null );
        bs.getLabel( ).setVisible( true );
        bs.setLabelPosition( Position.INSIDE_LITERAL );

        SeriesDefinition sdY1 = SeriesDefinitionImpl.create( );
        sdY1.getSeriesPalette( ).update( 1 );
        yAxisPrimary.getSeriesDefinitions( ).add( sdY1 );
        sdY1.getSeries( ).add( bs );


        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_PRO P,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);
        }
    }

}


Why can't I see the chart???
How can I configure birt to show the charts??

Please, can anybody help me??
Thanks...
 
Old May 19th, 2007, 08:08 AM
Registered User
 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
I didn't found the error yet. I've tried with several examples and the result is the same. I can't preview the design avec the preview Eclipse option.
I don't have any Exception, so I've thought the error can be at the Java Build Path configuration.

Can anybody tell me which .*jar I must put there to display my design??

Please I need to sort out this problem as soon as posible.
Or if you have other idea that can help me....

THANKS.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chart keso VB How-To 2 November 30th, 2006 04:51 AM
How Can one display chart series using arrays? Alseikhan Excel VBA 0 March 10th, 2006 04:22 PM
Using Customized chart for Pivot Chart kliu9 Excel VBA 3 October 13th, 2004 09:37 AM
Drill Down from Bar Chart to another chart snowbird Crystal Reports 0 July 11th, 2003 01:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.