KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > birt > chart > examples > api > processor > StyleChartViewer


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.processor;
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.model.Chart;
18 import org.eclipse.birt.chart.model.ChartWithAxes;
19 import org.eclipse.birt.chart.model.attribute.AxisType;
20 import org.eclipse.birt.chart.model.attribute.Bounds;
21 import org.eclipse.birt.chart.model.attribute.IntersectionType;
22 import org.eclipse.birt.chart.model.attribute.TickStyle;
23 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
24 import org.eclipse.birt.chart.model.component.Axis;
25 import org.eclipse.birt.chart.model.component.Series;
26 import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
27 import org.eclipse.birt.chart.model.data.NumberDataSet;
28 import org.eclipse.birt.chart.model.data.SeriesDefinition;
29 import org.eclipse.birt.chart.model.data.TextDataSet;
30 import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
31 import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
32 import org.eclipse.birt.chart.model.data.impl.TextDataSetImpl;
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 /**
49  * The selector of charts in SWT.
50  *
51  */

52 public final class StyleChartViewer implements
53         PaintListener
54 {
55     private IDeviceRenderer idr = null;
56
57     private Chart cm = null;
58
59     /**
60      * main() method for constructing the layout.
61      *
62      * @param args
63      */

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

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

149     public void paintControl( PaintEvent e )
150     {
151         idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, e.gc );
152         Composite co = (Composite) e.getSource( );
153         Rectangle re = co.getClientArea( );
154         Bounds bo = BoundsImpl.create( 0, 0, re.width, re.height );
155         bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) );
156         
157         Generator gr = Generator.instance( );
158         try
159         {
160             gr.render( idr, gr.build( idr.getDisplayServer( ),
161                     cm,
162                     bo,
163                     null,
164                     null,
165                     StyleProcessor.instance( ) ) );
166         }
167         catch ( ChartException ce )
168         {
169             ce.printStackTrace( );
170         }
171     }
172
173 }
174
Popular Tags