KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > birt > chart > examples > api > data > autobinding > AutoDataBindingViewer


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

11
12 package org.eclipse.birt.chart.examples.api.data.autobinding;
13
14 import org.eclipse.birt.chart.device.IDeviceRenderer;
15 import org.eclipse.birt.chart.exception.ChartException;
16 import org.eclipse.birt.chart.factory.Generator;
17 import org.eclipse.birt.chart.factory.IDataRowExpressionEvaluator;
18 import org.eclipse.birt.chart.factory.RunTimeContext;
19 import org.eclipse.birt.chart.model.Chart;
20 import org.eclipse.birt.chart.model.ChartWithAxes;
21 import org.eclipse.birt.chart.model.attribute.AxisType;
22 import org.eclipse.birt.chart.model.attribute.Bounds;
23 import org.eclipse.birt.chart.model.attribute.IntersectionType;
24 import org.eclipse.birt.chart.model.attribute.TickStyle;
25 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
26 import org.eclipse.birt.chart.model.component.Axis;
27 import org.eclipse.birt.chart.model.component.Series;
28 import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
29 import org.eclipse.birt.chart.model.data.Query;
30 import org.eclipse.birt.chart.model.data.SeriesDefinition;
31 import org.eclipse.birt.chart.model.data.impl.QueryImpl;
32 import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
33 import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl;
34 import org.eclipse.birt.chart.model.type.BarSeries;
35 import org.eclipse.birt.chart.model.type.impl.BarSeriesImpl;
36 import org.eclipse.birt.chart.util.PluginSettings;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.events.PaintEvent;
39 import org.eclipse.swt.events.PaintListener;
40 import org.eclipse.swt.graphics.Rectangle;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.layout.GridLayout;
43 import org.eclipse.swt.widgets.Canvas;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Display;
46 import org.eclipse.swt.widgets.Shell;
47
48 import com.ibm.icu.util.ULocale;
49
50 /**
51  * The selector of charts in SWT.
52  *
53  */

54 public final class AutoDataBindingViewer implements
55         PaintListener
56 {
57     private IDeviceRenderer idr = null;
58
59     private Chart cm = null;
60     
61     private IDataRowExpressionEvaluator dree = null;
62
63     /**
64      * main() method for constructing the layout.
65      *
66      * @param args
67      */

68     public static void main( String JavaDoc[] args )
69     {
70         Display display = Display.getDefault( );
71         Shell shell = new Shell( display );
72         shell.setSize( 600, 400 );
73         shell.setLayout( new GridLayout( ) );
74
75         Canvas cCenter = new Canvas( shell, SWT.NONE );
76         AutoDataBindingViewer adbv = new AutoDataBindingViewer( );
77         cCenter.setLayoutData( new GridData( GridData.FILL_BOTH ) );
78         cCenter.addPaintListener( adbv );
79
80         shell.open( );
81         while ( !shell.isDisposed( ) )
82         {
83             if ( !display.readAndDispatch( ) )
84                 display.sleep( );
85         }
86         display.dispose( );
87     }
88
89     /**
90      * Get the connection with SWT device to render the graphics.
91      */

92     AutoDataBindingViewer( )
93     {
94         final PluginSettings ps = PluginSettings.instance( );
95         try
96         {
97             idr = ps.getDevice( "dv.SWT" );//$NON-NLS-1$
98
}
99         catch ( ChartException ex )
100         {
101             ex.printStackTrace( );
102         }
103         cm = createSimpleChart( );
104     }
105
106     private static final Chart createSimpleChart( )
107     {
108         ChartWithAxes cwaBar = ChartWithAxesImpl.create( );
109
110         // X-Axis
111
Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes( )[0];
112         xAxisPrimary.setType( AxisType.TEXT_LITERAL );
113         xAxisPrimary.getOrigin( ).setType( IntersectionType.VALUE_LITERAL );
114         xAxisPrimary.getTitle( ).setVisible( true );
115
116         // Y-Axis
117
Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary );
118         yAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.LEFT_LITERAL );
119         yAxisPrimary.setType( AxisType.LINEAR_LITERAL );
120         yAxisPrimary.getTitle( ).setVisible( true );
121
122         // X-Series
123
Series seCategory = SeriesImpl.create( );
124         Query query = QueryImpl.create( "Items" );//$NON-NLS-1$
125
seCategory.getDataDefinition( ).add( query );
126
127         SeriesDefinition sdX = SeriesDefinitionImpl.create( );
128         xAxisPrimary.getSeriesDefinitions( ).add( sdX );
129         sdX.getSeries( ).add( seCategory );
130
131         // Y-Series
132
BarSeries bs = (BarSeries) BarSeriesImpl.create( );
133         Query query2 = QueryImpl.create( "Amounts" );//$NON-NLS-1$
134
bs.getDataDefinition( ).add( query2 );
135         bs.getLabel( ).setVisible( true );
136
137         SeriesDefinition sdY = SeriesDefinitionImpl.create( );
138         yAxisPrimary.getSeriesDefinitions( ).add( sdY );
139         sdY.getSeries( ).add( bs );
140
141         return cwaBar;
142     }
143     /*
144      * (non-Javadoc)
145      *
146      * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
147      */

148     public void paintControl( PaintEvent e )
149     {
150         idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, e.gc );
151         Composite co = (Composite) e.getSource( );
152         Rectangle re = co.getClientArea( );
153         Bounds bo = BoundsImpl.create( 0, 0, re.width, re.height );
154         bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) );
155         
156         RunTimeContext context = new RunTimeContext( );
157         context.setULocale( ULocale.getDefault( ) );
158         
159         String JavaDoc[] set = {
160                 "Items", "Amounts"};//$NON-NLS-1$ //$NON-NLS-2$
161
Object JavaDoc[][] data = {
162                 {
163                         "A", "B", "C"//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
164
}, {
165                         new Integer JavaDoc( 7 ), new Integer JavaDoc( 2 ), new Integer JavaDoc( 5 )
166                 }
167         };
168         dree = new DataRowExpressionEvaluator( set, data );
169         Generator gr = Generator.instance( );
170         try
171         {
172             gr.bindData( dree, cm, context );
173             gr.render( idr, gr.build( idr.getDisplayServer( ),
174                     cm,
175                     bo,
176                     null,
177                     context,
178                     null ) );
179         }
180         catch ( ChartException ce )
181         {
182             ce.printStackTrace( );
183         }
184     }
185
186 }
187
188
Popular Tags