Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » BIRT » Cannot render chart to image - possibly a bug
Cannot render chart to image - possibly a bug [message #759715] Tue, 29 November 2011 18:00 Go to next message
Anton is currently offline AntonFriend
Messages: 23
Registered: July 2009
Junior Member
I use birt 2.6.1. I use the example on this page:

http://www.birt-exchange.org/org/wiki/index.php?title=BIRT:Charts2.2

to render a chart to an image but the bottom of the image is missing. Here
is the code:



// DEFINE A CHART STRUCTURE
Chart cm = SampleCharts.createMyChart();
// OBTAIN A PNG IMAGE DEVICE RENDERER
PluginSettings ps = PluginSettings.instance();
IDeviceRenderer idr = null;
try {
idr = ps.getDevice("dv.PNG");
} catch (ChartException pex)
{
DefaultLoggerImpl.instance().log(pex);
}

// DEFINE THE BOUNDS IN POINTS
Bounds bo = BoundsImpl.create(0, 0, 800, 600); // IN POINTS
// BUILD THE RENDERED CHART STRUCTURE
Generator gr = Generator.instance();
GeneratedChartState gcs = null;
try {
gcs = gr.build( idr.getDisplayServer(),cm, null,bo, null);
} catch (ChartException gex)
{

DefaultLoggerImpl.instance().log(gex);
System.exit(0);
}
// RENDER THE CHART TO A PNG FILE
BufferedImage img = new BufferedImage((int) bo.getWidth(), (int)
bo.getHeight(), BufferedImage.TYPE_INT_ARGB);
idr.setProperty(IDeviceRenderer.CACHED_IMAGE, img);
idr.setProperty(IDeviceRenderer.FILE_IDENTIFIER, "chart.png");
try {
gr.render(idr, gcs);
} catch (ChartException rex)
{
DefaultLoggerImpl.instance().log(rex);
}


ImageIcon icon = new ImageIcon(img);
JLabel label = new JLabel(icon, JLabel.CENTER);
JOptionPane.showMessageDialog(null, label, "icon", -1);


The image that is written to disk looks fine though. Its has been up before
and it seems to be a bug:

http://www.eclipse.org/forums/index.php/m/94425/

but I cannot find the bug on the birt bugzilla.

Is this still a bug or has it been fixed in versions > 2.6.1?
Re: Cannot render chart to image - possibly a bug [message #759968 is a reply to message #759715] Wed, 30 November 2011 18:32 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Have you tried the swing viewer examples from the examples view?
Take a look at the attached files.

Jason

On 11/29/2011 1:00 PM, Anton wrote:
> I use birt 2.6.1. I use the example on this page:
>
> http://www.birt-exchange.org/org/wiki/index.php?title=BIRT:Charts2.2
>
> to render a chart to an image but the bottom of the image is missing.
> Here is the code:
>
>
>
> // DEFINE A CHART STRUCTURE
> Chart cm = SampleCharts.createMyChart();
> // OBTAIN A PNG IMAGE DEVICE RENDERER
> PluginSettings ps = PluginSettings.instance();
> IDeviceRenderer idr = null;
> try {
> idr = ps.getDevice("dv.PNG");
> } catch (ChartException pex)
> {
> DefaultLoggerImpl.instance().log(pex);
> }
>
> // DEFINE THE BOUNDS IN POINTS
> Bounds bo = BoundsImpl.create(0, 0, 800, 600); // IN POINTS
> // BUILD THE RENDERED CHART STRUCTURE
> Generator gr = Generator.instance();
> GeneratedChartState gcs = null;
> try {
> gcs = gr.build( idr.getDisplayServer(),cm, null,bo, null);
> } catch (ChartException gex)
> {
>
> DefaultLoggerImpl.instance().log(gex);
> System.exit(0);
> }
> // RENDER THE CHART TO A PNG FILE
> BufferedImage img = new BufferedImage((int) bo.getWidth(), (int)
> bo.getHeight(), BufferedImage.TYPE_INT_ARGB);
> idr.setProperty(IDeviceRenderer.CACHED_IMAGE, img);
> idr.setProperty(IDeviceRenderer.FILE_IDENTIFIER, "chart.png");
> try {
> gr.render(idr, gcs);
> } catch (ChartException rex)
> {
> DefaultLoggerImpl.instance().log(rex);
> }
>
>
> ImageIcon icon = new ImageIcon(img);
> JLabel label = new JLabel(icon, JLabel.CENTER);
> JOptionPane.showMessageDialog(null, label, "icon", -1);
>
>
> The image that is written to disk looks fine though. Its has been up
> before and it seems to be a bug:
>
> http://www.eclipse.org/forums/index.php/m/94425/
>
> but I cannot find the bug on the birt bugzilla.
>
> Is this still a bug or has it been fixed in versions > 2.6.1?
Re: Cannot render chart to image - possibly a bug [message #759969 is a reply to message #759968] Wed, 30 November 2011 18:34 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

BTW if you could build a small swing app that shows the issue it could
help us debug it.

Jason

On 11/30/2011 1:32 PM, Jason Weathersby wrote:
> Have you tried the swing viewer examples from the examples view?
> Take a look at the attached files.
>
> Jason
>
> On 11/29/2011 1:00 PM, Anton wrote:
>> I use birt 2.6.1. I use the example on this page:
>>
>> http://www.birt-exchange.org/org/wiki/index.php?title=BIRT:Charts2.2
>>
>> to render a chart to an image but the bottom of the image is missing.
>> Here is the code:
>>
>>
>>
>> // DEFINE A CHART STRUCTURE
>> Chart cm = SampleCharts.createMyChart();
>> // OBTAIN A PNG IMAGE DEVICE RENDERER
>> PluginSettings ps = PluginSettings.instance();
>> IDeviceRenderer idr = null;
>> try {
>> idr = ps.getDevice("dv.PNG");
>> } catch (ChartException pex)
>> {
>> DefaultLoggerImpl.instance().log(pex);
>> }
>>
>> // DEFINE THE BOUNDS IN POINTS
>> Bounds bo = BoundsImpl.create(0, 0, 800, 600); // IN POINTS
>> // BUILD THE RENDERED CHART STRUCTURE
>> Generator gr = Generator.instance();
>> GeneratedChartState gcs = null;
>> try {
>> gcs = gr.build( idr.getDisplayServer(),cm, null,bo, null);
>> } catch (ChartException gex)
>> {
>>
>> DefaultLoggerImpl.instance().log(gex);
>> System.exit(0);
>> }
>> // RENDER THE CHART TO A PNG FILE
>> BufferedImage img = new BufferedImage((int) bo.getWidth(), (int)
>> bo.getHeight(), BufferedImage.TYPE_INT_ARGB);
>> idr.setProperty(IDeviceRenderer.CACHED_IMAGE, img);
>> idr.setProperty(IDeviceRenderer.FILE_IDENTIFIER, "chart.png");
>> try {
>> gr.render(idr, gcs);
>> } catch (ChartException rex)
>> {
>> DefaultLoggerImpl.instance().log(rex);
>> }
>>
>>
>> ImageIcon icon = new ImageIcon(img);
>> JLabel label = new JLabel(icon, JLabel.CENTER);
>> JOptionPane.showMessageDialog(null, label, "icon", -1);
>>
>>
>> The image that is written to disk looks fine though. Its has been up
>> before and it seems to be a bug:
>>
>> http://www.eclipse.org/forums/index.php/m/94425/
>>
>> but I cannot find the bug on the birt bugzilla.
>>
>> Is this still a bug or has it been fixed in versions > 2.6.1?
>
Re: Cannot render chart to image - possibly a bug [message #760061 is a reply to message #759969] Thu, 01 December 2011 06:47 Go to previous messageGo to next message
Anton is currently offline AntonFriend
Messages: 23
Registered: July 2009
Junior Member
Rendering to swing works fine. The problem is when I render the chart to an
image:

ps = PluginSettings.instance();
double width = chart.getBlock().getBounds().getWidth();
double height = chart.getBlock().getBounds().getHeight();
Bounds bo = BoundsImpl.create(0, 0, width, height);
Generator gr = Generator.instance();
BufferedImage img = null;
try {
idr = ps.getDevice("dv.PNG");
img = new BufferedImage((int) bo.getWidth(), (int) bo.getHeight(),
BufferedImage.TYPE_INT_ARGB);
idr.setProperty(IDeviceRenderer.CACHED_IMAGE, img);
idr.setProperty(IDeviceRenderer.EXPECTED_BOUNDS, bo);
gcs = gr.build(idr.getDisplayServer(), chart, null, bo, null);
gr.render(idr, gcs);
} catch (ChartException pex) {
DefaultLoggerImpl.instance().log(pex);
}
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon, JLabel.CENTER);
JOptionPane.showMessageDialog(null, label, "icon", -1);



If I render to png file it works fine:

ps = PluginSettings.instance();
double width = chart.getBlock().getBounds().getWidth();
double height = chart.getBlock().getBounds().getHeight();
Bounds bo = BoundsImpl.create(0, 0, width, height);
Generator gr = Generator.instance();
BufferedImage img = null;
try {
idr = ps.getDevice("dv.PNG");
idr.setProperty(IDeviceRenderer.FILE_IDENTIFIER, out.getAbsolutePath());
gcs = gr.build(idr.getDisplayServer(), chart, null, bo, null);
gr.render(idr, gcs);
} catch (ChartException pex) {
DefaultLoggerImpl.instance().log(pex);
}






"Jason Weathersby" wrote in message news:jb5sv1$6fg$2@news.eclipse.org...

BTW if you could build a small swing app that shows the issue it could
help us debug it.

Jason

On 11/30/2011 1:32 PM, Jason Weathersby wrote:
> Have you tried the swing viewer examples from the examples view?
> Take a look at the attached files.
>
> Jason
>
> On 11/29/2011 1:00 PM, Anton wrote:
>> I use birt 2.6.1. I use the example on this page:
>>
>> http://www.birt-exchange.org/org/wiki/index.php?title=BIRT:Charts2.2
>>
>> to render a chart to an image but the bottom of the image is missing.
>> Here is the code:
>>
>>
>>
>> // DEFINE A CHART STRUCTURE
>> Chart cm = SampleCharts.createMyChart();
>> // OBTAIN A PNG IMAGE DEVICE RENDERER
>> PluginSettings ps = PluginSettings.instance();
>> IDeviceRenderer idr = null;
>> try {
>> idr = ps.getDevice("dv.PNG");
>> } catch (ChartException pex)
>> {
>> DefaultLoggerImpl.instance().log(pex);
>> }
>>
>> // DEFINE THE BOUNDS IN POINTS
>> Bounds bo = BoundsImpl.create(0, 0, 800, 600); // IN POINTS
>> // BUILD THE RENDERED CHART STRUCTURE
>> Generator gr = Generator.instance();
>> GeneratedChartState gcs = null;
>> try {
>> gcs = gr.build( idr.getDisplayServer(),cm, null,bo, null);
>> } catch (ChartException gex)
>> {
>>
>> DefaultLoggerImpl.instance().log(gex);
>> System.exit(0);
>> }
>> // RENDER THE CHART TO A PNG FILE
>> BufferedImage img = new BufferedImage((int) bo.getWidth(), (int)
>> bo.getHeight(), BufferedImage.TYPE_INT_ARGB);
>> idr.setProperty(IDeviceRenderer.CACHED_IMAGE, img);
>> idr.setProperty(IDeviceRenderer.FILE_IDENTIFIER, "chart.png");
>> try {
>> gr.render(idr, gcs);
>> } catch (ChartException rex)
>> {
>> DefaultLoggerImpl.instance().log(rex);
>> }
>>
>>
>> ImageIcon icon = new ImageIcon(img);
>> JLabel label = new JLabel(icon, JLabel.CENTER);
>> JOptionPane.showMessageDialog(null, label, "icon", -1);
>>
>>
>> The image that is written to disk looks fine though. Its has been up
>> before and it seems to be a bug:
>>
>> http://www.eclipse.org/forums/index.php/m/94425/
>>
>> but I cannot find the bug on the birt bugzilla.
>>
>> Is this still a bug or has it been fixed in versions > 2.6.1?
>
Re: Cannot render chart to image - possibly a bug [message #760220 is a reply to message #760061] Thu, 01 December 2011 16:01 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Can you try the example below?



import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import org.eclipse.birt.chart.api.ChartEngine;
import org.eclipse.birt.chart.device.IDeviceRenderer;
import org.eclipse.birt.chart.device.IDisplayServer;
import org.eclipse.birt.chart.exception.ChartException;
import org.eclipse.birt.chart.factory.GeneratedChartState;
import org.eclipse.birt.chart.factory.IGenerator;
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.Platform;
import org.eclipse.birt.core.framework.PlatformConfig;
import org.eclipse.emf.ecore.util.EcoreUtil;

/**
* Test decription:
* </p>
* Chart script: BeforeDrawLegendItem()
* </p>
*/

public class StandaloneChartOSGi
{

private static String OUTPUT = "output/StandaloneOSGi.png"; //$NON-NLS-1$

/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 1L;

/**
* A chart model instance
*/
private Chart cm = null;

/**
* The swing rendering device
*/
private IDeviceRenderer dRenderer = null;
private IDisplayServer dServer = null;


private GeneratedChartState gcs = null;

/**
* execute application
*
* @param args
*/
public static void main( String[] args )
{
new StandaloneChartOSGi( );
System.out.println("Finished");
}

/**
* Constructor
*/
public StandaloneChartOSGi( )
{
//The deployment runtime libs can be used to build the code
//if using osgi, must have valid plugins directory
PlatformConfig pf = new PlatformConfig();

pf.setBIRTHome("C:/birt/birt-runtime-2_6_1/birt-runtime-2_6_1/ReportEngine");


//Returns a singleton instance of the Chart Engine
ChartEngine ce = ChartEngine.instance(pf);
//Returns a singleton instance of the Generator
IGenerator gr = ce.getGenerator();

try
{
dRenderer = ce.getRenderer("dv.PNG");
dServer =dRenderer.getDisplayServer( );
}
catch( Exception ex)
{
ex.printStackTrace();
}

cm = StandaloneChartOSGi.createStackedBar();

BufferedImage img = new BufferedImage(
600,
600,
BufferedImage.TYPE_INT_ARGB );
Graphics g = img.getGraphics( );

Graphics2D g2d = (Graphics2D) g;
dRenderer.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, g2d );
//dRenderer.setProperty( IDeviceRenderer.FILE_IDENTIFIER, OUTPUT );
//$NON-NLS-1$
dRenderer.setProperty(IDeviceRenderer.CACHED_IMAGE, img);
Bounds bo = BoundsImpl.create( 0, 0, 600, 600 );
bo.scale( 72d / dRenderer.getDisplayServer( ).getDpiResolution( ) );



try
{
gcs = gr.build(
dServer,
cm,
bo,
null,
null,
null );
gr.render( dRenderer, gcs );
}
catch ( ChartException e )
{
// TODO Auto-generated catch block
e.printStackTrace( );
}
ImageIcon icon = new ImageIcon(img);
JLabel label = new JLabel(icon, JLabel.CENTER);
JOptionPane.showMessageDialog(null, label, "icon", -1);

//Only shutdown OSGi if started
Platform.shutdown();
}
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.SERIES_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 1", "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);

// Y-Series

BarSeries bs1 = (BarSeries) BarSeriesImpl.create();

bs1.setDataSet(orthoValues1);

bs1.setStacked(true);

bs1.getLabel().setVisible(true);

bs1.setLabelPosition(Position.INSIDE_LITERAL);

BarSeries bs2 = (BarSeries) BarSeriesImpl.create();

bs2.setDataSet(orthoValues2);

bs2.setStacked(true);

bs2.getLabel().setVisible(true);

bs2.setLabelPosition(Position.INSIDE_LITERAL);

Trigger tr1 = TriggerImpl.create(TriggerCondition.ONMOUSEOVER_LITERAL,
ActionImpl.create(ActionType.SHOW_TOOLTIP_LITERAL,
TooltipValueImpl.create(200, null)));
Trigger tr2 = TriggerImpl.create(TriggerCondition.ONCLICK_LITERAL,
ActionImpl.create(ActionType.URL_REDIRECT_LITERAL, URLValueImpl
.create("https://www.google.com", null, "component",
"value", "")));
//Trigger tr3 =
TriggerImpl.create(TriggerCondition.ONMOUSEOVER_LITERAL,
ActionImpl.create(ActionType.SHOW_TOOLTIP_LITERAL,
TooltipValueImpl.create(200, null)));

Trigger tr3 = (Trigger) EcoreUtil.copy(tr1);
Trigger tr4 = (Trigger) EcoreUtil.copy(tr2);
bs1.getTriggers().add(tr1);
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


On 12/1/2011 1:47 AM, Anton wrote:
> Rendering to swing works fine. The problem is when I render the chart to
> an image:
>
> ps = PluginSettings.instance();
> double width = chart.getBlock().getBounds().getWidth();
> double height = chart.getBlock().getBounds().getHeight();
> Bounds bo = BoundsImpl.create(0, 0, width, height);
> Generator gr = Generator.instance();
> BufferedImage img = null;
> try {
> idr = ps.getDevice("dv.PNG");
> img = new BufferedImage((int) bo.getWidth(), (int) bo.getHeight(),
> BufferedImage.TYPE_INT_ARGB);
> idr.setProperty(IDeviceRenderer.CACHED_IMAGE, img);
> idr.setProperty(IDeviceRenderer.EXPECTED_BOUNDS, bo);
> gcs = gr.build(idr.getDisplayServer(), chart, null, bo, null);
> gr.render(idr, gcs);
> } catch (ChartException pex) {
> DefaultLoggerImpl.instance().log(pex);
> }
> ImageIcon icon = new ImageIcon(image);
> JLabel label = new JLabel(icon, JLabel.CENTER);
> JOptionPane.showMessageDialog(null, label, "icon", -1);
>
>
>
> If I render to png file it works fine:
>
> ps = PluginSettings.instance();
> double width = chart.getBlock().getBounds().getWidth();
> double height = chart.getBlock().getBounds().getHeight();
> Bounds bo = BoundsImpl.create(0, 0, width, height);
> Generator gr = Generator.instance();
> BufferedImage img = null;
> try {
> idr = ps.getDevice("dv.PNG");
> idr.setProperty(IDeviceRenderer.FILE_IDENTIFIER, out.getAbsolutePath());
> gcs = gr.build(idr.getDisplayServer(), chart, null, bo, null);
> gr.render(idr, gcs);
> } catch (ChartException pex) {
> DefaultLoggerImpl.instance().log(pex);
> }
>
>
>
>
>
>
> "Jason Weathersby" wrote in message news:jb5sv1$6fg$2@news.eclipse.org...
>
> BTW if you could build a small swing app that shows the issue it could
> help us debug it.
>
> Jason
>
> On 11/30/2011 1:32 PM, Jason Weathersby wrote:
>> Have you tried the swing viewer examples from the examples view?
>> Take a look at the attached files.
>>
>> Jason
>>
>> On 11/29/2011 1:00 PM, Anton wrote:
>>> I use birt 2.6.1. I use the example on this page:
>>>
>>> http://www.birt-exchange.org/org/wiki/index.php?title=BIRT:Charts2.2
>>>
>>> to render a chart to an image but the bottom of the image is missing.
>>> Here is the code:
>>>
>>>
>>>
>>> // DEFINE A CHART STRUCTURE
>>> Chart cm = SampleCharts.createMyChart();
>>> // OBTAIN A PNG IMAGE DEVICE RENDERER
>>> PluginSettings ps = PluginSettings.instance();
>>> IDeviceRenderer idr = null;
>>> try {
>>> idr = ps.getDevice("dv.PNG");
>>> } catch (ChartException pex)
>>> {
>>> DefaultLoggerImpl.instance().log(pex);
>>> }
>>>
>>> // DEFINE THE BOUNDS IN POINTS
>>> Bounds bo = BoundsImpl.create(0, 0, 800, 600); // IN POINTS
>>> // BUILD THE RENDERED CHART STRUCTURE
>>> Generator gr = Generator.instance();
>>> GeneratedChartState gcs = null;
>>> try {
>>> gcs = gr.build( idr.getDisplayServer(),cm, null,bo, null);
>>> } catch (ChartException gex)
>>> {
>>>
>>> DefaultLoggerImpl.instance().log(gex);
>>> System.exit(0);
>>> }
>>> // RENDER THE CHART TO A PNG FILE
>>> BufferedImage img = new BufferedImage((int) bo.getWidth(), (int)
>>> bo.getHeight(), BufferedImage.TYPE_INT_ARGB);
>>> idr.setProperty(IDeviceRenderer.CACHED_IMAGE, img);
>>> idr.setProperty(IDeviceRenderer.FILE_IDENTIFIER, "chart.png");
>>> try {
>>> gr.render(idr, gcs);
>>> } catch (ChartException rex)
>>> {
>>> DefaultLoggerImpl.instance().log(rex);
>>> }
>>>
>>>
>>> ImageIcon icon = new ImageIcon(img);
>>> JLabel label = new JLabel(icon, JLabel.CENTER);
>>> JOptionPane.showMessageDialog(null, label, "icon", -1);
>>>
>>>
>>> The image that is written to disk looks fine though. Its has been up
>>> before and it seems to be a bug:
>>>
>>> http://www.eclipse.org/forums/index.php/m/94425/
>>>
>>> but I cannot find the bug on the birt bugzilla.
>>>
>>> Is this still a bug or has it been fixed in versions > 2.6.1?
>>
>
Re: Cannot render chart to image - possibly a bug [message #760328 is a reply to message #760220] Thu, 01 December 2011 21:52 Go to previous message
Anton is currently offline AntonFriend
Messages: 23
Registered: July 2009
Junior Member
Thank you very much I have now made it work using your example, I am not
quite sure how though :-)



"Jason Weathersby" wrote in message news:jb88c8$ps1$1@news.eclipse.org...

Can you try the example below?



import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import org.eclipse.birt.chart.api.ChartEngine;
import org.eclipse.birt.chart.device.IDeviceRenderer;
import org.eclipse.birt.chart.device.IDisplayServer;
import org.eclipse.birt.chart.exception.ChartException;
import org.eclipse.birt.chart.factory.GeneratedChartState;
import org.eclipse.birt.chart.factory.IGenerator;
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.Platform;
import org.eclipse.birt.core.framework.PlatformConfig;
import org.eclipse.emf.ecore.util.EcoreUtil;

/**
* Test decription:
* </p>
* Chart script: BeforeDrawLegendItem()
* </p>
*/

public class StandaloneChartOSGi
{

private static String OUTPUT = "output/StandaloneOSGi.png"; //$NON-NLS-1$

/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 1L;

/**
* A chart model instance
*/
private Chart cm = null;

/**
* The swing rendering device
*/
private IDeviceRenderer dRenderer = null;
private IDisplayServer dServer = null;


private GeneratedChartState gcs = null;

/**
* execute application
*
* @param args
*/
public static void main( String[] args )
{
new StandaloneChartOSGi( );
System.out.println("Finished");
}

/**
* Constructor
*/
public StandaloneChartOSGi( )
{
//The deployment runtime libs can be used to build the code
//if using osgi, must have valid plugins directory
PlatformConfig pf = new PlatformConfig();

pf.setBIRTHome("C:/birt/birt-runtime-2_6_1/birt-runtime-2_6_1/ReportEngine");


//Returns a singleton instance of the Chart Engine
ChartEngine ce = ChartEngine.instance(pf);
//Returns a singleton instance of the Generator
IGenerator gr = ce.getGenerator();

try
{
dRenderer = ce.getRenderer("dv.PNG");
dServer =dRenderer.getDisplayServer( );
}
catch( Exception ex)
{
ex.printStackTrace();
}

cm = StandaloneChartOSGi.createStackedBar();

BufferedImage img = new BufferedImage(
600,
600,
BufferedImage.TYPE_INT_ARGB );
Graphics g = img.getGraphics( );

Graphics2D g2d = (Graphics2D) g;
dRenderer.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, g2d );
//dRenderer.setProperty( IDeviceRenderer.FILE_IDENTIFIER, OUTPUT );
//$NON-NLS-1$
dRenderer.setProperty(IDeviceRenderer.CACHED_IMAGE, img);
Bounds bo = BoundsImpl.create( 0, 0, 600, 600 );
bo.scale( 72d / dRenderer.getDisplayServer( ).getDpiResolution( ) );



try
{
gcs = gr.build(
dServer,
cm,
bo,
null,
null,
null );
gr.render( dRenderer, gcs );
}
catch ( ChartException e )
{
// TODO Auto-generated catch block
e.printStackTrace( );
}
ImageIcon icon = new ImageIcon(img);
JLabel label = new JLabel(icon, JLabel.CENTER);
JOptionPane.showMessageDialog(null, label, "icon", -1);

//Only shutdown OSGi if started
Platform.shutdown();
}
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.SERIES_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 1", "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);

// Y-Series

BarSeries bs1 = (BarSeries) BarSeriesImpl.create();

bs1.setDataSet(orthoValues1);

bs1.setStacked(true);

bs1.getLabel().setVisible(true);

bs1.setLabelPosition(Position.INSIDE_LITERAL);

BarSeries bs2 = (BarSeries) BarSeriesImpl.create();

bs2.setDataSet(orthoValues2);

bs2.setStacked(true);

bs2.getLabel().setVisible(true);

bs2.setLabelPosition(Position.INSIDE_LITERAL);

Trigger tr1 = TriggerImpl.create(TriggerCondition.ONMOUSEOVER_LITERAL,
ActionImpl.create(ActionType.SHOW_TOOLTIP_LITERAL,
TooltipValueImpl.create(200, null)));
Trigger tr2 = TriggerImpl.create(TriggerCondition.ONCLICK_LITERAL,
ActionImpl.create(ActionType.URL_REDIRECT_LITERAL, URLValueImpl
..create("https://www.google.com", null, "component",
"value", "")));
//Trigger tr3 =
TriggerImpl.create(TriggerCondition.ONMOUSEOVER_LITERAL,
ActionImpl.create(ActionType.SHOW_TOOLTIP_LITERAL,
TooltipValueImpl.create(200, null)));

Trigger tr3 = (Trigger) EcoreUtil.copy(tr1);
Trigger tr4 = (Trigger) EcoreUtil.copy(tr2);
bs1.getTriggers().add(tr1);
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


On 12/1/2011 1:47 AM, Anton wrote:
> Rendering to swing works fine. The problem is when I render the chart to
> an image:
>
> ps = PluginSettings.instance();
> double width = chart.getBlock().getBounds().getWidth();
> double height = chart.getBlock().getBounds().getHeight();
> Bounds bo = BoundsImpl.create(0, 0, width, height);
> Generator gr = Generator.instance();
> BufferedImage img = null;
> try {
> idr = ps.getDevice("dv.PNG");
> img = new BufferedImage((int) bo.getWidth(), (int) bo.getHeight(),
> BufferedImage.TYPE_INT_ARGB);
> idr.setProperty(IDeviceRenderer.CACHED_IMAGE, img);
> idr.setProperty(IDeviceRenderer.EXPECTED_BOUNDS, bo);
> gcs = gr.build(idr.getDisplayServer(), chart, null, bo, null);
> gr.render(idr, gcs);
> } catch (ChartException pex) {
> DefaultLoggerImpl.instance().log(pex);
> }
> ImageIcon icon = new ImageIcon(image);
> JLabel label = new JLabel(icon, JLabel.CENTER);
> JOptionPane.showMessageDialog(null, label, "icon", -1);
>
>
>
> If I render to png file it works fine:
>
> ps = PluginSettings.instance();
> double width = chart.getBlock().getBounds().getWidth();
> double height = chart.getBlock().getBounds().getHeight();
> Bounds bo = BoundsImpl.create(0, 0, width, height);
> Generator gr = Generator.instance();
> BufferedImage img = null;
> try {
> idr = ps.getDevice("dv.PNG");
> idr.setProperty(IDeviceRenderer.FILE_IDENTIFIER, out.getAbsolutePath());
> gcs = gr.build(idr.getDisplayServer(), chart, null, bo, null);
> gr.render(idr, gcs);
> } catch (ChartException pex) {
> DefaultLoggerImpl.instance().log(pex);
> }
>
>
>
>
>
>
> "Jason Weathersby" wrote in message news:jb5sv1$6fg$2@news.eclipse.org...
>
> BTW if you could build a small swing app that shows the issue it could
> help us debug it.
>
> Jason
>
> On 11/30/2011 1:32 PM, Jason Weathersby wrote:
>> Have you tried the swing viewer examples from the examples view?
>> Take a look at the attached files.
>>
>> Jason
>>
>> On 11/29/2011 1:00 PM, Anton wrote:
>>> I use birt 2.6.1. I use the example on this page:
>>>
>>> http://www.birt-exchange.org/org/wiki/index.php?title=BIRT:Charts2.2
>>>
>>> to render a chart to an image but the bottom of the image is missing.
>>> Here is the code:
>>>
>>>
>>>
>>> // DEFINE A CHART STRUCTURE
>>> Chart cm = SampleCharts.createMyChart();
>>> // OBTAIN A PNG IMAGE DEVICE RENDERER
>>> PluginSettings ps = PluginSettings.instance();
>>> IDeviceRenderer idr = null;
>>> try {
>>> idr = ps.getDevice("dv.PNG");
>>> } catch (ChartException pex)
>>> {
>>> DefaultLoggerImpl.instance().log(pex);
>>> }
>>>
>>> // DEFINE THE BOUNDS IN POINTS
>>> Bounds bo = BoundsImpl.create(0, 0, 800, 600); // IN POINTS
>>> // BUILD THE RENDERED CHART STRUCTURE
>>> Generator gr = Generator.instance();
>>> GeneratedChartState gcs = null;
>>> try {
>>> gcs = gr.build( idr.getDisplayServer(),cm, null,bo, null);
>>> } catch (ChartException gex)
>>> {
>>>
>>> DefaultLoggerImpl.instance().log(gex);
>>> System.exit(0);
>>> }
>>> // RENDER THE CHART TO A PNG FILE
>>> BufferedImage img = new BufferedImage((int) bo.getWidth(), (int)
>>> bo.getHeight(), BufferedImage.TYPE_INT_ARGB);
>>> idr.setProperty(IDeviceRenderer.CACHED_IMAGE, img);
>>> idr.setProperty(IDeviceRenderer.FILE_IDENTIFIER, "chart.png");
>>> try {
>>> gr.render(idr, gcs);
>>> } catch (ChartException rex)
>>> {
>>> DefaultLoggerImpl.instance().log(rex);
>>> }
>>>
>>>
>>> ImageIcon icon = new ImageIcon(img);
>>> JLabel label = new JLabel(icon, JLabel.CENTER);
>>> JOptionPane.showMessageDialog(null, label, "icon", -1);
>>>
>>>
>>> The image that is written to disk looks fine though. Its has been up
>>> before and it seems to be a bug:
>>>
>>> http://www.eclipse.org/forums/index.php/m/94425/
>>>
>>> but I cannot find the bug on the birt bugzilla.
>>>
>>> Is this still a bug or has it been fixed in versions > 2.6.1?
>>
>
Previous Topic:java.io.IOException: Content is not allowed in prolog.
Next Topic:Calling BIRT reports from Oracle Apps
Goto Forum:
  


Current Time: Tue Mar 19 07:58:18 GMT 2024

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

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

Back to the top