Home » Archived » BIRT » how to render charts to stream or object (png, svg)
how to render charts to stream or object (png, svg) [message #909496] |
Fri, 07 September 2012 08:40 |
moritz du Messages: 102 Registered: February 2010 |
Senior Member |
|
|
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)
[Updated on: Fri, 07 September 2012 14:18] Report message to a moderator
|
|
| |
Re: how to render charts to stream (png, svg) [message #909746 is a reply to message #909597] |
Fri, 07 September 2012 17:35 |
|
I posted a reply to the stackoverflow question. Here is the complete example:
package org.eclipse.chart.birt.servlet.example;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.eclipse.birt.chart.api.ChartEngine;
import org.eclipse.birt.chart.device.EmptyUpdateNotifier;
import org.eclipse.birt.chart.device.IDeviceRenderer;
import org.eclipse.birt.chart.exception.ChartException;
import org.eclipse.birt.chart.factory.GeneratedChartState;
import org.eclipse.birt.chart.factory.Generator;
import org.eclipse.birt.chart.factory.IGenerator;
import org.eclipse.birt.chart.factory.RunTimeContext;
import org.eclipse.birt.chart.model.Chart;
import org.eclipse.birt.chart.model.ChartWithAxes;
import org.eclipse.birt.chart.model.attribute.ActionType;
import org.eclipse.birt.chart.model.attribute.AxisType;
import org.eclipse.birt.chart.model.attribute.Bounds;
import org.eclipse.birt.chart.model.attribute.IntersectionType;
import org.eclipse.birt.chart.model.attribute.LegendItemType;
import org.eclipse.birt.chart.model.attribute.Position;
import org.eclipse.birt.chart.model.attribute.TickStyle;
import org.eclipse.birt.chart.model.attribute.TriggerCondition;
import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
import org.eclipse.birt.chart.model.attribute.impl.TooltipValueImpl;
import org.eclipse.birt.chart.model.attribute.impl.URLValueImpl;
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.BaseSampleData;
import org.eclipse.birt.chart.model.data.DataFactory;
import org.eclipse.birt.chart.model.data.NumberDataSet;
import org.eclipse.birt.chart.model.data.OrthogonalSampleData;
import org.eclipse.birt.chart.model.data.SampleData;
import org.eclipse.birt.chart.model.data.SeriesDefinition;
import org.eclipse.birt.chart.model.data.TextDataSet;
import org.eclipse.birt.chart.model.data.Trigger;
import org.eclipse.birt.chart.model.data.impl.ActionImpl;
import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
import org.eclipse.birt.chart.model.data.impl.TextDataSetImpl;
import org.eclipse.birt.chart.model.data.impl.TriggerImpl;
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.PlatformConfig;
import com.ibm.icu.util.ULocale;
/**
* Servlet implementation class DynamicContentCreation
*/
public class ChartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private IDeviceRenderer idr = null;
private ChartEngine ce = null;
private Chart chart = null;
public ChartServlet( )
{
PlatformConfig pf = new PlatformConfig();
pf.setProperty("STANDALONE", true);
ce = ChartEngine.instance( pf);
try
{
//idr = ce.getRenderer( "dv.SWING" );
idr = ce.getRenderer( "dv.SVG" );
}
catch ( ChartException ex )
{
ex.printStackTrace( );
}
}
public void init() throws ServletException {
}
public void destroy() {
super.destroy();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
chart = ChartServlet.createStackedBar();
// Set the Content-Type header for the image output
response.setHeader( "Cache-Control", "no-store" ); //$NON-NLS-1$//$NON-NLS-2$
response.setDateHeader( "Expires", 0 ); //$NON-NLS-1$
//response.setContentType( "image/png" );
response.setContentType( "image/svg+xml" );
try {
// Render
//createImage((OutputStream) response.getOutputStream());
//SVG
createSVGImage((OutputStream) response.getOutputStream());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(request, response);
}
private void createImage( OutputStream out )
{
// Create a buffered image
BufferedImage image = new BufferedImage( 600,
400,
BufferedImage.TYPE_INT_RGB );
// Draw the chart in the buffered image
Graphics2D g2d = (Graphics2D) image.getGraphics( );
idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, g2d );
Bounds bo = BoundsImpl.create( 0, 0, 600, 400 );
// convert pixels to points (1 inch = 72 points = dpi pixels )
// 1 pixel = 72/dpi points
bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) );
IGenerator gr = ce.getGenerator();
try
{
GeneratedChartState gcs =
gr.build( idr.getDisplayServer( ),
chart,
bo,
null,
null,
null );
gr.render( idr, gcs ); // Style processor
}
catch ( ChartException ex )
{
ex.printStackTrace( );
}
g2d.dispose( );
// Output the image
try
{
ImageIO.write( image, "PNG", out ); //$NON-NLS-1$
}
catch ( IOException io )
{
io.printStackTrace( );
}
}
private void createSVGImage( OutputStream out )
{
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 );
}
catch ( ChartException io )
{
io.printStackTrace( );
}
}
public static final Chart createStackedBar()
{
ChartWithAxes cwaBar = ChartWithAxesImpl.create();
cwaBar.setType("Bar Chart"); //$NON-NLS-1$
cwaBar.setSubType("Stacked"); //$NON-NLS-1$
// Plot
cwaBar.getBlock().setBackground(ColorDefinitionImpl.WHITE());
cwaBar.getBlock().getOutline().setVisible(true);
Plot p = cwaBar.getPlot();
p.getClientArea().setBackground(ColorDefinitionImpl.create(255,
255,
225));
// Title
cwaBar.getTitle()
.getLabel()
.getCaption()
.setValue("Stacked Bar Chart"); //$NON-NLS-1$
// Legend
Legend lg = cwaBar.getLegend();
lg.setItemType(LegendItemType.CATEGORIES_LITERAL);
//Add Script
cwaBar
.setScript("function beforeGeneration( cm, icsc )"
+ "{importPackage(Packages.org.eclipse.birt.chart.model.attribute); "
+ " importPackage(Packages.org.eclipse.birt.chart.model.attribute.impl); "
+ " cm.getLegend().getOutline( ).setStyle( LineStyle.DASH_DOTTED_LITERAL );"
+ " cm.getLegend().getOutline( ).setColor( ColorDefinitionImpl.GREEN() );"
+ " cm.getLegend().getOutline( ).setVisible( true );} ");
// X-Axis
Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes()[0];
xAxisPrimary.setType(AxisType.TEXT_LITERAL);
xAxisPrimary.getMajorGrid().setTickStyle(TickStyle.BELOW_LITERAL);
xAxisPrimary.getOrigin().setType(IntersectionType.MIN_LITERAL);
// Y-Axis
Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis(xAxisPrimary);
yAxisPrimary.getMajorGrid().setTickStyle(TickStyle.LEFT_LITERAL);
yAxisPrimary.setType(AxisType.LINEAR_LITERAL);
yAxisPrimary.setLabelPosition(Position.RIGHT_LITERAL);
yAxisPrimary.getLabel().getCaption().getFont().setRotation(45);
// Data Set
TextDataSet categoryValues = TextDataSetImpl.create(new String[] {
"Item 1a", "Item 2", "Item 3", "Item 4", "Item 5" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
NumberDataSet orthoValues1 = NumberDataSetImpl.create(new double[] {
25, 35, 15, 5, 20
});
NumberDataSet orthoValues2 = NumberDataSetImpl.create(new double[] {
5, 10, 25, 10, 5
});
SampleData sd = DataFactory.eINSTANCE.createSampleData();
BaseSampleData sdBase = DataFactory.eINSTANCE.createBaseSampleData();
sdBase.setDataSetRepresentation("");//$NON-NLS-1$
sd.getBaseSampleData().add(sdBase);
OrthogonalSampleData sdOrthogonal1 = DataFactory.eINSTANCE
.createOrthogonalSampleData();
sdOrthogonal1.setDataSetRepresentation("");//$NON-NLS-1$
sdOrthogonal1.setSeriesDefinitionIndex(0);
sd.getOrthogonalSampleData().add(sdOrthogonal1);
OrthogonalSampleData sdOrthogonal2 = DataFactory.eINSTANCE
.createOrthogonalSampleData();
sdOrthogonal2.setDataSetRepresentation("");//$NON-NLS-1$
sdOrthogonal2.setSeriesDefinitionIndex(1);
sd.getOrthogonalSampleData().add(sdOrthogonal2);
cwaBar.setSampleData(sd);
// X-Series
Series seCategory = SeriesImpl.create();
seCategory.setDataSet(categoryValues);
SeriesDefinition sdX = SeriesDefinitionImpl.create();
xAxisPrimary.getSeriesDefinitions().add(sdX);
sdX.getSeries().add(seCategory);
sdX.getSeriesPalette().shift(0);
// Y-Series
BarSeries bs1 = (BarSeries) BarSeriesImpl.create();
bs1.setDataSet(orthoValues1);
bs1.setStacked(true);
bs1.getLabel().setVisible(true);
bs1.setLabelPosition(Position.INSIDE_LITERAL);
bs1.setRiserOutline(ColorDefinitionImpl.TRANSPARENT());
BarSeries bs2 = (BarSeries) BarSeriesImpl.create();
bs2.setDataSet(orthoValues2);
bs2.setStacked(true);
bs2.setRiserOutline(ColorDefinitionImpl.TRANSPARENT());
bs2.getLabel().setVisible(true);
bs2.setLabelPosition(Position.INSIDE_LITERAL);
Trigger tr2 = TriggerImpl.create(TriggerCondition.ONCLICK_LITERAL,
ActionImpl.create(ActionType.URL_REDIRECT_LITERAL, URLValueImpl
.create("https://www.google.com", null, "component",
"value", "")));
bs1.getTriggers().add(tr2);
//bs2.getTriggers().add(tr3);
//bs2.getTriggers().add(tr4);
SeriesDefinition sdY = SeriesDefinitionImpl.create();
sdY.getSeriesPalette().shift(0);
yAxisPrimary.getSeriesDefinitions().add(sdY);
sdY.getSeries().add(bs1);
sdY.getSeries().add(bs2);
return cwaBar;
}
}
Jason
|
|
| | |
Re: how to render charts to stream (png, svg) [message #910960 is a reply to message #910665] |
Mon, 10 September 2012 17:56 |
|
Yes you can change the renderer, like
idr = ce.getRenderer( "dv.PNG" );
//idr = ce.getRenderer( "dv.SVG" );
Make sure to change the mime type as well.
response.setContentType( "image/png" );
//response.setContentType( "image/svg+xml" );
Yes it would be good to have this in the faq and the JavaDocs. Can you open a bugzilla entry for this?
Another thing that would be nice is to change the setting from a file identifier to an output stream indentifier, which is technically what it is.
Jason
|
|
| | |
Goto Forum:
Current Time: Sat Sep 21 22:13:57 GMT 2024
Powered by FUDForum. Page generated in 0.03430 seconds
|