[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[birt-charting-dev] programmatically Bar Chart report design
|
Dear all
i want to create a bar chart
programmatically
with data source and datat set. here i have attached java file which create report design file(rptdesign file ), but the viewer doesnt show the chart and in the log file i have found some error. here is the error. plz help me . any help ?
org.eclipse.birt.chart.exception.ChartException: A data definition _expression_ must be associated with the value (Y) series-0 defined by
org.eclipse.birt.chart.model.type.impl.LineSeriesImpl@f27b6e
(visible: true, seriesIdentifier: B Corp., labelPosition: Above, stacked: false, translucent: <unset>) (paletteLineColor: <unset>, curve: <unset>).
at org.eclipse.birt.chart.internal.factory.DataProcessor.getRowExpressions
(DataProcessor.java:545)
SEVERE: Exception
org.eclipse.birt.chart.exception.ChartException: The DataSet bound to the chart returned no rows.
at org.eclipse.birt.chart.reportitem.ChartReportItemPresentationImpl.onRowSets(ChartReportItemPresentationImpl.java
:371)
SEVERE: Exception
org.eclipse.birt.chart.exception.ChartException: The DataSet bound to the chart returned no rows.
at org.eclipse.birt.chart.reportitem.ChartReportItemPresentationImpl.onRowSets(ChartReportItemPresentationImpl.java
:371)
at org.eclipse.birt.report.engine.presentation.LocalizedEmitter.startExtendedContent(LocalizedEmitter.java:655)
package report.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.Anchor;
import org.eclipse.birt.chart.model.attribute.AxisType;
import org.eclipse.birt.chart.model.attribute.IntersectionType;
import org.eclipse.birt.chart.model.attribute.LineAttributes;
import org.eclipse.birt.chart.model.attribute.LineStyle;
import org.eclipse.birt.chart.model.attribute.MarkerType;
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.AxisImpl;
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.LineSeries;
import org.eclipse.birt.chart.model.type.impl.BarSeriesImpl;
import org.eclipse.birt.chart.model.type.impl.LineSeriesImpl;
import org.eclipse.birt.chart.reportitem.ChartReportItemImpl;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.DesignEngine;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.ExtendedItemHandle;
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.SessionHandle;
import org.eclipse.birt.report.model.api.TableHandle;
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.DataSetParameter;
import org.eclipse.birt.report.model.api.extension.ExtendedElementException;
import org.eclipse.birt.report.model.metadata.MetaDataDictionary;
public class BarChart {
private ReportDesignHandle reportDesignHandle = null;
private ElementFactory elementFactory = null;
private OdaDataSourceHandle customerDataSource = null;
private String dataSourceName = "Customer";
private String customerdataSetName = "Customer";
private String query = "SELECT ProductLine , buyprice , quantityinstock from Products ";
MetaDataDictionary dict;
public void createReport( ) throws SemanticException, IOException
{
init();
createMasterPages( );
createDataSources( );
createDataSets();
createBody( );
String outputPath = "D:" ;
reportDesignHandle.saveAs( outputPath + "/" + "BarChart.rptdesign" );//$NON-NLS-1$//$NON-NLS-2$
}
private void createMasterPages( ) throws ContentException, NameException
{
DesignElementHandle simpleMasterPage = elementFactory.newSimpleMasterPage( "Master Page" );//$NON-NLS-1$
reportDesignHandle.getMasterPages( ).add( simpleMasterPage );
}
private void init(){
//we need a handle of session of design engine
SessionHandle sessionHandle = DesignEngine.newSession(null);
reportDesignHandle = sessionHandle.createDesign();
elementFactory = reportDesignHandle.getElementFactory();
}
private void createDataSets() throws SemanticException{
//type of the data set or extension ID
String dataSetType = "org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet";
//name of the data source
//query to be send to the database
OdaDataSetHandle dataSetHandle = null;
DataSetParameter theDataSetParameter = null;
PropertyHandle propertyHandle = null;
//customer dataset
dataSetHandle = elementFactory.newOdaDataSet(customerdataSetName,dataSetType);
dataSetHandle.setQueryText(query);
dataSetHandle.setDataSource(dataSourceName);
reportDesignHandle.getDataSets().add(dataSetHandle);
}
private void createDataSources( ) throws SemanticException
{
//name of the data source
//type of the data source
String extensionID = "org.eclipse.birt.report.data.oda.jdbc";
//driver class name
String driverClassName = "com.mysql.jdbc.Driver";
//database url
String driverURL = "jdbc:mysql://localhost:3306/classicmodels";
//user name
String userName = "root";
//password
String userPassword = "raju";
//now make a data source with the dataSourceName and extensionID.
customerDataSource = elementFactory.newOdaDataSource(dataSourceName,extensionID);
//set the driver class name
customerDataSource.setProperty("odaDriverClass",driverClassName);
//set the database url
customerDataSource.setProperty("odaURL",driverURL);
//set the user name
customerDataSource.setProperty("odaUser",userName);
//set the password
customerDataSource.setProperty("odaPassword",userPassword);
System.out.println("odaURL is " + customerDataSource.getProperty("odaURL"));
System.out.println("odaUser is " + customerDataSource.getProperty("odaUser"));
System.out.println("odaPassword is " + customerDataSource.getProperty("odaPassword"));
//so far we have developed our data source DOM so now
//lets give the handle to the reportDesignHandle .
reportDesignHandle.getDataSources().add(customerDataSource);
}
private void createBody() throws SemanticException{
TableHandle table = elementFactory.newTableItem(null,1,1,1,1);
table.setWidth("100%");
reportDesignHandle.getBody().add(createMulitYSeriesChart( ));
// RowHandle rowHandle = (RowHandle)table.getDetail().get(0);
// CellHandle cellHandle = (CellHandle)rowHandle.getCells().get(0);
// cellHandle.getContent().add(createPieChart());
}
protected ExtendedItemHandle createMulitYSeriesChart( )
{
ExtendedItemHandle eih = elementFactory.newExtendedItem( null, "Chart" );//$NON-NLS-1$
try
{
eih.setHeight( "5.406in" );//$NON-NLS-1$
eih.setWidth( "7.927in" );//$NON-NLS-1$
eih.setProperty( ExtendedItemHandle.DATA_SET_PROP, customerdataSetName );//$NON-NLS-1$
}
catch ( SemanticException e )
{
e.printStackTrace( );
}
ChartReportItemImpl crii;
try
{
//Add ChartReportItemImpl to ExtendedItemHandle
crii = (ChartReportItemImpl) eih.getReportItem( );
//Add chart instance to ChartReportItemImpl
crii.setProperty( "chart.instance", createMultiYAxisChart( ) );//$NON-NLS-1$
}
catch ( ExtendedElementException e )
{
e.printStackTrace( );
}
System.out.println("this is bar chart");
return eih;
}
protected Chart createMultiYAxisChart( )
{
ChartWithAxes cwaBar = ChartWithAxesImpl.create( );
// Plot
cwaBar.getBlock( ).setBackground( ColorDefinitionImpl.WHITE( ) );
Plot p = cwaBar.getPlot( );
p.getClientArea( ).setBackground( ColorDefinitionImpl.create( 255,
245,
255 ) );
// Title
cwaBar.getTitle( )
.getLabel( )
.getCaption( )
.setValue( "Line Chart with Multiple Y Axis" );//$NON-NLS-1$
// Legend
Legend lg = cwaBar.getLegend( );
LineAttributes lia = lg.getOutline( );
lg.getText( ).getFont( ).setSize( 16 );
lia.setStyle( LineStyle.SOLID_LITERAL );
lg.getInsets( ).set( 10, 5, 0, 0 );
lg.getOutline( ).setVisible( false );
lg.setAnchor( Anchor.NORTH_LITERAL );
// 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 );
// Y-Axis
Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary );
yAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.LEFT_LITERAL );
yAxisPrimary.getTitle( )
.getCaption( )
.setValue( "Sales Growth ($Million)" );//$NON-NLS-1$
// Y-Axis (2)
Axis yAxis = AxisImpl.create( Axis.ORTHOGONAL );
yAxis.setType( AxisType.LINEAR_LITERAL );
yAxis.getMajorGrid( ).setTickStyle( TickStyle.RIGHT_LITERAL );
yAxis.setLabelPosition( Position.RIGHT_LITERAL );
xAxisPrimary.getAssociatedAxes( ).add( yAxis );
// Data Set
// TextDataSet categoryValues = TextDataSetImpl.create( new String[]{
// "March", "April", "May", "June", "July"} );//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$//$NON-NLS-5$
// NumberDataSet orthoValues1 = NumberDataSetImpl.create( new double[]{
// 12.5, 19.6, 18.3, 13.2, 26.5
// } );
// NumberDataSet orthoValues2 = NumberDataSetImpl.create( new double[]{
// 22.7, 23.6, 38.3, 43.2, 40.5
// } );
// X-Series
Series seCategory = SeriesImpl.create( );
Query query = QueryImpl.create( "row[\"ProductLine\"]" );//$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)
LineSeries ls1 = (LineSeries) LineSeriesImpl.create( );
ls1.setSeriesIdentifier( "A Corp." );//$NON-NLS-1$
Query query1 = QueryImpl.create( "row[\"buyprice\"]" );//$NON-NLS-1$
ls1.getDataDefinition( ).add( query1 );
//ls1.setDataSet( orthoValues1 );
ls1.getLineAttributes( ).setColor( ColorDefinitionImpl.CREAM( ) );
ls1.getMarker( ).setType( MarkerType.TRIANGLE_LITERAL );
ls1.getMarker( ).setSize( 10 );
ls1.getLabel( ).setVisible( true );
SeriesDefinition sdY1 = SeriesDefinitionImpl.create( );
sdY1.getSeriesPalette( ).update( -2 );
yAxisPrimary.getSeriesDefinitions( ).add( sdY1 );
sdY1.getSeries( ).add( ls1 );
// Y-Serires (2)
LineSeries ls2 = (LineSeries) LineSeriesImpl.create( );
ls2.setSeriesIdentifier( "B Corp." );//$NON-NLS-1$
Query query2 = QueryImpl.create( "row[\"quantityinstock\"]" );//$NON-NLS-1$
ls1.getDataDefinition( ).add( query2 );
// ls2.setDataSet( orthoValues2 );
ls2.getLineAttributes( ).setColor( ColorDefinitionImpl.CREAM( ) );
ls2.getMarker( ).setType( MarkerType.CIRCLE_LITERAL );
ls2.getMarker( ).setSize( 10 );
ls2.getLabel( ).setVisible( true );
SeriesDefinition sdY2 = SeriesDefinitionImpl.create( );
sdY2.getSeriesPalette( ).update( -3 );
yAxis.getSeriesDefinitions( ).add( sdY2 );
sdY2.getSeries( ).add( ls2 );
return cwaBar;
}
}