1 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 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 68 public static void main( String [] 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 92 AutoDataBindingViewer( ) 93 { 94 final PluginSettings ps = PluginSettings.instance( ); 95 try 96 { 97 idr = ps.getDevice( "dv.SWT" ); } 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 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 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 Series seCategory = SeriesImpl.create( ); 124 Query query = QueryImpl.create( "Items" ); seCategory.getDataDefinition( ).add( query ); 126 127 SeriesDefinition sdX = SeriesDefinitionImpl.create( ); 128 xAxisPrimary.getSeriesDefinitions( ).add( sdX ); 129 sdX.getSeries( ).add( seCategory ); 130 131 BarSeries bs = (BarSeries) BarSeriesImpl.create( ); 133 Query query2 = QueryImpl.create( "Amounts" ); 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 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 [] set = { 160 "Items", "Amounts"}; Object [][] data = { 162 { 163 "A", "B", "C" }, { 165 new Integer ( 7 ), new Integer ( 2 ), new Integer ( 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 |