KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > birt > chart > examples > api > viewer > SwingLiveChartViewer


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.viewer;
13
14 import java.awt.BorderLayout JavaDoc;
15 import java.awt.Container JavaDoc;
16 import java.awt.Dimension JavaDoc;
17 import java.awt.Graphics JavaDoc;
18 import java.awt.Graphics2D JavaDoc;
19 import java.awt.Toolkit JavaDoc;
20 import java.util.Timer JavaDoc;
21 import java.util.TimerTask JavaDoc;
22
23 import javax.swing.JFrame JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25
26 import org.eclipse.birt.chart.device.IDeviceRenderer;
27 import org.eclipse.birt.chart.exception.ChartException;
28 import org.eclipse.birt.chart.factory.GeneratedChartState;
29 import org.eclipse.birt.chart.factory.Generator;
30 import org.eclipse.birt.chart.model.Chart;
31 import org.eclipse.birt.chart.model.ChartWithAxes;
32 import org.eclipse.birt.chart.model.attribute.AxisType;
33 import org.eclipse.birt.chart.model.attribute.Bounds;
34 import org.eclipse.birt.chart.model.attribute.IntersectionType;
35 import org.eclipse.birt.chart.model.attribute.LineAttributes;
36 import org.eclipse.birt.chart.model.attribute.LineStyle;
37 import org.eclipse.birt.chart.model.attribute.Marker;
38 import org.eclipse.birt.chart.model.attribute.MarkerType;
39 import org.eclipse.birt.chart.model.attribute.Position;
40 import org.eclipse.birt.chart.model.attribute.RiserType;
41 import org.eclipse.birt.chart.model.attribute.TickStyle;
42 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
43 import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
44 import org.eclipse.birt.chart.model.component.Axis;
45 import org.eclipse.birt.chart.model.component.Series;
46 import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
47 import org.eclipse.birt.chart.model.data.NumberDataSet;
48 import org.eclipse.birt.chart.model.data.SeriesDefinition;
49 import org.eclipse.birt.chart.model.data.TextDataSet;
50 import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
51 import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
52 import org.eclipse.birt.chart.model.data.impl.TextDataSetImpl;
53 import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl;
54 import org.eclipse.birt.chart.model.layout.Legend;
55 import org.eclipse.birt.chart.model.layout.Plot;
56 import org.eclipse.birt.chart.model.type.BarSeries;
57 import org.eclipse.birt.chart.model.type.LineSeries;
58 import org.eclipse.birt.chart.model.type.impl.BarSeriesImpl;
59 import org.eclipse.birt.chart.model.type.impl.LineSeriesImpl;
60 import org.eclipse.birt.chart.util.PluginSettings;
61
62 /**
63  * Generates a combination of live chart (Line chart + bar chart) using a Swing JPanel.
64  */

65
66 public final class SwingLiveChartViewer extends JPanel JavaDoc
67 {
68
69     private static final long serialVersionUID = 1L;
70     /**
71      * A chart model instance
72      */

73     private Chart cm = null;
74
75     /**
76      * The swing rendering device
77      */

78     private IDeviceRenderer dRenderer = null;
79
80     /**
81      * Maintains the structure of the chart for quick refresh
82      */

83     private GeneratedChartState gcState = null;
84
85     /**
86      * Used in building the chart for the first time
87      */

88     private boolean bFirstPaint = true;
89
90     /**
91      * execute application
92      * @param args
93      */

94     public static void main( String JavaDoc[] args )
95     {
96         SwingLiveChartViewer lcViewer = new SwingLiveChartViewer( );
97         JFrame JavaDoc frame = new JFrame JavaDoc( );
98         frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
99         Container JavaDoc container = frame.getContentPane( );
100         container.setLayout( new BorderLayout JavaDoc( ) );
101         container.add( lcViewer, BorderLayout.CENTER );
102
103         // Center window on the screen
104
Dimension JavaDoc dScreen = Toolkit.getDefaultToolkit( ).getScreenSize( );
105         Dimension JavaDoc dApp = new Dimension JavaDoc( 600, 400 );
106         frame.setSize( dApp );
107         frame.setLocation( ( dScreen.width - dApp.width ) / 2,
108                 ( dScreen.height - dApp.height ) / 2 );
109
110         frame.setTitle( lcViewer.getClass( ).getName( )
111                 + " [device=" + lcViewer.dRenderer.getClass( ).getName( ) + "]" );//$NON-NLS-1$//$NON-NLS-2$
112
frame.setVisible( true );
113     }
114
115     /**
116      * Constructor
117      */

118     SwingLiveChartViewer( )
119     {
120         final PluginSettings ps = PluginSettings.instance( );
121         try
122         {
123             dRenderer = ps.getDevice( "dv.SWING" );//$NON-NLS-1$
124
}
125         catch ( ChartException ex )
126         {
127             ex.printStackTrace( );
128         }
129         cm = createLiveChart( );
130     }
131
132     /**
133      * Called to refresh the panel that renders the chart
134      */

135     public void paint( Graphics JavaDoc g )
136     {
137         super.paint( g );
138         Graphics2D JavaDoc g2d = (Graphics2D JavaDoc) g;
139         dRenderer.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, g2d );
140         Dimension JavaDoc d = getSize( );
141         Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height );
142         bo.scale( 72d / dRenderer.getDisplayServer( ).getDpiResolution( ) );
143
144         final Generator gr = Generator.instance( );
145         if ( bFirstPaint )
146         {
147             bFirstPaint = false;
148             try
149             {
150                 gcState = gr.build( dRenderer.getDisplayServer( ),
151                         cm,
152                         bo,
153                         null,
154                         null,
155                         null );
156             }
157             catch ( ChartException ex )
158             {
159                 ex.printStackTrace( );
160             }
161
162             Timer JavaDoc t = new Timer JavaDoc( );
163             t.schedule( new ChartRefresh( ), 1000 );
164         }
165         try
166         {
167             gr.render( dRenderer, gcState );
168         }
169         catch ( ChartException ex )
170         {
171             ex.printStackTrace( );
172         }
173     }
174
175     //Live Date Set
176
private static final String JavaDoc[] sa = {
177             "One", "Two", "Three", "Four", "Five",//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$//$NON-NLS-5$
178
"Six", "Seven", "Eight", "Nine", "Ten"};//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$//$NON-NLS-5$
179
private static final double[] da1 = {
180             56.99,
181             352.95,
182             -201.95,
183             299.95,
184             -95.95,
185             25.45,
186             129.33,
187             -26.5,
188             43.5,
189             122
190     };
191
192     private static final double[] da2 = {
193             20, 35, 59, 105, 150, -37, -65, -99, -145, -185
194     };
195
196     /**
197      * Creates a chart instance that may be used to demo live/animated
198      * charts with scrolling data
199      *
200      * @return An instance of the simulated runtime chart model (containing
201      * filled datasets)
202      */

203     public static final Chart createLiveChart( )
204     {
205         ChartWithAxes cwaBar = ChartWithAxesImpl.create( );
206
207         //Plot
208
cwaBar.getBlock( ).setBackground( ColorDefinitionImpl.WHITE( ) );
209         Plot p = cwaBar.getPlot( );
210         p.getClientArea( ).setBackground( ColorDefinitionImpl.create( 255,
211                 255,
212                 225 ) );
213
214         //Legend
215
Legend lg = cwaBar.getLegend( );
216         LineAttributes lia = lg.getOutline( );
217         lg.getText( ).getFont( ).setSize( 16 );
218         lia.setStyle( LineStyle.SOLID_LITERAL );
219         lg.getInsets( ).setLeft( 10 );
220         lg.getInsets( ).setRight( 10 );
221
222         //Title
223
cwaBar.getTitle( )
224                 .getLabel( )
225                 .getCaption( )
226                 .setValue( "Live Chart Demo" );//$NON-NLS-1$
227

228         //X-Axis
229
Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes( )[0];
230
231         xAxisPrimary.setType( AxisType.TEXT_LITERAL );
232         xAxisPrimary.getOrigin( ).setType( IntersectionType.VALUE_LITERAL );
233         xAxisPrimary.getOrigin( ).setType( IntersectionType.MIN_LITERAL );
234
235         xAxisPrimary.getTitle( )
236                 .getCaption( )
237                 .setValue( "Category Text X-Axis" );//$NON-NLS-1$
238
xAxisPrimary.setTitlePosition( Position.BELOW_LITERAL );
239
240         xAxisPrimary.getLabel( ).getCaption( ).getFont( ).setRotation( 75 );
241         xAxisPrimary.setLabelPosition( Position.BELOW_LITERAL );
242
243         xAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.BELOW_LITERAL );
244         xAxisPrimary.getMajorGrid( )
245                 .getLineAttributes( )
246                 .setStyle( LineStyle.DOTTED_LITERAL );
247         xAxisPrimary.getMajorGrid( )
248                 .getLineAttributes( )
249                 .setColor( ColorDefinitionImpl.create( 64, 64, 64 ) );
250         xAxisPrimary.getMajorGrid( ).getLineAttributes( ).setVisible( true );
251
252         //Y-Axis
253
Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary );
254
255         yAxisPrimary.getLabel( ).getCaption( ).setValue( "Price Axis" );//$NON-NLS-1$
256
yAxisPrimary.getLabel( ).getCaption( ).getFont( ).setRotation( 37 );
257         yAxisPrimary.setLabelPosition( Position.LEFT_LITERAL );
258
259         yAxisPrimary.setTitlePosition( Position.LEFT_LITERAL );
260         yAxisPrimary.getTitle( ).getCaption( ).setValue( "Linear Value Y-Axis" );//$NON-NLS-1$
261

