birt-使用api生成chart报表

原链接

package com.envision.birt.demo.chart;

import java.io.IOException;

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.IntersectionType;
import org.eclipse.birt.chart.model.attribute.LegendItemType;
import org.eclipse.birt.chart.model.attribute.Palette;
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.PaletteImpl;
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.BaseSampleData;
import org.eclipse.birt.chart.model.data.DataFactory;
import org.eclipse.birt.chart.model.data.OrthogonalSampleData;
import org.eclipse.birt.chart.model.data.SampleData;
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.LineSeries;
import org.eclipse.birt.chart.model.type.impl.LineSeriesImpl;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.DesignConfig;
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.ReportDesignHandle;
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.PropertyBinding;

import com.ibm.icu.util.ULocale;
/**
 * @author YangHaojie
 * @date 2021/3/10
 */
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;

    org.eclipse.birt.report.model.api.elements.structures.ComputedColumn 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();

        buildDataSource();
        buildDataSet();
        createBody();

        reportDesignHandle.saveAs("C:\\Users\\haojie.yang2\\Desktop\\birt\\tmp\\simplechart.rptdesign");
        reportDesignHandle.close( );
        Platform.shutdown();
        System.out.println("Finished");

    }


    private void init(){


        DesignConfig config = new DesignConfig( );
        config.setBIRTHome("C:\\Users\\haojie.yang2\\Desktop\\birt\\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 = 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 createBody() throws SemanticException

    {
        ExtendedItemHandle extendedItemHandle = elementFactory.newExtendedItem("Simple Chart", "Chart");
        extendedItemHandle.setWidth("700px");
        extendedItemHandle.setHeight("500px");
        extendedItemHandle.setProperty(ExtendedItemHandle.DATA_SET_PROP, dataSetName);
        extendedItemHandle.setProperty("outputFormat","PNG");

        Chart c = createChart();

        extendedItemHandle.getReportItem().setProperty( "chart.instance", c );

        reportDesignHandle.getBody().add(extendedItemHandle);

//        cs1 = StructureFactory.createComputedColumn( );
//        cs1.setName( "PRODUCTCODE1" );
//        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);
        cwaLine.getTitle().getLabel().getCaption().setColor(ColorDefinitionImpl.create(172,172,172,0));

        // Legend
        cwaLine.getLegend().setVisible( true );
        Legend lg = cwaLine.getLegend();
        lg.setItemType(LegendItemType.CATEGORIES_LITERAL);

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

        SampleData sd = DataFactory.eINSTANCE.createSampleData( );
        BaseSampleData sdBase = DataFactory.eINSTANCE.createBaseSampleData( );
        sdBase.setDataSetRepresentation( "Category-A, Category-B" );//$NON-NLS-1$
        sd.getBaseSampleData( ).add( sdBase );


        OrthogonalSampleData sdOrthogonal = DataFactory.eINSTANCE.createOrthogonalSampleData( );
        sdOrthogonal.setDataSetRepresentation( "4,12" );//$NON-NLS-1$
        sdOrthogonal.setSeriesDefinitionIndex( 0 );
        sd.getOrthogonalSampleData( ).add( sdOrthogonal );

        cwaLine.setSampleData( sd );

        Series seCategory = SeriesImpl.create( );

        seCategory.getDataDefinition( )
                .add( QueryImpl.create( "row[\"PRODUCTCODE\"]" ) );

        SeriesDefinition sdX = SeriesDefinitionImpl.create( );
        Palette palx = PaletteImpl.create(10, false);
        sdX.setSeriesPalette(palx);
        xAxisPrimary.getSeriesDefinitions( ).add( sdX );
        sdX.getSeries( ).add( seCategory );
        // Y-Series
        LineSeries bs1 = (LineSeries) LineSeriesImpl.create( );
        LineSeries bs2 = (LineSeries) LineSeriesImpl.create( );

        bs1.getDataDefinition( ).add( QueryImpl.create( "row[\"QUANTITYORDERED\"]" ) );
        bs1.getLabel( ).setVisible( true );

        bs2.getDataDefinition( ).add( QueryImpl.create( "row[\"QUANTITYORDERED1\"]" ) );
        bs2.getLabel( ).setVisible( true );


        SeriesDefinition sdY = SeriesDefinitionImpl.create( );
        SeriesDefinition sdY1 = SeriesDefinitionImpl.create( );

        sdY.getGrouping().setEnabled(false);
        yAxisPrimary.getSeriesDefinitions( ).add( sdY );
        yAxisPrimary.getSeriesDefinitions( ).add( sdY1);

        sdY.getSeries( ).add( bs1 );
        sdY1.getSeries( ).add( bs2 );


        return cwaLine;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值