1 11 12 package org.eclipse.birt.chart.examples.api.viewer; 13 14 import java.awt.BorderLayout ; 15 import java.awt.Container ; 16 import java.awt.Dimension ; 17 import java.awt.Graphics ; 18 import java.awt.Graphics2D ; 19 import java.awt.Toolkit ; 20 import java.util.Timer ; 21 import java.util.TimerTask ; 22 23 import javax.swing.JFrame ; 24 import javax.swing.JPanel ; 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 65 66 public final class SwingLiveChartViewer extends JPanel 67 { 68 69 private static final long serialVersionUID = 1L; 70 73 private Chart cm = null; 74 75 78 private IDeviceRenderer dRenderer = null; 79 80 83 private GeneratedChartState gcState = null; 84 85 88 private boolean bFirstPaint = true; 89 90 94 public static void main( String [] args ) 95 { 96 SwingLiveChartViewer lcViewer = new SwingLiveChartViewer( ); 97 JFrame frame = new JFrame ( ); 98 frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); 99 Container container = frame.getContentPane( ); 100 container.setLayout( new BorderLayout ( ) ); 101 container.add( lcViewer, BorderLayout.CENTER ); 102 103 Dimension dScreen = Toolkit.getDefaultToolkit( ).getScreenSize( ); 105 Dimension dApp = new Dimension ( 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( ) + "]" ); frame.setVisible( true ); 113 } 114 115 118 SwingLiveChartViewer( ) 119 { 120 final PluginSettings ps = PluginSettings.instance( ); 121 try 122 { 123 dRenderer = ps.getDevice( "dv.SWING" ); } 125 catch ( ChartException ex ) 126 { 127 ex.printStackTrace( ); 128 } 129 cm = createLiveChart( ); 130 } 131 132 135 public void paint( Graphics g ) 136 { 137 super.paint( g ); 138 Graphics2D g2d = (Graphics2D ) g; 139 dRenderer.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, g2d ); 140 Dimension 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 t = new Timer ( ); 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 private static final String [] sa = { 177 "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"}; 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 203 public static final Chart createLiveChart( ) 204 { 205 ChartWithAxes cwaBar = ChartWithAxesImpl.create( ); 206 207 cwaBar.getBlock( ).setBackground( ColorDefinitionImpl.WHITE( ) ); 209 Plot p = cwaBar.getPlot( ); 210 p.getClientArea( ).setBackground( ColorDefinitionImpl.create( 255, 211 255, 212 225 ) ); 213 214 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 cwaBar.getTitle( ) 224 .getLabel( ) 225 .getCaption( ) 226 .setValue( "Live Chart Demo" ); 228 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" ); 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 Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary ); 254 255 yAxisPrimary.getLabel( ).getCaption( ).setValue( "Price Axis" ); 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" ); 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 TextDataSet categoryValues = TextDataSetImpl.create( sa ); 275 NumberDataSet seriesOneValues = NumberDataSetImpl.create( da1 ); 276 NumberDataSet seriesTwoValues = NumberDataSetImpl.create( da2 ); 277 278 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 BarSeries bs1 = (BarSeries) BarSeriesImpl.create( ); 288 bs1.setSeriesIdentifier( "Unit Price" ); bs1.setDataSet( seriesOneValues ); 290 bs1.setRiserOutline( null ); 291 bs1.setRiser( RiserType.RECTANGLE_LITERAL ); 292 293 LineSeries ls1 = (LineSeries) LineSeriesImpl.create( ); 295 ls1.setSeriesIdentifier( "Quantity" ); 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 318 static final void scrollData( ChartWithAxes cwa ) 319 { 320 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 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 String 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 348 private final class ChartRefresh extends TimerTask 349 { 350 351 public final void run( ) 352 { 353 for ( ;; ) 354 { 355 final Generator gr = Generator.instance( ); 356 scrollData( (ChartWithAxes) cm ); 357 358 try 360 { 361 gr.refresh( gcState ); 362 } 363 catch ( ChartException ex ) 364 { 365 ex.printStackTrace( ); 366 } 367 repaint( ); 368 369 try 371 { 372 Thread.sleep( 500 ); 373 } 374 catch ( InterruptedException iex ) 375 { 376 iex.printStackTrace( ); 377 } 378 } 379 } 380 } 381 } 382 | Popular Tags |