262         yAxisPrimary.setType( AxisType.LINEAR_LITERAL );
263
264         yAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.LEFT_LITERAL );
265         yAxisPrimary.getMajorGrid( )
266                 .getLineAttributes( )
267                 .setStyle( LineStyle.DOTTED_LITERAL );
268         yAxisPrimary.getMajorGrid( )
269                 .getLineAttributes( )
270                 .setColor( ColorDefinitionImpl.RED( ) );
271         yAxisPrimary.getMajorGrid( ).getLineAttributes( ).setVisible( true );
272
273         //Associate with Data Set
274
TextDataSet categoryValues = TextDataSetImpl.create( sa );
275         NumberDataSet seriesOneValues = NumberDataSetImpl.create( da1 );
276         NumberDataSet seriesTwoValues = NumberDataSetImpl.create( da2 );
277
278         //X-Series
279
Series seCategory = SeriesImpl.create( );
280         seCategory.setDataSet( categoryValues );
281
282         SeriesDefinition sdX = SeriesDefinitionImpl.create( );
283         xAxisPrimary.getSeriesDefinitions( ).add( sdX );
284         sdX.getSeries( ).add( seCategory );
285
286         //Y-Series (1)
287
BarSeries bs1 = (BarSeries) BarSeriesImpl.create( );
288         bs1.setSeriesIdentifier( "Unit Price" );//$NON-NLS-1$
289
bs1.setDataSet( seriesOneValues );
290         bs1.setRiserOutline( null );
291         bs1.setRiser( RiserType.RECTANGLE_LITERAL );
292
293         //Y-Series (2)
294
LineSeries ls1 = (LineSeries) LineSeriesImpl.create( );
295         ls1.setSeriesIdentifier( "Quantity" );//$NON-NLS-1$
296
ls1.setDataSet( seriesTwoValues );
297         ls1.getLineAttributes( ).setColor( ColorDefinitionImpl.GREEN( ) );
298         for ( int i = 0; i < ls1.getMarkers( ).size( ); i++ )
299         {
300             ( (Marker) ls1.getMarkers( ).get( i ) ).setType( MarkerType.BOX_LITERAL );
301         }
302         ls1.setCurve( true );
303
304         SeriesDefinition sdY = SeriesDefinitionImpl.create( );
305         yAxisPrimary.getSeriesDefinitions( ).add( sdY );
306         sdY.getSeriesPalette( ).update( 1 );
307         sdY.getSeries( ).add( bs1 );
308         sdY.getSeries( ).add( ls1 );
309
310         return cwaBar;
311     }
312
313     /**
314      * A method for changing the data value
315      * @param cwa
316      * @param iOffset
317      */

318     static final void scrollData( ChartWithAxes cwa )
319     {
320         //Scroll the bar (Y) series
321
double dTemp = da1[0];
322         for ( int i = 0; i < da1.length - 1; i++ )
323         {
324             da1[i] = da1[i + 1];
325         }
326         da1[da1.length - 1] = dTemp;
327
328         //Scroll the line (Y) series
329
dTemp = da2[0];
330         for ( int i = 0; i < da2.length - 1; i++ )
331         {
332             da2[i] = da2[i + 1];
333         }
334         da2[da2.length - 1] = dTemp;
335
336         //Scroll X series
337
String JavaDoc sTemp = sa[0];
338         for ( int i = 0; i < sa.length - 1; i++ )
339         {
340             sa[i] = sa[i + 1];
341         }
342         sa[sa.length - 1] = sTemp;
343     }
344
345     /**
346      * A background thread that scrolls/refreshes the chart (offscreeen)
347      */

348     private final class ChartRefresh extends TimerTask JavaDoc
349     {
350
351         public final void run( )
352         {
353             for ( ;; )
354             {
355                 final Generator gr = Generator.instance( );
356                 scrollData( (ChartWithAxes) cm );
357
358                 // Refresh
359
try
360                 {
361                     gr.refresh( gcState );
362                 }
363                 catch ( ChartException ex )
364                 {
365                     ex.printStackTrace( );
366                 }
367                 repaint( );
368
369                 // Delay
370
try
371                 {
372                     Thread.sleep( 500 );
373                 }
374                 catch ( InterruptedException JavaDoc iex )
375                 {
376                     iex.printStackTrace( );
377                 }
378             }
379         }
380     }
381 }
382
Popular Tags