Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » BIRT » Create chart programmatically BIRT and RPC service(Create chart programmatically BIRT and RPC service)
icon4.gif  Create chart programmatically BIRT and RPC service [message #677064] Tue, 07 June 2011 08:59 Go to next message
wolverine  is currently offline wolverine
Messages: 1
Registered: July 2010
Junior Member

Hi all,

I am trying to create a bar chart with BIRT chart engine and report engine.
I am successfully able to create a simple report and view it in the GWT programmatically. I am also able to add dynamic tables to my report. Now I am trying to add a chart to my report. but I am getting null pointer exception. Anybdy have any idea about the following exception?

I am using following code to create a charthandle.

CODE:
public static final Chart createAxisFormatChart() {
ChartWithAxes cwaBar = ChartWithAxesImpl.create();
// 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 Formatted Axes");//$NON-NLS-1$

// Legend
Legend lg = cwaBar.getLegend();
lg.getText().getFont().setSize(16);
lg.setItemType(LegendItemType.CATEGORIES_LITERAL);

// X-Axis
Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes()[0];
xAxisPrimary.setCategoryAxis(true);
xAxisPrimary.setType(AxisType.DATE_TIME_LITERAL);
xAxisPrimary.getMajorGrid().setTickStyle(TickStyle.BELOW_LITERAL);
xAxisPrimary.getOrigin().setType(IntersectionType.VALUE_LITERAL);
xAxisPrimary.getTitle().getCaption().setValue("Regional Markets"); //$NON-NLS-1$
xAxisPrimary.setTitlePosition(Position.BELOW_LITERAL);
xAxisPrimary.setLabelPosition(Position.BELOW_LITERAL);
xAxisPrimary.setFormatSpecifier(JavaDateFormatSpecifierImpl
.create("MM/dd/yyyy"));//$NON-NLS-1$

// Y-Axis
Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis(xAxisPrimary);
yAxisPrimary.getMajorGrid().setTickStyle(TickStyle.LEFT_LITERAL);
yAxisPrimary.getTitle().getCaption().setValue("Net Profit");//$NON-NLS-1$
yAxisPrimary.setFormatSpecifier(JavaNumberFormatSpecifierImpl
.create("$###,###"));//$NON-NLS-1$

// Data Set
DateTimeDataSet categoryValues = DateTimeDataSetImpl
.create(new Calendar[] { new CDateTime(2001, 5, 1),
new CDateTime(2001, 4, 11), new CDateTime(2001, 8, 23) });
NumberDataSet orthoValues = NumberDataSetImpl.create(new double[] {
16170, 24210, -4300 });

// X-Series
Series seCategory = SeriesImpl.create();
seCategory.setDataSet(categoryValues);

SeriesDefinition sdX = SeriesDefinitionImpl.create();
sdX.getSeriesPalette().shift(-2);
sdX.getSeries().add(seCategory);
xAxisPrimary.getSeriesDefinitions().add(sdX);

// Y-Series
BarSeries bs = (BarSeries) BarSeriesImpl.create();
bs.setSeriesIdentifier("Sales");//$NON-NLS-1$
bs.setDataSet(orthoValues);
bs.setRiserOutline(null);
bs.getLabel().setVisible(true);
bs.setLabelPosition(Position.INSIDE_LITERAL);

SeriesDefinition sdY = SeriesDefinitionImpl.create();
sdY.getSeries().add(bs);
yAxisPrimary.getSeriesDefinitions().add(sdY);

return cwaBar;
}



Exception :

java.lang.NullPointerException
at org.eclipse.birt.chart.reportitem.ChartReportItemImpl.validate(ChartReportItemImpl.java:801)
at org.eclipse.birt.report.model.api.validators.ExtensionValidator.doValidate(ExtensionValidator.java:142)
at org.eclipse.birt.report.model.api.validators.ExtensionValidator.validate(ExtensionValidator.java:81)
at org.eclipse.birt.report.model.validators.ValidationNode.perform(ValidationNode.java:133)
at org.eclipse.birt.report.model.validators.ValidationExecutor.perform(ValidationExecutor.java:79)
at org.eclipse.birt.report.model.core.DesignElement.validateWithContents(DesignElement.java:2121)
at org.eclipse.birt.report.model.core.DesignElement.validateWithContents(DesignElement.java:2130)
at org.eclipse.birt.report.model.core.ModuleImpl.semanticCheck(ModuleImpl.java:1173)
at org.eclipse.birt.report.model.core.LayoutModule.semanticCheck(LayoutModule.java:304)
at org.eclipse.birt.report.model.core.LayoutModule.doClone(LayoutModule.java:1125)
at org.eclipse.birt.report.model.elements.ReportDesignImpl.doClone(ReportDesignImpl.java:309)
at org.eclipse.birt.report.model.api.ModuleHandleImpl.copy(ModuleHandleImpl.java:3050)
at org.eclipse.birt.report.engine.api.impl.ReportRunnable.cloneRunnable(ReportRunnable.java:132)
at org.eclipse.birt.report.engine.api.impl.EngineTask.prepareDesign(EngineTask.java:1787)
at org.eclipse.birt.report.engine.api.impl.RunAndRenderTask.doRun(RunAndRenderTask.java:99)
at org.eclipse.birt.report.engine.api.impl.RunAndRenderTask.run(RunAndRenderTask.java:77)
Re: Create chart programmatically BIRT and RPC service [message #677198 is a reply to message #677064] Tue, 07 June 2011 14:33 Go to previous message
Jason Weathersby is currently offline Jason Weathersby
Messages: 9167
Registered: July 2009
Senior Member

Take a look at this example.



import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import org.eclipse.birt.chart.model.Chart;
import org.eclipse.birt.chart.model.ChartWithAxes;
import org.eclipse.birt.chart.model.attribute.Anchor;
import org.eclipse.birt.chart.model.attribute.AxisType;
import org.eclipse.birt.chart.model.attribute.ChartDimension;
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.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.NumberDataSet;
import org.eclipse.birt.chart.model.data.Query;
import org.eclipse.birt.chart.model.data.SeriesDefinition;
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.QueryImpl;
import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
import org.eclipse.birt.chart.model.data.impl.TextDataSetImpl;
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.core.framework.Platform;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignEngine;
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.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
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.extension.ExtendedElementException;
import org.eclipse.birt.chart.model.data.*;
import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;

import com.ibm.icu.util.ULocale;


public class AddChart
{

/**
* execute application
*
* @param args
*/
public static void main( String[] args )
{
new AddChart( ).addMyChart( );

}

/**
* Get the chart instance from the design file and add series grouping key.
*
* @return An instance of the simulated runtime chart model (containing
* filled datasets)
*/
void addMyChart( )
{
ReportDesignHandle designHandle = null;
DesignConfig config = new DesignConfig( );

config.setBIRTHome("C:/birt/birt-runtime-2_6_2/birt-runtime-2_6_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 sessionHandle = engine.newSessionHandle( (ULocale) null );

try
{
designHandle = sessionHandle.openDesign(
"report/reporttemplate.rpttemplate" );//$NON-NLS-1$
ElementFactory designFactory = designHandle.getElementFactory( );
designHandle.getBody( ).add( createMyChart(designFactory) );
}
catch ( Exception e )
{
// TODO Auto-generated catch block
e.printStackTrace( );
}


try
{
designHandle.saveAs( "report/myaddedchart.rptdesign" );//$NON-NLS-1$
System.out.println("Finished");
}
catch ( IOException e )
{
e.printStackTrace( );
}
Platform.shutdown();

}
public static ExtendedItemHandle createMyChart(ElementFactory
designFactory)
{

ExtendedItemHandle eih = designFactory.newExtendedItem (
null,"Chart" );
ComputedColumn cs1=null, cs2=null;
try
{
eih.setHeight( "175pt" );//$NON-NLS-1$
eih.setWidth( "450pt" );//$NON-NLS-1$
eih.setProperty ( ExtendedItemHandle.DATA_SET_PROP,
"ChartData");//$NON-NLS-1$
eih.setProperty("outputFormat", "PNG");

PropertyHandle cs = eih.getColumnBindings();
cs1 = StructureFactory.createComputedColumn();
cs2 = StructureFactory.createComputedColumn();
cs1.setName("xaxis");
cs1.setDataType("string");
cs2.setName("yaxis");
cs2.setDataType("float");

cs1.setExpression("dataSetRow[\"xaxis\"]");
cs2.setExpression("dataSetRow[\"yaxis\"]");
cs.addItem(cs1);
cs.addItem(cs2);
}
catch ( SemanticException e )
{
e.printStackTrace( );
}


// BAR CHARTS ARE BASED ON CHARTS THAT CONTAIN AXES

ChartWithAxes cwaBar = ChartWithAxesImpl.create();
cwaBar.setType("Bar Chart"); //$NON-NLS-1$
cwaBar.setSubType("Side-by-side");
cwaBar.getBlock().setBounds( BoundsImpl.create( 0, 0, 450, 175));


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


cwaBar.setSampleData( sd );



cwaBar.getBlock().setBackground(

ColorDefinitionImpl.WHITE()

);

cwaBar.getBlock().getOutline().setVisible(true);

cwaBar.setDimension(

ChartDimension.TWO_DIMENSIONAL_WITH_DEPTH_LITERAL

);

// CUSTOMIZE THE PLOT

Plot p = cwaBar.getPlot();

p.getClientArea().setBackground(

ColorDefinitionImpl.create(255, 255, 225)

);

p.getOutline().setVisible(false);

cwaBar.getTitle().getLabel().getCaption().setValue(

"Simple Bar Chart"

);

// CUSTOMIZE THE LEGEND

Legend lg = cwaBar.getLegend();

lg.getText().getFont().setSize(16);

lg.getInsets().set(10, 5, 0, 0);

lg.setAnchor(Anchor.NORTH_LITERAL);

// CUSTOMIZE THE 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().setVisible(false);

// CUSTOMIZE THE Y-AXIS

Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis(

xAxisPrimary

);

yAxisPrimary.getMajorGrid().setTickStyle(

TickStyle.LEFT_LITERAL

);

yAxisPrimary.setType(AxisType.LINEAR_LITERAL);

yAxisPrimary.getLabel().getCaption().getFont().setRotation(90);

// INITIALIZE A COLLECTION WITH THE X-SERIES DATA

Vector vs = new Vector();

vs.add("zero");

vs.add("one");

vs.add("two");

TextDataSet categoryValues = TextDataSetImpl.create(vs);

// INITIALIZE A COLLECTION WITH THE Y-SERIES DATA

ArrayList vn1 = new ArrayList();

vn1.add(new Double(25));

vn1.add(new Double(35));

vn1.add(new Double(-45));

NumberDataSet orthoValues1 = NumberDataSetImpl.create(

vn1

);

// CREATE THE CATEGORY BASE SERIES

Series seCategory = SeriesImpl.create();
Query xQ = QueryImpl.create( "row[\"xaxis\"]" );
seCategory.getDataDefinition( ).add( xQ );
//seCategory.setDataSet(categoryValues);

// CREATE THE VALUE ORTHOGONAL SERIES

BarSeries bs1 = (BarSeries) BarSeriesImpl.create();

bs1.setSeriesIdentifier("My Bar Series");

//bs1.setDataSet(orthoValues1);
Query yQ = QueryImpl.create( "row[\"yaxis\"]" );
bs1.getDataDefinition( ).add( yQ );
bs1.setRiserOutline(null);

bs1.getLabel().setVisible(true);

bs1.setLabelPosition(Position.INSIDE_LITERAL);

// WRAP THE BASE SERIES IN THE X-AXIS SERIES DEFINITION

SeriesDefinition sdX = SeriesDefinitionImpl.create();

sdX.getSeriesPalette().update(0); // SET THE COLORS IN THE PALETTE

xAxisPrimary.getSeriesDefinitions().add(sdX);

sdX.getSeries().add(seCategory);

// WRAP THE ORTHOGONAL SERIES IN THE X-AXIS SERIES DEFINITION

SeriesDefinition sdY = SeriesDefinitionImpl.create();

sdY.getSeriesPalette().update(1); // SET THE COLOR IN THE PALETTE

yAxisPrimary.getSeriesDefinitions().add(sdY);

sdY.getSeries().add(bs1);

try
{
//Add ChartReportItemImpl to ExtendedItemHandle
eih.getReportItem( ).setProperty( "chart.instance", cwaBar );
}
catch ( ExtendedElementException e )
{
e.printStackTrace( );
}

return eih;

}
}

Jason

On 6/7/2011 8:59 AM, Bhushan Barhate wrote:
> Hi all,
>
> I am trying to create a bar chart with BIRT chart engine and report engine.
> I am successfully able to create a simple report and view it in the GWT
> programmatically. I am also able to add dynamic tables to my report. Now
> I am trying to add a chart to my report. but I am getting null pointer
> exception. Anybdy have any idea about the following exception?
>
> I am using following code to create a charthandle.
>
> CODE: public static final Chart createAxisFormatChart() {
> ChartWithAxes cwaBar = ChartWithAxesImpl.create();
> // 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 Formatted Axes");//$NON-NLS-1$
>
> // Legend
> Legend lg = cwaBar.getLegend();
> lg.getText().getFont().setSize(16);
> lg.setItemType(LegendItemType.CATEGORIES_LITERAL);
>
> // X-Axis
> Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes()[0];
> xAxisPrimary.setCategoryAxis(true);
> xAxisPrimary.setType(AxisType.DATE_TIME_LITERAL);
> xAxisPrimary.getMajorGrid().setTickStyle(TickStyle.BELOW_LITERAL);
> xAxisPrimary.getOrigin().setType(IntersectionType.VALUE_LITERAL);
> xAxisPrimary.getTitle().getCaption().setValue("Regional Markets");
> //$NON-NLS-1$ xAxisPrimary.setTitlePosition(Position.BELOW_LITERAL);
> xAxisPrimary.setLabelPosition(Position.BELOW_LITERAL);
> xAxisPrimary.setFormatSpecifier(JavaDateFormatSpecifierImpl
> .create("MM/dd/yyyy"));//$NON-NLS-1$
>
> // Y-Axis
> Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis(xAxisPrimary);
> yAxisPrimary.getMajorGrid().setTickStyle(TickStyle.LEFT_LITERAL);
> yAxisPrimary.getTitle().getCaption().setValue("Net Profit");//$NON-NLS-1$
> yAxisPrimary.setFormatSpecifier(JavaNumberFormatSpecifierImpl
> .create("$###,###"));//$NON-NLS-1$
>
> // Data Set
> DateTimeDataSet categoryValues = DateTimeDataSetImpl
> .create(new Calendar[] { new CDateTime(2001, 5, 1),
> new CDateTime(2001, 4, 11), new CDateTime(2001, 8, 23) });
> NumberDataSet orthoValues = NumberDataSetImpl.create(new double[] {
> 16170, 24210, -4300 });
>
> // X-Series
> Series seCategory = SeriesImpl.create();
> seCategory.setDataSet(categoryValues);
>
> SeriesDefinition sdX = SeriesDefinitionImpl.create();
> sdX.getSeriesPalette().shift(-2);
> sdX.getSeries().add(seCategory);
> xAxisPrimary.getSeriesDefinitions().add(sdX);
>
> // Y-Series
> BarSeries bs = (BarSeries) BarSeriesImpl.create();
> bs.setSeriesIdentifier("Sales");//$NON-NLS-1$
> bs.setDataSet(orthoValues);
> bs.setRiserOutline(null);
> bs.getLabel().setVisible(true);
> bs.setLabelPosition(Position.INSIDE_LITERAL);
>
> SeriesDefinition sdY = SeriesDefinitionImpl.create();
> sdY.getSeries().add(bs);
> yAxisPrimary.getSeriesDefinitions().add(sdY);
>
> return cwaBar;
> }
>
>
>
> Exception :
> java.lang.NullPointerException
> at
> org.eclipse.birt.chart.reportitem.ChartReportItemImpl.validate(ChartReportItemImpl.java:801)
>
> at
> org.eclipse.birt.report.model.api.validators.ExtensionValidator.doValidate(ExtensionValidator.java:142)
>
> at
> org.eclipse.birt.report.model.api.validators.ExtensionValidator.validate(ExtensionValidator.java:81)
>
> at
> org.eclipse.birt.report.model.validators.ValidationNode.perform(ValidationNode.java:133)
>
> at
> org.eclipse.birt.report.model.validators.ValidationExecutor.perform(ValidationExecutor.java:79)
>
> at
> org.eclipse.birt.report.model.core.DesignElement.validateWithContents(DesignElement.java:2121)
>
> at
> org.eclipse.birt.report.model.core.DesignElement.validateWithContents(DesignElement.java:2130)
>
> at
> org.eclipse.birt.report.model.core.ModuleImpl.semanticCheck(ModuleImpl.java:1173)
>
> at
> org.eclipse.birt.report.model.core.LayoutModule.semanticCheck(LayoutModule.java:304)
>
> at
> org.eclipse.birt.report.model.core.LayoutModule.doClone(LayoutModule.java:1125)
>
> at
> org.eclipse.birt.report.model.elements.ReportDesignImpl.doClone(ReportDesignImpl.java:309)
>
> at
> org.eclipse.birt.report.model.api.ModuleHandleImpl.copy(ModuleHandleImpl.java:3050)
>
> at
> org.eclipse.birt.report.engine.api.impl.ReportRunnable.cloneRunnable(ReportRunnable.java:132)
>
> at
> org.eclipse.birt.report.engine.api.impl.EngineTask.prepareDesign(EngineTask.java:1787)
>
> at
> org.eclipse.birt.report.engine.api.impl.RunAndRenderTask.doRun(RunAndRenderTask.java:99)
>
> at
> org.eclipse.birt.report.engine.api.impl.RunAndRenderTask.run(RunAndRenderTask.java:77)
>
>
Previous Topic:3D bar chart, with multiple series spacing
Next Topic:How to give security option in BIRT reporting tool
Goto Forum:
  


Current Time: Sat Aug 23 13:26:47 EDT 2014

Powered by FUDForum. Page generated in 0.01898 seconds