Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » BIRT » Implementation of the zoom in a chart
Implementation of the zoom in a chart [message #1016368] Wed, 06 March 2013 09:20 Go to next message
txas kun is currently offline txas kunFriend
Messages: 5
Registered: March 2013
Junior Member
Hello!

I am visualizing in a view (Eclipse, java, SWT) a BIRT Line Chart, amongst other things. Now I want to implement the zoom option. I have the rectangle of the selection but I am in a trouble to get the data that is in this rectangle(I have tried different things and I have search on internet for some days). Can someone guide me? Which are the steps that I should follow?

Thank you for your time!
Re: Implementation of the zoom in a chart [message #1016878 is a reply to message #1016368] Fri, 08 March 2013 02:20 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

How are you implementing the rectangle. If you could post an example that we can run and look at we may be able to help.

Jason
Re: Implementation of the zoom in a chart [message #1016910 is a reply to message #1016878] Fri, 08 March 2013 08:05 Go to previous messageGo to next message
txas kun is currently offline txas kunFriend
Messages: 5
Registered: March 2013
Junior Member
I have the rectangle that I have implemented in the listener of the ChartViewer and to be able to generate the Char I have an ArrayList of the dataset. In the listener I don´t know how can I access to the information of the dataset.

Listener listener = new Listener() {
   public void handleEvent(Event event) {
	switch (event.type) {
		case SWT.MouseDown:
			mouseDownXCoor = event.x;
			mouseDownYCoor = event.y;

			drag = true;
                        break;

		case SWT.MouseMove:
			if (drag) {
				mouseUpXCoor = event.x;
				mouseUpYCoor = event.y;
				lineChartViewer.redraw();
			}
			break;

		case SWT.MouseUp:
			mouseUpXCoor = event.x;
			mouseUpYCoor = event.y;
			drag = false;
			break;

		case SWT.Paint:
			if (drag) {
			    GC gc = event.gc;
			    gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
			    gc.setAlpha(128);

		  	    int minX = Math.min(mouseDownXCoor, mouseUpXCoor);
  			    int minY = Math.min(mouseDownYCoor, mouseUpYCoor);
  			    
                            int maxX = Math.max(mouseDownXCoor, mouseUpXCoor);
			    int maxY = Math.max(mouseDownYCoor, mouseUpYCoor);

			    int width = maxX - minX;
			    int height = maxY - minY;

			    gc.fillRectangle(minX, minY, width, height);
			}
		        break;
		}
	}
};
lineChartViewer.addListener(SWT.MouseDown, listener);
lineChartViewer.addListener(SWT.MouseMove, listener);
lineChartViewer.addListener(SWT.MouseUp, listener);
lineChartViewer.addListener(SWT.Paint, listener);


This is the listener part where I create the rectangle. Thanks!!

[Updated on: Wed, 13 March 2013 09:50]

Report message to a moderator

Re: Implementation of the zoom in a chart [message #1017047 is a reply to message #1016910] Fri, 08 March 2013 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 looked at this thread:
Chart Zoom

Jason
Re: Implementation of the zoom in a chart [message #1018167 is a reply to message #1017047] Wed, 13 March 2013 09:49 Go to previous messageGo to next message
txas kun is currently offline txas kunFriend
Messages: 5
Registered: March 2013
Junior Member
Hi Jason,

I have looked and your example I can't run it, but I think that it is not important. I have read Savitha's comment and I think that is interesting, but I have a problem: I have never worked with triggers. These days I have been trying to do something but I have no progress done.

I have try to add the Trigger to the lineSeries but then I don't know how I can continue. Here is my trigger example.

LineSeries ls = (LineSeries) LineSeriesImpl.create();

ls.getTriggers().add(TriggerImpl.create(TriggerCondition.ONMOUSEUP_LITERAL,
            			ActionImpl.create(ActionType.CALL_BACK_LITERAL,
            			CallBackValueImpl.create(String.valueOf(ls.getSeriesIdentifier())))
            			));


I am a little bit lost. Can someone help me explaining the steps or with an easy example?
Thanks.
Re: Implementation of the zoom in a chart [message #1027133 is a reply to message #1018167] Tue, 26 March 2013 15:36 Go to previous messageGo to next message
txas kun is currently offline txas kunFriend
Messages: 5
Registered: March 2013
Junior Member
Has somebody an example how to work with Triggers?

Thanks!
Re: Implementation of the zoom in a chart [message #1027367 is a reply to message #1027133] Tue, 26 March 2013 22:33 Go to previous message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

When you use that type of trigger the class needs to implement ICallBackNotifier and you need to set the update notifier for the device renderer like:
idr.setProperty( IDeviceRenderer.UPDATE_NOTIFIER, this );

Look at this example

package my.swt.live.viewer;

import org.eclipse.birt.chart.computation.DataPointHints;
import org.eclipse.birt.chart.device.ICallBackNotifier;
import org.eclipse.birt.chart.device.IDeviceRenderer;
import org.eclipse.birt.chart.event.StructureSource;
import org.eclipse.birt.chart.event.WrappedStructureSource;
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.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.CallBackValue;
import org.eclipse.birt.chart.model.attribute.IntersectionType;
import org.eclipse.birt.chart.model.attribute.LineAttributes;
import org.eclipse.birt.chart.model.attribute.LineStyle;
import org.eclipse.birt.chart.model.attribute.Marker;
import org.eclipse.birt.chart.model.attribute.MarkerType;
import org.eclipse.birt.chart.model.attribute.Position;
import org.eclipse.birt.chart.model.attribute.RiserType;
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.CallBackValueImpl;
import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
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.NumberDataSet;
import org.eclipse.birt.chart.model.data.SeriesDefinition;
import org.eclipse.birt.chart.model.data.TextDataSet;
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.LineSeries;
import org.eclipse.birt.chart.model.type.impl.BarSeriesImpl;
import org.eclipse.birt.chart.model.type.impl.LineSeriesImpl;
import org.eclipse.birt.chart.util.PluginSettings;
import org.eclipse.birt.core.framework.PlatformConfig;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

public class SwtLiveChartViewer extends Composite implements
PaintListener,
ICallBackNotifier
{
private boolean bNeedsGeneration = true;

private IDeviceRenderer idr = null;

private Chart cm = null;


private GeneratedChartState gcs = null;

public static SwtLiveChartViewer c3dViewer;

/**
* Used in building the chart for the first time
*/
private boolean bFirstPaint = true;
public Chart getDesignTimeModel( )
{
return cm;
}
public void regenerateChart( )
{
redraw( );
}
public Chart getRunTimeModel( )
{
return gcs.getChartModel( );
}

public void repaintChart( )
{
redraw( );
}

public Object peerInstance()
{
return this;
}

public void callback( Object event, Object source, CallBackValue value )
{
Object obj1 = ((DataPointHints)((WrappedStructureSource)source).getSource()).getBaseValue();
Object obj2 = ((DataPointHints)((WrappedStructureSource)source).getSource()).getOrthogonalValue().toString();
Object obj3 = ((DataPointHints)((WrappedStructureSource)source).getSource()).getSeriesValue();
double xval = ((DataPointHints)((WrappedStructureSource)source).getSource()).getLocation().getX();
double yval = ((DataPointHints)((WrappedStructureSource)source).getSource()).getLocation().getY();

StructureSource structuredSource = (StructureSource) source;

DataPointHints dph = (DataPointHints) structuredSource.getSource();

Object obj4 = ((DataPointHints)((WrappedStructureSource)source).getSource()).getSeriesValue();
MessageBox mb = new MessageBox ( this.getShell( ) );
mb.setMessage( xval + "," + yval);
mb.open( );
}
/**
* execute application
*
* @param args
*/
public static void main( String[] args )
{
Display display = Display.getDefault( );
Shell shell = new Shell( display );
shell.setSize( 600, 400 );
shell.setLayout( new GridLayout( ) );

c3dViewer = new SwtLiveChartViewer( shell, SWT.NO_BACKGROUND );
c3dViewer.setLayoutData( new GridData( GridData.FILL_BOTH ) );
c3dViewer.addPaintListener( c3dViewer );


shell.open( );
while ( !shell.isDisposed( ) )
{
if ( !display.readAndDispatch( ) )
display.sleep( );
}
display.dispose( );
}

/**
* Constructor
*/
SwtLiveChartViewer( Composite parent, int style )
{

super( parent, style );


PlatformConfig config = new PlatformConfig();
config.setProperty( "STANDALONE", "true" ); //$NON-NLS-1$//$NON-NLS-2$
final PluginSettings ps = PluginSettings.instance( config );
try
{
idr = ps.getDevice( "dv.SWT" );//$NON-NLS-1$
idr.setProperty( IDeviceRenderer.UPDATE_NOTIFIER, this );
}
catch ( ChartException ex )
{
ex.printStackTrace( );
}

cm = createLiveChart( );



}
public static final Chart createLiveChart( )
{
ChartWithAxes cwaBar = ChartWithAxesImpl.create( );

//Plot
cwaBar.getBlock( ).setBackground( ColorDefinitionImpl.WHITE( ) );
Plot p = cwaBar.getPlot( );
p.getClientArea( ).setBackground( ColorDefinitionImpl.create( 255,
255,
225 ) );

//Legend
Legend lg = cwaBar.getLegend( );
LineAttributes lia = lg.getOutline( );
lg.getText( ).getFont( ).setSize( 16 );
lia.setStyle( LineStyle.SOLID_LITERAL );
lg.getInsets( ).setLeft( 10 );
lg.getInsets( ).setRight( 10 );

//Title
cwaBar.getTitle( )
.getLabel( )
.getCaption( )
.setValue( "Live Chart Demo" );//$NON-NLS-1$

//X-Axis
Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes( )[0];

xAxisPrimary.setType( AxisType.TEXT_LITERAL );
xAxisPrimary.getOrigin( ).setType( IntersectionType.VALUE_LITERAL );
xAxisPrimary.getOrigin( ).setType( IntersectionType.MIN_LITERAL );

xAxisPrimary.getTitle( )
.getCaption( )
.setValue( "Category Text X-Axis" );//$NON-NLS-1$
xAxisPrimary.setTitlePosition( Position.BELOW_LITERAL );

xAxisPrimary.getLabel( ).getCaption( ).getFont( ).setRotation( 75 );
xAxisPrimary.setLabelPosition( Position.BELOW_LITERAL );

xAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.BELOW_LITERAL );
xAxisPrimary.getMajorGrid( )
.getLineAttributes( )
.setStyle( LineStyle.DOTTED_LITERAL );
xAxisPrimary.getMajorGrid( )
.getLineAttributes( )
.setColor( ColorDefinitionImpl.create( 64, 64, 64 ) );
xAxisPrimary.getMajorGrid( ).getLineAttributes( ).setVisible( true );

//Y-Axis
Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary );

yAxisPrimary.getLabel( ).getCaption( ).setValue( "Price Axis" );//$NON-NLS-1$
yAxisPrimary.getLabel( ).getCaption( ).getFont( ).setRotation( 37 );
yAxisPrimary.setLabelPosition( Position.LEFT_LITERAL );

yAxisPrimary.setTitlePosition( Position.LEFT_LITERAL );
yAxisPrimary.getTitle( ).getCaption( ).setValue( "Linear Value Y-Axis" );//$NON-NLS-1$

yAxisPrimary.setType( AxisType.LINEAR_LITERAL );

yAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.LEFT_LITERAL );
yAxisPrimary.getMajorGrid( )
.getLineAttributes( )
.setStyle( LineStyle.DOTTED_LITERAL );
yAxisPrimary.getMajorGrid( )
.getLineAttributes( )
.setColor( ColorDefinitionImpl.RED( ) );
yAxisPrimary.getMajorGrid( ).getLineAttributes( ).setVisible( true );

//X-Series
Series seCategory = SeriesImpl.create( );
SeriesDefinition sdX = SeriesDefinitionImpl.create( );
xAxisPrimary.getSeriesDefinitions( ).add( sdX );
sdX.getSeries( ).add( seCategory );

//Y-Series (1)
BarSeries bs1 = (BarSeries) BarSeriesImpl.create( );
bs1.setSeriesIdentifier( "Unit Price" );//$NON-NLS-1$
bs1.setRiserOutline( null );
bs1.setRiser( RiserType.RECTANGLE_LITERAL );

//bs1.getTriggers( ).add(
// TriggerImpl.create( TriggerCondition.MOUSE_CLICK_LITERAL,
// ActionImpl.create( ActionType.TOGGLE_VISIBILITY_LITERAL, SeriesValueImpl.create("not used" ) ) ) );


//Trigger tr1 = TriggerImpl.create(TriggerCondition.ONMOUSEOVER_LITERAL,
// ActionImpl.create(ActionType.SHOW_TOOLTIP_LITERAL,
// TooltipValueImpl.create(200, null)));


bs1.getTriggers( ).add( TriggerImpl.create( TriggerCondition.ONMOUSEOVER_LITERAL,
ActionImpl.create( ActionType.CALL_BACK_LITERAL,
CallBackValueImpl.create( String.valueOf( bs1.getSeriesIdentifier( ) ) ) ) ) );




//bs1.getTriggers().add(tr1);

//Y-Series (2)
LineSeries ls1 = (LineSeries) LineSeriesImpl.create( );
ls1.setSeriesIdentifier( "Quantity" );//$NON-NLS-1$
ls1.getLineAttributes( ).setColor( ColorDefinitionImpl.GREEN( ) );
for ( int i = 0; i < ls1.getMarkers( ).size( ); i++ )
{
( (Marker) ls1.getMarkers( ).get( i ) ).setType( MarkerType.BOX_LITERAL );
}
ls1.setCurve( true );

SeriesDefinition sdY = SeriesDefinitionImpl.create( );
yAxisPrimary.getSeriesDefinitions( ).add( sdY );
sdY.getSeriesPalette( ).shift( -1 );
sdY.getSeries( ).add( bs1 );
sdY.getSeries( ).add( ls1 );

// Update data
updateDataSet( cwaBar );
return cwaBar;
}
static final void updateDataSet( ChartWithAxes cwaBar )
{
// Associate with Data Set
TextDataSet categoryValues = TextDataSetImpl.create( sa );
NumberDataSet seriesOneValues = NumberDataSetImpl.create( da1 );
NumberDataSet seriesTwoValues = NumberDataSetImpl.create( da2 );

// X-Axis
Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes( )[0];
SeriesDefinition sdX = (SeriesDefinition) xAxisPrimary.getSeriesDefinitions( )
.get( 0 );
( (Series) sdX.getSeries( ).get( 0 ) ).setDataSet( categoryValues );

// Y-Axis
Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary );
SeriesDefinition sdY = (SeriesDefinition) yAxisPrimary.getSeriesDefinitions( )
.get( 0 );
( (Series) sdY.getSeries( ).get( 0 ) ).setDataSet( seriesOneValues );
( (Series) sdY.getSeries( ).get( 1 ) ).setDataSet( seriesTwoValues );
}
//Live Date Set
private static final String[] sa = {
"One", "Two", "Three", "Four", "Five",//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$//$NON-NLS-5$
"Six", "Seven", "Eight", "Nine", "Ten"};//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$//$NON-NLS-5$
private static final double[] da1 = {
56.99,
352.95,
-201.95,
299.95,
-95.95,
25.45,
129.33,
-26.5,
43.5,
122
};

private static final double[] da2 = {
20, 35, 59, 105, 150, -37, -65, -99, -145, -185
};

public final void paintControl( PaintEvent e )
{
Rectangle d = this.getClientArea( );
Image imgChart = new Image( this.getDisplay( ), d );
GC gcImage = new GC( imgChart );
//Look at IDeviceRenderer.java for all properties
//like DPI_RESOLUTION
//FILE_IDENTIFIER
//FORMAT_IDENTIFIER
//UPDATE_NOTIFIER
idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, gcImage );
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( );
}


}


if ( bFirstPaint )
{
bFirstPaint = false;


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



}
}

Previous Topic:The right and generic way to format numbers in all reports
Next Topic:Pagination, Run Task, Render Task, and Writing to Disk
Goto Forum:
  


Current Time: Fri Apr 26 05:53:34 GMT 2024

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

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

Back to the top