Advertisement
Guest User

Untitled

a guest
Nov 24th, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1.  
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Container;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.Toolkit;
  9.  
  10. import java.awt.event.ComponentEvent;
  11. import java.awt.event.ComponentListener;
  12.  
  13. import java.util.HashMap;
  14. import java.util.Map;
  15.  
  16. import javax.swing.JFrame;
  17. import javax.swing.JPanel;
  18.  
  19. import org.eclipse.birt.chart.api.ChartEngine;
  20.  
  21. import org.eclipse.birt.chart.device.IDeviceRenderer;
  22. import org.eclipse.birt.chart.device.IUpdateNotifier;
  23.  
  24. import org.eclipse.birt.chart.exception.ChartException;
  25.  
  26. import org.eclipse.birt.chart.factory.GeneratedChartState;
  27. import org.eclipse.birt.chart.factory.Generator;
  28.  
  29. import org.eclipse.birt.chart.model.Chart;
  30. import org.eclipse.birt.chart.model.ChartWithAxes;
  31.  
  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.LegendItemType;
  36. import org.eclipse.birt.chart.model.attribute.Position;
  37. import org.eclipse.birt.chart.model.attribute.TickStyle;
  38.  
  39. import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
  40. import org.eclipse.birt.chart.model.attribute.impl
  41. .ColorDefinitionImpl;
  42.  
  43. import org.eclipse.birt.chart.model.component.Axis;
  44. import org.eclipse.birt.chart.model.component.Series;
  45.  
  46. import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
  47.  
  48. import org.eclipse.birt.chart.model.data.NumberDataSet;
  49. import org.eclipse.birt.chart.model.data.SeriesDefinition;
  50. import org.eclipse.birt.chart.model.data.TextDataSet;
  51.  
  52. import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
  53. import org.eclipse.birt.chart.model.data.impl
  54. .SeriesDefinitionImpl;
  55. import org.eclipse.birt.chart.model.data.impl.TextDataSetImpl;
  56.  
  57. import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl;
  58.  
  59. import org.eclipse.birt.chart.model.layout.Legend;
  60. import org.eclipse.birt.chart.model.layout.Plot;
  61.  
  62. import org.eclipse.birt.chart.model.type.BarSeries;
  63.  
  64. import org.eclipse.birt.chart.model.type.impl.BarSeriesImpl;
  65. import org.eclipse.birt.chart.timeview.data.TVDataSet;
  66. import org.eclipse.birt.chart.timeview.data.impl.TVDataSetImpl;
  67. import org.eclipse.birt.chart.timeview.dataprocessor.TVEntry;
  68. import org.eclipse.birt.chart.timeview.model.TimeViewSeries;
  69. import org.eclipse.birt.chart.timeview.model.impl.TimeViewSeriesImpl;
  70.  
  71. import org.eclipse.birt.core.framework.PlatformConfig;
  72.  
  73. /*
  74. * The selector of charts in Swing JPanel.
  75. */
  76.  
  77. public final class SwingChartingApp extends JPanel implements
  78. IUpdateNotifier,
  79. ComponentListener
  80. {
  81. private static final long serialVersionUID = 1L;
  82. private boolean bNeedsGeneration = true;
  83. private GeneratedChartState gcs = null;
  84. private Chart cm = null;
  85. private IDeviceRenderer idr = null;
  86. private Map contextMap;
  87.  
  88. /*
  89. * Create the layout with a container for displaying a chart
  90. */
  91. public static void main( String[ ] args )
  92. {
  93. SwingChartingApp scv = new SwingChartingApp( );
  94. JFrame jf = new JFrame( );
  95. jf.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
  96. jf.addComponentListener( scv );
  97. Container co = jf.getContentPane( );
  98. co.setLayout( new BorderLayout( ) );
  99. co.add( scv, BorderLayout.CENTER );
  100.  
  101. Dimension dScreen = Toolkit.getDefaultToolkit( )
  102. .getScreenSize( );
  103. Dimension dApp = new Dimension( 800, 600 );
  104. jf.setSize( dApp );
  105. jf.setLocation( ( dScreen.width - dApp.width ) / 2,
  106. ( dScreen.height - dApp.height ) / 2 );
  107. jf.setTitle( scv.getClass( ).getName( ) + " [device="
  108. + scv.idr.getClass( ).getName( ) + "]" );//$NON-NLS-1$
  109. jf.setVisible( true );
  110. }
  111.  
  112. /*
  113. * Connect with a SWING device to render the graphics.
  114. */
  115. SwingChartingApp( )
  116. {
  117. contextMap = new HashMap( );
  118. try
  119. {
  120. PlatformConfig config = new PlatformConfig( );
  121. config.setBIRTHome("/Users/administrator/Desktop/birt-runtime-4_2_1/ReportEngine");
  122. // config.setProperty( "STANDALONE", "true" ); //$NON-NLS-1$
  123. //$NON-NLS-2$
  124. idr = ChartEngine.instance( config ).getRenderer(
  125. "dv.SWING" );//$NON-NLS-1$
  126. }
  127. catch ( ChartException ex )
  128. {
  129. ex.printStackTrace( );
  130. }
  131. cm = createBarChart( );
  132. }
  133. /* Build a simple bar chart */
  134. public static final Chart createBarChart( )
  135. {
  136. ChartWithAxes cwaBar = ChartWithAxesImpl.create( );
  137.  
  138. /* Plot */
  139. cwaBar.getBlock( )
  140. .setBackground( ColorDefinitionImpl.WHITE( ) );
  141. cwaBar.getBlock( ).getOutline( ).setVisible( true );
  142. Plot p = cwaBar.getPlot( );
  143. p.getClientArea( ).setBackground(
  144. ColorDefinitionImpl.create( 255, 255, 225 ) );
  145. p.getOutline( ).setVisible( false );
  146.  
  147. /* Title */
  148. cwaBar.getTitle( ).getLabel( ).getCaption( )
  149. .setValue( "TimeView Chart" );
  150.  
  151.  
  152.  
  153. /* X-Axis */
  154. Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes( )[0];
  155. xAxisPrimary.setType( AxisType.TEXT_LITERAL );
  156. xAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.BELOW_LITERAL );
  157. xAxisPrimary.getOrigin( ).setType( IntersectionType.VALUE_LITERAL );
  158. xAxisPrimary.getTitle( ).setVisible( true );
  159.  
  160. /* Y-Axis */
  161. Axis yAxisPrimary = cwaBar
  162. .getPrimaryOrthogonalAxis( xAxisPrimary );
  163. yAxisPrimary.getMajorGrid( )
  164. .setTickStyle( TickStyle.LEFT_LITERAL );
  165. yAxisPrimary.setType( AxisType.LINEAR_LITERAL );
  166. yAxisPrimary.getLabel( ).getCaption( ).getFont( )
  167. .setRotation( 90 );
  168.  
  169. /* Data Sets */
  170. TVEntry[] gea = {new TVEntry("active"),new TVEntry("active"),new TVEntry("finishng")};
  171. TVDataSet orthoValues = TVDataSetImpl
  172. .create(gea);
  173. TextDataSet categoryValues = TextDataSetImpl
  174. .create( new String[]{ "10","20","30" } );
  175.  
  176. /* X-Series */
  177. Series seCategory = SeriesImpl.create( );
  178. seCategory.setDataSet( categoryValues );
  179.  
  180. SeriesDefinition sdX = SeriesDefinitionImpl.create( );
  181. sdX.getSeriesPalette( ).shift( 0 );
  182. xAxisPrimary.getSeriesDefinitions( ).add( sdX );
  183. sdX.getSeries( ).add( seCategory );
  184.  
  185. /* Y-Series */
  186. TimeViewSeries bs = (TimeViewSeries) TimeViewSeriesImpl.create( );
  187. bs.setDataSet( orthoValues );
  188.  
  189. bs.getLabel( ).setVisible( true );
  190. bs.setLabelPosition( Position.INSIDE_LITERAL );
  191. SeriesDefinition sdY = SeriesDefinitionImpl.create( );
  192. yAxisPrimary.getSeriesDefinitions( ).add( sdY );
  193. sdY.getSeries( ).add( bs );
  194. return cwaBar;
  195. }
  196.  
  197. public void regenerateChart( )
  198. {
  199. bNeedsGeneration = true;
  200. repaint( );
  201. }
  202.  
  203. public void repaintChart( )
  204. {
  205. repaint( );
  206. }
  207.  
  208. public Object peerInstance( )
  209. {
  210. return this;
  211. }
  212. public Chart getDesignTimeModel( )
  213. {
  214. return cm;
  215. }
  216.  
  217. public Object getContext( Object key )
  218. {
  219. return contextMap.get( key );
  220. }
  221.  
  222. public Object putContext( Object key, Object value )
  223. {
  224. return contextMap.put( key, value );
  225. }
  226.  
  227. public Object removeContext( Object key )
  228. {
  229. return contextMap.remove( key );
  230. }
  231.  
  232. public Chart getRunTimeModel( )
  233. {
  234. return gcs.getChartModel( );
  235. }
  236.  
  237. public void paint( Graphics g )
  238. {
  239. super.paint( g );
  240. Graphics2D g2d = (Graphics2D) g;
  241. idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, g2d );
  242. idr.setProperty( IDeviceRenderer.UPDATE_NOTIFIER, this );
  243. Dimension d = getSize( );
  244. Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height );
  245. bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution());
  246. Generator gr = Generator.instance( );
  247.  
  248. if ( bNeedsGeneration ) {
  249. bNeedsGeneration = false;
  250. try {
  251. gcs = gr.build( idr.getDisplayServer( ),
  252. cm,
  253. bo,
  254. null,
  255. null,
  256. null );
  257. }
  258. catch ( ChartException ex ) {
  259. System.out.println( ex );
  260. }
  261. }
  262. try {
  263. gr.render( idr, gcs );
  264. }
  265. catch ( ChartException ex ) { System.out.println( ex ); }
  266. }
  267.  
  268. public void componentHidden( ComponentEvent e ) { }
  269. public void componentMoved( ComponentEvent e ) { }
  270. public void componentResized( ComponentEvent e )
  271. {
  272. bNeedsGeneration = true;
  273. }
  274. public void componentShown( ComponentEvent e ) { }
  275.  
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement