Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » BIRT » Setting x & y axis series in Chart Engine API with dataset(Data set is a query from the user and the series' should be values from the query)
Setting x & y axis series in Chart Engine API with dataset [message #660817] Mon, 21 March 2011 18:15 Go to next message
Olly   is currently offline Olly Friend
Messages: 61
Registered: June 2010
Location: Florida
Member
I am trying to use the Chart Engine API to create a line graph. The data source is already set and so is the data set. The data set's query is generated from the user. I am able to create a report using the report designer tool with a sample query and I am able to set the Y-Axis series to be "row["value"]" which will be values returned from the database, the x-axis series to be "row[time"]" and that the y-series grouping to be row["prod_name"]. My question is how do I do this programmatically? I am at the point in my code that I am creating the chart and I need to specify the series dataset, Series.setDataSet(????). If i'm not being clear enough please let me know. I read through the XML code in my .rptdesign that has the designed chart and then I found the Series Interface API and I was reading getDataDefinition() and I read "Defines the source of the data for the series. (e.g. Database field which will provide the values for the series)." This is what I need to do. Can anyone help? I need to define the source of the data which will be a database field that will provide the values for that series.

[Updated on: Mon, 21 March 2011 19:37]

Report message to a moderator

Re: Setting x & y axis series in Chart Engine API with dataset [message #660842 is a reply to message #660817] Mon, 21 March 2011 22:15 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

In your code are you building a report or just the chart? Does the
chart go in a report?

Here is an example that creates a report with a chart in it.

package DEAPI;

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

Jason

On 3/21/2011 2:15 PM, azuniga wrote:
> I am trying to use the Chart Engine API to create a line graph. The data
> source is already set and so is the data set. The data set's query is
> generated from the user. I am able to create a report using the report
> designer tool with a sample query and I am able to set the Y-Axis series
> to be "row["value"]" which will be values returned from the database,
> the x-axis series to be "row[time"]" and that the y-series grouping to
> be row["prod_name"]. My question is how do I do this programmatically? I
> am at the point in my code that I am creating the chart and I need to
> specify the series dataset, Series.setDataSet(????). If i'm not being
> clear enough please let me know.
Re: Setting x & y axis series in Chart Engine API with dataset [message #660935 is a reply to message #660842] Tue, 22 March 2011 12:36 Go to previous messageGo to next message
Olly   is currently offline Olly Friend
Messages: 61
Registered: June 2010
Location: Florida
Member
Thanks Jason, I'm gonna go ahead and try editing and running that example. Now my question is the following. Is it possible to have a data source, data set, chart and table in the .rptdesign and then modify the data set, then run the report and have the elements display properly? The reason I ask is b/c at the moment I am designing and creating a new report every time the user clicks generate report. I have a blank.rptdesign with only the datasource already set up and I grab that blank.rptdesign, create the data source, create a chart and create a table and run it. But in reality the user is always using the same data set and the only thing that really changes is the query. But the query always displays all the columns. Here is a sample query: "SELECT * FROM table WHERE ( product_name = 'Green' or product_name = 'Blue') AND product_time BETWEEN TO_DATE('21-FEB-00 03:45:05 PM', 'DD-MON-YY HH:MI:SS PM') AND
TO_DATE('24-FEB-00 03:45:05 PM', 'DD-MON-YY HH:MI:SS PM'). The user selects what product names he wants to match, 'green', 'blue' etc.. and what dates it should be between. So the table displaying the query results contains all the columns. I'm curious if it would be smarter or simpler or faster to have the .rptdesign all set up and then make the changes. Thanks for your help. If i'm not being clear let me know plz. When I run your sample code I get the error: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at com.harris.birt.chart.SimpleChart.createReport(SimpleChart.j ava:128)
at com.harris.birt.chart.SimpleChart.main(SimpleChart.java:89)
at the line of code:
ExtendedItemHandle eih = (ExtendedItemHandle)reportDesignHandle.getBody( ).getContents( ).get( 0 );

So I was able to just grab the query and modify it and run the report. Now I have a chart and a table in the .rptdesign file and I just modify the data set query and run the report and voila. Never mind the question then.

[Updated on: Tue, 22 March 2011 20:11]

Report message to a moderator

Re: Setting x & y axis series in Chart Engine API with dataset [message #661060 is a reply to message #660935] Tue, 22 March 2011 23:41 Go to previous message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

If the same column names are returned every time absolutely. If all you
are doing is changing the where clause you could even just do this in
script in the beforeOpen of the dataset. You can event do it if the
columns change but this will require more code to update the bindings on
the table and chart.

Jason

On 3/22/2011 8:36 AM, azuniga wrote:
> Thanks Jason, I'm gonna go ahead and try editing and running that
> example. Now my question is the following. Is it possible to have a data
> source, data set, chart and table in the .rptdesign and then modify the
> data set, then run the report and have the elements display properly?
> The reason I ask is b/c at the moment I am designing and creating a new
> report every time the user clicks generate report. I have a
> blank.rptdesign with only the datasource already set up and I grab that
> blank.rptdesign, create the data source, create a chart and create a
> table and run it. But in reality the user is always using the same data
> set and the only thing that really changes is the query. But the query
> always displays all the columns. Here is a sample query: "SELECT * FROM
> table WHERE ( product_name = 'Green' or product_name = 'Blue') AND
> product_time BETWEEN TO_DATE('21-FEB-00 03:45:05 PM', 'DD-MON-YY
> HH:MI:SS PM') AND
> TO_DATE('24-FEB-00 03:45:05 PM', 'DD-MON-YY HH:MI:SS PM'). The user
> selects what product names he wants to match, 'green', 'blue' etc.. and
> what dates it should be between. So the table displaying the query
> results contains all the columns. I'm curious if it would be smarter or
> simpler or faster to have the .rptdesign all set up and then make the
> changes. Thanks for your help. If i'm not being clear let me know plz.
Previous Topic:Uncheck "connect missing values" doesn't work
Next Topic:Exception Stack Trace
Goto Forum:
  


Current Time: Thu Apr 18 18:26:38 GMT 2024

Powered by FUDForum. Page generated in 0.03716 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top