Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » BIRT » Using the BIRT Chart Engine in a plugin view
Using the BIRT Chart Engine in a plugin view [message #505465] Thu, 31 December 2009 03:16 Go to next message
Eclipse UserFriend
Originally posted by: rodrigo.garcia.kotasoft.com

Hi,

I can paint a pie chart in a view only for painting this chart, but when
I try to include this chart in a formPage I can't see the pie chart.

Any idea?

CODE:

//Create part control of my view

public void createPartControl( Composite parent )
{
parent.setLayout( new GridLayout( ) );
SwtChartViewerSelector scv = new SwtChartViewerSelector( parent,
SWT.NO_BACKGROUND );
scv.setLayoutData( new GridData( GridData.FILL_BOTH ) );
scv.addPaintListener( scv );
}





//painting the chart in the formPage


SwtChartViewerSelector scv = new SwtChartViewerSelector( parent,
SWT.NO_BACKGROUND );
scv.setLayoutData( new GridData( GridData.FILL_BOTH ) );
scv.addPaintListener( scv );



//SwtChartViewerSelector


/*********************************************************** ************
* Copyright (c) 2004, 2007 Actuate Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Actuate Corporation - initial API and implementation
************************************************************ ***********/

package com.kotasoft.wps.ui.view;

import org.eclipse.birt.chart.api.ChartEngine;
import org.eclipse.birt.chart.device.IDeviceRenderer;
import org.eclipse.birt.chart.device.IUpdateNotifier;
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.model.Chart;
import org.eclipse.birt.chart.model.attribute.Bounds;
import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
* The selector of charts in SWT.
*
*/
public final class SwtChartViewerSelector extends Composite
implements
PaintListener,
IUpdateNotifier
{


private IDeviceRenderer idr = null;

private Chart cm = null;

public Chart getCm() {
return cm;
}

public void setCm(Chart cm) {
this.cm = cm;
}

private GeneratedChartState gcs = null;

private boolean bNeedsGeneration = true;

/**
* Get the connection with SWT device to render the graphics.
*/
public SwtChartViewerSelector( Composite parent, int style )
{
super( parent, style );
try
{
idr = ChartEngine.instance( ).getRenderer( "dv.SWT" );//$NON-NLS-1$
}
catch ( ChartException ex )
{
ex.printStackTrace();
}
addControlListener( new ControlListener( ) {

public void controlMoved( ControlEvent e )
{
bNeedsGeneration = true;
}

public void controlResized( ControlEvent e )
{
bNeedsGeneration = true;
}
} );
cm=Pie.createPie();

}

/*
* (non-Javadoc)
*
* @see
org.eclipse.swt.events.PaintListener#paintControl(org.eclips e.swt.events.PaintEvent)
*/
public final void paintControl( PaintEvent e )
{
if (cm == null) {
return;
}
Rectangle d = this.getClientArea( );
Image imgChart = new Image( this.getDisplay( ), d );
GC gcImage = new GC( imgChart );
idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, gcImage );
idr.setProperty( IDeviceRenderer.UPDATE_NOTIFIER, this );

Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height );
bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) );

Generator gr = Generator.instance( );
if ( bNeedsGeneration )
{
bNeedsGeneration = false;
try
{
gcs = gr.build( idr.getDisplayServer( ),
cm,
bo,
null,
null,
null );
}
catch ( ChartException ce )
{
ce.printStackTrace( );
}
}

try
{
gr.render( idr, gcs );
GC gc = e.gc;
gc.drawImage( imgChart, d.x, d.y );
}
catch ( ChartException gex )
{
showException( e.gc, gex );
}
}

private final void showException( GC g2d, Exception ex )
{
String sWrappedException = ex.getClass( ).getName( );
Throwable th = ex;
while ( ex.getCause( ) != null )
{
ex = (Exception) ex.getCause( );
}
String sException = ex.getClass( ).getName( );
if ( sWrappedException.equals( sException ) )
{
sWrappedException = null;
}

String sMessage = null;
if ( th instanceof BirtException )
{
sMessage = ( (BirtException) th ).getLocalizedMessage( );
}
else
{
sMessage = ex.getMessage( );
}

if ( sMessage == null )
{
sMessage = "<null>";//$NON-NLS-1$
}
StackTraceElement[] stea = ex.getStackTrace( );
Point d = this.getSize( );

Device dv = Display.getCurrent( );
Font fo = new Font( dv, "Courier", SWT.BOLD, 16 );//$NON-NLS-1$
g2d.setFont( fo );
FontMetrics fm = g2d.getFontMetrics( );
g2d.setBackground( dv.getSystemColor( SWT.COLOR_WHITE ) );
g2d.fillRectangle( 20, 20, d.x - 40, d.y - 40 );
g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
g2d.drawRectangle( 20, 20, d.x - 40, d.y - 40 );
g2d.setClipping( 20, 20, d.x - 40, d.y - 40 );
int x = 25, y = 20 + fm.getHeight( );
g2d.drawString( "Exception:", x, y );//$NON-NLS-1$
x += g2d.textExtent( "Exception:" ).x + 5;//$NON-NLS-1$
g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) );
g2d.drawString( sException, x, y );
x = 25;
y += fm.getHeight( );
if ( sWrappedException != null )
{
g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
g2d.drawString( "Wrapped In:", x, y );//$NON-NLS-1$
x += g2d.textExtent( "Wrapped In:" ).x + 5;//$NON-NLS-1$
g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) );
g2d.drawString( sWrappedException, x, y );
x = 25;
y += fm.getHeight( );
}
g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
y += 10;
g2d.drawString( "Message:", x, y );//$NON-NLS-1$
x += g2d.textExtent( "Message:" ).x + 5;//$NON-NLS-1$
g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLUE ) );
g2d.drawString( sMessage, x, y );
x = 25;
y += fm.getHeight( );
g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
y += 10;
g2d.drawString( "Trace:", x, y );//$NON-NLS-1$
x = 40;
y += fm.getHeight( );
g2d.setForeground( dv.getSystemColor( SWT.COLOR_DARK_GREEN ) );
for ( int i = 0; i < stea.length; i++ )
{
g2d.drawString( stea[i].getClassName( ) + ":"//$NON-NLS-1$
+ stea[i].getMethodName( ) + "(...):"//$NON-NLS-1$
+ stea[i].getLineNumber( ), x, y );
x = 40;
y += fm.getHeight( );
}
fo.dispose( );
}

/*
* (non-Javadoc)
*
* @see org.eclipse.birt.chart.device.IUpdateNotifier#getDesignTimeM odel()
*/
public Chart getDesignTimeModel( )
{
return cm;
}

/*
* (non-Javadoc)
*
* @see org.eclipse.birt.chart.device.IUpdateNotifier#getRunTimeMode l()
*/
public Chart getRunTimeModel( )
{
return gcs.getChartModel( );
}

/*
* (non-Javadoc)
*
* @see org.eclipse.birt.chart.device.IUpdateNotifier#peerInstance()
*/
public Object peerInstance( )
{
return this;
}

/*
* (non-Javadoc)
*
* @see org.eclipse.birt.chart.device.IUpdateNotifier#regenerateChar t()
*/
public void regenerateChart( )
{
redraw( );
}

/*
* (non-Javadoc)
*
* @see org.eclipse.birt.chart.device.IUpdateNotifier#repaintChart()
*/
public void repaintChart( )
{
redraw( );
}

public boolean isbNeedsGeneration() {
return bNeedsGeneration;
}

public void setbNeedsGeneration(boolean bNeedsGeneration) {
this.bNeedsGeneration = bNeedsGeneration;
}
}


The class Pie is copied from the birt examples plugin.

Thanks in advance.

Best regards and happy new year!
Re: Using the BIRT Chart Engine in a plugin view [message #505733 is a reply to message #505465] Mon, 04 January 2010 17:34 Go to previous message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Rodrigo,

Can you email me the complete source for the project that is including
the chart in the formPage?

jasonweathersby at windstream.net

Jason

Rodrigo García wrote:
> Hi,
>
> I can paint a pie chart in a view only for painting this chart, but when
> I try to include this chart in a formPage I can't see the pie chart.
>
> Any idea?
>
> CODE:
>
> //Create part control of my view
>
> public void createPartControl( Composite parent )
> {
> parent.setLayout( new GridLayout( ) );
> SwtChartViewerSelector scv = new SwtChartViewerSelector( parent,
> SWT.NO_BACKGROUND );
> scv.setLayoutData( new GridData( GridData.FILL_BOTH ) );
> scv.addPaintListener( scv );
> }
>
>
>
>
>
> //painting the chart in the formPage
>
>
> SwtChartViewerSelector scv = new SwtChartViewerSelector( parent,
> SWT.NO_BACKGROUND );
> scv.setLayoutData( new GridData( GridData.FILL_BOTH ) );
> scv.addPaintListener( scv );
>
>
>
> //SwtChartViewerSelector
>
>
> /*********************************************************** ************
> * Copyright (c) 2004, 2007 Actuate Corporation.
> * All rights reserved. This program and the accompanying materials
> * are made available under the terms of the Eclipse Public License v1.0
> * which accompanies this distribution, and is available at
> * http://www.eclipse.org/legal/epl-v10.html
> *
> * Contributors:
> * Actuate Corporation - initial API and implementation
> ************************************************************ ***********/
>
> package com.kotasoft.wps.ui.view;
>
> import org.eclipse.birt.chart.api.ChartEngine;
> import org.eclipse.birt.chart.device.IDeviceRenderer;
> import org.eclipse.birt.chart.device.IUpdateNotifier;
> 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.model.Chart;
> import org.eclipse.birt.chart.model.attribute.Bounds;
> import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
> import org.eclipse.birt.core.exception.BirtException;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.ControlEvent;
> import org.eclipse.swt.events.ControlListener;
> import org.eclipse.swt.events.PaintEvent;
> import org.eclipse.swt.events.PaintListener;
> import org.eclipse.swt.graphics.Device;
> import org.eclipse.swt.graphics.Font;
> import org.eclipse.swt.graphics.FontMetrics;
> import org.eclipse.swt.graphics.GC;
> import org.eclipse.swt.graphics.Image;
> import org.eclipse.swt.graphics.Point;
> import org.eclipse.swt.graphics.Rectangle;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.layout.RowLayout;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Shell;
>
> /**
> * The selector of charts in SWT.
> *
> */
> public final class SwtChartViewerSelector extends Composite
> implements
> PaintListener,
> IUpdateNotifier
> {
>
>
> private IDeviceRenderer idr = null;
>
> private Chart cm = null;
>
> public Chart getCm() {
> return cm;
> }
>
> public void setCm(Chart cm) {
> this.cm = cm;
> }
>
> private GeneratedChartState gcs = null;
>
> private boolean bNeedsGeneration = true;
>
> /**
> * Get the connection with SWT device to render the graphics.
> */
> public SwtChartViewerSelector( Composite parent, int style )
> {
> super( parent, style );
> try
> {
> idr = ChartEngine.instance( ).getRenderer( "dv.SWT"
> );//$NON-NLS-1$
> }
> catch ( ChartException ex )
> {
> ex.printStackTrace();
> }
> addControlListener( new ControlListener( ) {
>
> public void controlMoved( ControlEvent e )
> {
> bNeedsGeneration = true;
> }
>
> public void controlResized( ControlEvent e )
> {
> bNeedsGeneration = true;
> }
> } );
> cm=Pie.createPie();
>
> }
>
> /*
> * (non-Javadoc)
> *
> * @see
> org.eclipse.swt.events.PaintListener#paintControl(org.eclips e.swt.events.PaintEvent)
>
> */
> public final void paintControl( PaintEvent e )
> {
> if (cm == null) {
> return;
> }
> Rectangle d = this.getClientArea( );
> Image imgChart = new Image( this.getDisplay( ), d );
> GC gcImage = new GC( imgChart );
> idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, gcImage );
> idr.setProperty( IDeviceRenderer.UPDATE_NOTIFIER, this );
>
> Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height );
> bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) );
>
> Generator gr = Generator.instance( );
> if ( bNeedsGeneration )
> {
> bNeedsGeneration = false;
> try
> {
> gcs = gr.build( idr.getDisplayServer( ),
> cm,
> bo,
> null,
> null,
> null );
> }
> catch ( ChartException ce )
> {
> ce.printStackTrace( );
> }
> }
>
> try
> {
> gr.render( idr, gcs );
> GC gc = e.gc;
> gc.drawImage( imgChart, d.x, d.y );
> }
> catch ( ChartException gex )
> {
> showException( e.gc, gex );
> }
> }
>
> private final void showException( GC g2d, Exception ex )
> {
> String sWrappedException = ex.getClass( ).getName( );
> Throwable th = ex;
> while ( ex.getCause( ) != null )
> {
> ex = (Exception) ex.getCause( );
> }
> String sException = ex.getClass( ).getName( );
> if ( sWrappedException.equals( sException ) )
> {
> sWrappedException = null;
> }
>
> String sMessage = null;
> if ( th instanceof BirtException )
> {
> sMessage = ( (BirtException) th ).getLocalizedMessage( );
> }
> else
> {
> sMessage = ex.getMessage( );
> }
>
> if ( sMessage == null )
> {
> sMessage = "<null>";//$NON-NLS-1$
> }
> StackTraceElement[] stea = ex.getStackTrace( );
> Point d = this.getSize( );
>
> Device dv = Display.getCurrent( );
> Font fo = new Font( dv, "Courier", SWT.BOLD, 16 );//$NON-NLS-1$
> g2d.setFont( fo );
> FontMetrics fm = g2d.getFontMetrics( );
> g2d.setBackground( dv.getSystemColor( SWT.COLOR_WHITE ) );
> g2d.fillRectangle( 20, 20, d.x - 40, d.y - 40 );
> g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
> g2d.drawRectangle( 20, 20, d.x - 40, d.y - 40 );
> g2d.setClipping( 20, 20, d.x - 40, d.y - 40 );
> int x = 25, y = 20 + fm.getHeight( );
> g2d.drawString( "Exception:", x, y );//$NON-NLS-1$
> x += g2d.textExtent( "Exception:" ).x + 5;//$NON-NLS-1$
> g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) );
> g2d.drawString( sException, x, y );
> x = 25;
> y += fm.getHeight( );
> if ( sWrappedException != null )
> {
> g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
> g2d.drawString( "Wrapped In:", x, y );//$NON-NLS-1$
> x += g2d.textExtent( "Wrapped In:" ).x + 5;//$NON-NLS-1$
> g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) );
> g2d.drawString( sWrappedException, x, y );
> x = 25;
> y += fm.getHeight( );
> }
> g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
> y += 10;
> g2d.drawString( "Message:", x, y );//$NON-NLS-1$
> x += g2d.textExtent( "Message:" ).x + 5;//$NON-NLS-1$
> g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLUE ) );
> g2d.drawString( sMessage, x, y );
> x = 25;
> y += fm.getHeight( );
> g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
> y += 10;
> g2d.drawString( "Trace:", x, y );//$NON-NLS-1$
> x = 40;
> y += fm.getHeight( );
> g2d.setForeground( dv.getSystemColor( SWT.COLOR_DARK_GREEN ) );
> for ( int i = 0; i < stea.length; i++ )
> {
> g2d.drawString( stea[i].getClassName( ) + ":"//$NON-NLS-1$
> + stea[i].getMethodName( ) + "(...):"//$NON-NLS-1$
> + stea[i].getLineNumber( ), x, y );
> x = 40;
> y += fm.getHeight( );
> }
> fo.dispose( );
> }
>
> /*
> * (non-Javadoc)
> *
> * @see
> org.eclipse.birt.chart.device.IUpdateNotifier#getDesignTimeM odel()
> */
> public Chart getDesignTimeModel( )
> {
> return cm;
> }
>
> /*
> * (non-Javadoc)
> *
> * @see org.eclipse.birt.chart.device.IUpdateNotifier#getRunTimeMode l()
> */
> public Chart getRunTimeModel( )
> {
> return gcs.getChartModel( );
> }
>
> /*
> * (non-Javadoc)
> *
> * @see org.eclipse.birt.chart.device.IUpdateNotifier#peerInstance()
> */
> public Object peerInstance( )
> {
> return this;
> }
>
> /*
> * (non-Javadoc)
> *
> * @see org.eclipse.birt.chart.device.IUpdateNotifier#regenerateChar t()
> */
> public void regenerateChart( )
> {
> redraw( );
> }
>
> /*
> * (non-Javadoc)
> *
> * @see org.eclipse.birt.chart.device.IUpdateNotifier#repaintChart()
> */
> public void repaintChart( )
> {
> redraw( );
> }
>
> public boolean isbNeedsGeneration() {
> return bNeedsGeneration;
> }
>
> public void setbNeedsGeneration(boolean bNeedsGeneration) {
> this.bNeedsGeneration = bNeedsGeneration;
> }
> }
>
>
> The class Pie is copied from the birt examples plugin.
>
> Thanks in advance.
>
> Best regards and happy new year!
Previous Topic:BIRT Installation Issue
Next Topic:BIRT VIEWER PROBLEM
Goto Forum:
  


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

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

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

Back to the top