Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » BIRT » Generating a chart image from an rptdesign file
Generating a chart image from an rptdesign file [message #757148] Wed, 16 November 2011 18:47 Go to next message
Todd Blackwell is currently offline Todd BlackwellFriend
Messages: 5
Registered: November 2011
Junior Member
Hi All,

We currently have a number of report design files that contain a single chart. I'd like to be able to create an image representing that chart from that definition, and then return the URL for that image to a web application for display.

Currently, the only way I see to do this is to use the HTMLRenderOption, set the image directory to one available to the web application, and run the report. The chart will be automatically created as an image, and included into the generated HTML report, but also be available as an image from the specified image directory.

I'd like to bypass having to create the HTML file, and just create the chart directly, using the data set defined in the design file, or even switching it out for another one at runtime based on the input parameters from the web application.

So far, the only thing I've been able to accomplish is to write a simple test application that extracts the chart from the design file, and use a Generator object to render the chart. The only problem with this is that this method doesn't seem to automatically use the data set defined in the design file to populate the chart with data. Here's the simple class I wrote:

import org.eclipse.birt.chart.device.IDeviceRenderer;
import org.eclipse.birt.chart.exception.ChartException;
import org.eclipse.birt.chart.model.ChartWithAxes;
import org.eclipse.birt.chart.util.PluginSettings;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignFileException;
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.ReportDesignHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.extension.ExtendedElementException;
import org.eclipse.birt.chart.factory.GeneratedChartState;
import org.eclipse.birt.chart.factory.Generator;
import org.eclipse.birt.chart.factory.RunTimeContext;
import org.eclipse.birt.chart.model.attribute.Bounds;
import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;

import com.ibm.icu.util.ULocale;

public class ChartTest
{
   private void createChart()
   {
      ReportDesignHandle designHandle = null;
      DesignConfig config = new DesignConfig();

      // config.setBIRTHome("C:/birt/birt-runtime-2_5_1/birt-runtime-2_5_1/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("/nethome/eblack04/AnomalyGraphTemplate.rptdesign");
      }
      catch(DesignFileException e)
      {
         e.printStackTrace();
      }

      ExtendedItemHandle eih = (ExtendedItemHandle) designHandle.getBody().getContents().get(4);

      ChartWithAxes chart = null;
      try
      {
         chart = (ChartWithAxes)eih.getReportItem().getProperty("chart.instance");
         System.out.println("Chart is " + (chart == null ? "" : "not ") + "null");

         PluginSettings ps = PluginSettings.instance();
         IDeviceRenderer renderer = ps.getDevice("dv.PNG");
         
         renderer.setProperty(IDeviceRenderer.FILE_IDENTIFIER, "/nethome/eblack04/AnomalyGraphTemplate.png");

         RunTimeContext rtc = new RunTimeContext( );
         rtc.setULocale( ULocale.getDefault( ) );

         final Generator gr = Generator.instance( );
         GeneratedChartState gcs = null;
         //Set the chart size
         Bounds bounds = BoundsImpl.create( 0, 0, 450, 300 );
         gcs = gr.build( renderer.getDisplayServer( ), chart, bounds, null, rtc, null );
         
         //generate the chart
         gr.render(renderer, gcs);
      }
      catch(ExtendedElementException e)
      {
         e.printStackTrace();
      }
      catch(ChartException e)
      {
         e.printStackTrace();
      }
   }

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


Do I have to pull out the data set defined in the report design file as well as the chart, and then set that data set into the chart before using the generator to create it? If so, how? Any help would be greatly appreciated on this.

Thanks,

Todd
Re: Generating a chart image from an rptdesign file [message #757828 is a reply to message #757148] Wed, 16 November 2011 22:05 Go to previous message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Todd,

This will not be easy to do as you will have to reproduce a lot of
classes BIRT uses to process the data. A better approach may be to
write your own emitter (Much like the html emitter) and just throw
everything away but the chart generation piece.

Jason

On 11/16/2011 1:47 PM, Todd Blackwell wrote:
> Hi All,
>
> We currently have a number of report design files that contain a single
> chart. I'd like to be able to create an image representing that chart
> from that definition, and then return the URL for that image to a web
> application for display.
>
> Currently, the only way I see to do this is to use the HTMLRenderOption,
> set the image directory to one available to the web application, and run
> the report. The chart will be automatically created as an image, and
> included into the generated HTML report, but also be available as an
> image from the specified image directory.
>
> I'd like to bypass having to create the HTML file, and just create the
> chart directly, using the data set defined in the design file, or even
> switching it out for another one at runtime based on the input
> parameters from the web application.
>
> So far, the only thing I've been able to accomplish is to write a simple
> test application that extracts the chart from the design file, and use a
> Generator object to render the chart. The only problem with this is that
> this method doesn't seem to automatically use the data set defined in
> the design file to populate the chart with data. Here's the simple class
> I wrote:
>
>
> import org.eclipse.birt.chart.device.IDeviceRenderer;
> import org.eclipse.birt.chart.exception.ChartException;
> import org.eclipse.birt.chart.model.ChartWithAxes;
> import org.eclipse.birt.chart.util.PluginSettings;
> import org.eclipse.birt.core.framework.Platform;
> import org.eclipse.birt.report.model.api.DesignConfig;
> import org.eclipse.birt.report.model.api.DesignFileException;
> 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.ReportDesignHandle;
> import org.eclipse.birt.report.model.api.SessionHandle;
> import
> org.eclipse.birt.report.model.api.extension.ExtendedElementException;
> import org.eclipse.birt.chart.factory.GeneratedChartState;
> import org.eclipse.birt.chart.factory.Generator;
> import org.eclipse.birt.chart.factory.RunTimeContext;
> import org.eclipse.birt.chart.model.attribute.Bounds;
> import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
>
> import com.ibm.icu.util.ULocale;
>
> public class ChartTest
> {
> private void createChart()
> {
> ReportDesignHandle designHandle = null;
> DesignConfig config = new DesignConfig();
>
> //
> config.setBIRTHome("C:/birt/birt-runtime-2_5_1/birt-runtime-2_5_1/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("/nethome/eblack04/AnomalyGraphTemplate.rptdesign");
>
> }
> catch(DesignFileException e)
> {
> e.printStackTrace();
> }
>
> ExtendedItemHandle eih = (ExtendedItemHandle)
> designHandle.getBody().getContents().get(4);
>
> ChartWithAxes chart = null;
> try
> {
> chart = (ChartWithAxes)eih.getReportItem().getProperty("chart.instance");
> System.out.println("Chart is " + (chart == null ? "" : "not ") + "null");
>
> PluginSettings ps = PluginSettings.instance();
> IDeviceRenderer renderer = ps.getDevice("dv.PNG");
> renderer.setProperty(IDeviceRenderer.FILE_IDENTIFIER,
> "/nethome/eblack04/AnomalyGraphTemplate.png");
>
> RunTimeContext rtc = new RunTimeContext( );
> rtc.setULocale( ULocale.getDefault( ) );
>
> final Generator gr = Generator.instance( );
> GeneratedChartState gcs = null;
> //Set the chart size
> Bounds bounds = BoundsImpl.create( 0, 0, 450, 300 );
> gcs = gr.build( renderer.getDisplayServer( ), chart, bounds, null, rtc,
> null );
> //generate the chart
> gr.render(renderer, gcs);
> }
> catch(ExtendedElementException e)
> {
> e.printStackTrace();
> }
> catch(ChartException e)
> {
> e.printStackTrace();
> }
> }
>
> /**
> * execute application
> * * @param args
> */
> public static void main(String[] args)
> {
> new ChartTest().createChart();
> }
> }
>
>
> Do I have to pull out the data set defined in the report design file as
> well as the chart, and then set that data set into the chart before
> using the generator to create it? If so, how? Any help would be greatly
> appreciated on this.
>
> Thanks,
>
> Todd
Previous Topic:Chart ReportItems in a library
Next Topic:Open a .chart file in the eclipse reporting platform?
Goto Forum:
  


Current Time: Tue Apr 23 08:15:57 GMT 2024

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

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

Back to the top