0

i am able to render my charts to different file types:

 dRenderer = ce.getRenderer("dv.SVG");//or dv.PNG
 dRenderer.setProperty(IDeviceRenderer.FILE_IDENTIFIER, path);

but how to get the binary data as a stream? the charts will most likely be end in some one's browser. so a file on disk i relativly useless.

(it would be stupid to save many charts on disk before reading them again, wouldn't it?)

Answer in short:

it is possible to give an Outputstream instead of a path string as "FILE_IDENTIFIER" (works with both "dv.SVG" and "dv.PNG")

dermoritz
  • 12,519
  • 25
  • 97
  • 185

3 Answers3

1

I don't know BIRT that much, but what you can do is set the renderer to render the image in a BufferedImage and get the bytes out of it.

dRenderer = ce.getRenderer("dv.PNG");

BufferedImage img = new BufferedImage(size1, size2, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D)img.getGraphics();
dRenderer.setProperty(IDeviceRenderer.GRAPHICS_CONTEXT, g2d);
dRenderer.setProperty(IDeviceRenderer.CACHED_IMAGE, img);

// render

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "png", baos);
baos.flush();
byte[] imgBytes = baos.toByteArray();

I don't think it will work for SVG though. But there seems to be a newer API to generate reports with BIRT, where you can passe Rendering Options to the renderer, especially an option to set an OutputStream, you will give it a ByteArrayOutputStream and there you go, you can serve it to your web client.

Alex
  • 25,147
  • 6
  • 59
  • 55
  • thx the png part is solved. but i cant find something to get svg as an object (String or something else)? I can't find anything in "IDeviceRenderer" - do you have further tips? – dermoritz Sep 07 '12 at 11:03
1

If you are using SVG set the file identifier to the output stream and make sure to set the content type(response.setContentType( "image/svg+xml" );

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

        Generator gr = Generator.instance( );
        GeneratedChartState gcs = null;
        Bounds bo = BoundsImpl.create( 0, 0, 600, 400 );
        gcs = gr.build( idr.getDisplayServer( ), chart, bo, null, rtc, null );

        idr.setProperty( IDeviceRenderer.FILE_IDENTIFIER, out ); 
        idr.setProperty(
                IDeviceRenderer.UPDATE_NOTIFIER,
                new EmptyUpdateNotifier( chart, gcs.getChartModel( ) ) );

        gr.render( idr, gcs );
    }

See this link

athspk
  • 6,722
  • 7
  • 37
  • 51
Jason Weathersby
  • 1,071
  • 5
  • 5
  • thx for complete example on eclipse forum. to give a outputstream as "FILE_IDENTIFIER" is simple but not obvious ;-). – dermoritz Sep 10 '12 at 06:56
0

If you want to return your PNG directly to a browser, you need some kind of server-side machinery. Here you can see how to do it with a REST service

Community
  • 1
  • 1
MarcoS
  • 13,386
  • 7
  • 42
  • 63