|
|
|||
|
|||
|
效果如下图:
如何实现:当然我把鼠标放到某个柱状条上,可以弹出一个信息框,显示该柱状图的数值。比如当我我把鼠标移到粉红条上时,可以显示弹出一个框,显示出25来。其实就是想知道如何为图表增加事件响应。以下是一个例子的代码: public static final Chart createBar( ) { ChartWithAxes cwaBar = ChartWithAxesImpl.create( ); cwaBar.setType( "Bar Chart" ); cwaBar.setSubType( "Side-by-side" ); //$NON-NLS-1$ cwaBar.setUnitSpacing(80); // Plot cwaBar.getBlock( ).setBackground( ColorDefinitionImpl.WHITE( ) ); cwaBar.getBlock( ).getOutline( ).setVisible( true ); Plot p = cwaBar.getPlot( ); p.getClientArea( ).setBackground( ColorDefinitionImpl.create( 255, 255, 225 ) ); // Title cwaBar.getTitle( ) .getLabel( ) .getCaption( ) .setValue( "Side-by-side Bar Chart" ); //$NON-NLS-1$ // Legend Legend lg = cwaBar.getLegend( ); // cwaBar.getLegend( ).setVisible( false ); lg.setItemType( LegendItemType.CATEGORIES_LITERAL ); System.out.println("int--->"+lg.getColumn()); // X-Axis Axis xAxisPrimary = cwaBar.getPrimaryBaseAxes( )[0]; // xAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.BELOW_LITERAL ); // xAxisPrimary.getScale( ).setMin( DateTimeDataElementImpl.create( new CDateTime(2008,1,1) ) ); // xAxisPrimary.getScale( ).setMax( DateTimeDataElementImpl.create( new CDateTime(2009,1,1) ) ); xAxisPrimary.setFormatSpecifier( JavaDateFormatSpecifierImpl.create( "MM/dd/yyyy" ) );//$NON-NLS-1$ xAxisPrimary.setType( AxisType.DATE_TIME_LITERAL ); // xAxisPrimary.getScale().setUnit(ScaleUnitType.DAYS_LITERAL); // xAxisPrimary.getScale().setStepNumber(1); xAxisPrimary.getOrigin( ).setType( IntersectionType.MIN_LITERAL ); xAxisPrimary.getLabel( ) .getCaption( ) .setColor( ColorDefinitionImpl.RED( ) ); xAxisPrimary.getLabel( ).getCaption( ).getFont( ).setRotation( -30 ); xAxisPrimary.getLabel().setVisible(true); // Y-Axis Axis yAxisPrimary = cwaBar.getPrimaryOrthogonalAxis( xAxisPrimary ); yAxisPrimary.getMajorGrid( ).setTickStyle( TickStyle.LEFT_LITERAL ); xAxisPrimary.getMajorGrid( ).getLineAttributes( ).setVisible( true ); yAxisPrimary.setType( AxisType.LINEAR_LITERAL ); yAxisPrimary.getLabel( ).getCaption( ).getFont( ).setRotation( 90 ); yAxisPrimary.getMarkerRanges( ) .add( MarkerRangeImpl.create( yAxisPrimary, NumberDataElementImpl.create( 20 ), // org.eclipse.birt.chart.model.data.impl.TextDataSetImpl NumberDataElementImpl.create( 28 ), ColorDefinitionImpl.GREEN( ).translucent( ) ) ); // Data Set DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // TextDataSet categoryValues = TextDataSetImpl.create( new String[]{ // sdf.parse("2008-02-05"), "2008-02-07", "2008-04-05", "2008-04-05", "2008-07-05"} ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ DateTimeDataSet categoryValues = DateTimeDataSetImpl.create( new Calendar[]{ new CDateTime( 2008, 1, 21 ), new CDateTime( 2008, 1, 20 ), new CDateTime( 2008, 1, 17 ), new CDateTime( 2008, 1, 16 ), new CDateTime( 2008, 3, 15 ) } ); NumberDataSet orthoValues1 = NumberDataSetImpl.create( new double[]{ 25, 35, 15, 5, 20 } ); NumberDataSet orthoValues2 = NumberDataSetImpl.create( new double[]{ 5, 10, 25, 10, 5 } ); // X-Series Series seCategory = SeriesImpl.create( ); seCategory.setDataSet( categoryValues ); SeriesDefinition sdX = SeriesDefinitionImpl.create( ); sdX.getSeriesPalette( ).shift( -1 ); xAxisPrimary.getSeriesDefinitions( ).add( sdX ); sdX.getSeries( ).add( seCategory ); // Y-Series BarSeries bs1 = (BarSeries) BarSeriesImpl.create( ); bs1.setDataSet( orthoValues1 ); // bs1.getLabel( ).setVisible( true ); // bs1.setLabelPosition( Position.INSIDE_LITERAL ); bs1.setTranslucent( true ); bs1.setLabelPosition( Position.INSIDE_LITERAL ); // BarSeries bs2 = (BarSeries) BarSeriesImpl.create( ); // bs2.setDataSet( orthoValues2 ); // bs2.getLabel( ).setVisible( true ); // bs2.setLabelPosition( Position.INSIDE_LITERAL ); SeriesDefinition sdY = SeriesDefinitionImpl.create( ); yAxisPrimary.getSeriesDefinitions( ).add( sdY ); sdY.getSeries( ).add( bs1 ); // sdY.getSeries( ).add( bs2 ); return cwaBar; } 能否帮我修改此例,实现一下当然我把鼠标放到某个柱状条上,可以弹出一个信息框,显示该柱状图的数值的功能。万分感谢!!! |
|
|||
|
引用:
。不知道是哪里出了问题。另外我导入了import org.eclipse.birt.chart.model.data.impl.ActionImpl; 这个包。 |
|
|||
|
万分感谢,不过运行程序的时候报错。
2008-7-23 10:12:10 org.eclipse.birt.chart.util.PluginSettings instance 警告: Exception org.eclipse.birt.core.exception.BirtException: Cant startup the OSGI framework at org.eclipse.birt.core.framework.Platform.startup(Platform.java:91) at org.eclipse.birt.chart.util.PluginSettings.instance(PluginSettings.java:338) at org.eclipse.birt.chart.util.PluginSettings.instance(PluginSettings.java:314) at birt.Regression_117876_svg.<init>(Regression_117876_svg.java:103) at birt.Regression_117876_svg.main(Regression_117876_svg.java:118) Caused by: org.eclipse.birt.core.exception.BirtException: Could not find the Framework - org.eclipse.osgi at org.eclipse.birt.core.framework.osgi.OSGILauncher.startup(OSGILauncher.java:95) at org.eclipse.birt.core.framework.Platform.startup(Platform.java:79) ... 4 more 我现在正在下package org.eclipse.birt.report.test的源码,看看。想问下这个包是干嘛用的。谢谢 |
|
|||
|
我现在知道为什么加了事件以后响应不了。可能和我显示报表的方式有关:
第一种显示方法:(参见:org.eclipse.birt.chart.examples/src/org/eclipse/birt/chart/examples/view/util/ChartPreview.java) public void renderModel( Chart cm ) { // if ( cm == null ) // { // return; // } this.cm = cm; if ( preview != null && !preview.isDisposed( ) ) { updateBuffer( ); preview.redraw( ); } } /** * Generate and render the chart model,the chart image is stored in a * buffer. The buffer will be updated only when the chart model is changed. */ private void updateBuffer( ) { if ( bIsPainting ) { return; } Throwable paintError = null; Rectangle re = preview.getClientArea( ); final Rectangle adjustedRe = new Rectangle( 0, 0, re.width, re.height ); if ( adjustedRe.width - 2 * X_OFFSET <= 0 || adjustedRe.height - 2 * Y_OFFSET <= 0 ) { if ( buffer != null && !buffer.isDisposed( ) ) { buffer.dispose( ); buffer = null; } return; } if ( cm == null ) { buffer = null; } else { bIsPainting = true; Image oldBuffer = null; if ( buffer == null ) { buffer = new Image( Display.getDefault( ), adjustedRe ); } else { Rectangle ore = buffer.getBounds( ); oldBuffer = buffer; if ( !adjustedRe.equals( ore ) ) { buffer = new Image( Display.getDefault( ), adjustedRe ); } } GC gc = new GC( buffer ); // fill default backgournd as white. gc.setForeground( Display.getDefault( ) .getSystemColor( SWT.COLOR_WHITE ) ); gc.fillRectangle( buffer.getBounds( ) ); final Bounds bo = BoundsImpl.create( X_OFFSET, Y_OFFSET, adjustedRe.width - 2 * X_OFFSET, adjustedRe.height - 2 * Y_OFFSET ); IDeviceRenderer deviceRenderer = null; try { deviceRenderer = PluginSettings.instance( ) .getDevice( "dv.SWT" ); //$NON-NLS-1$ deviceRenderer.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, gc ); bo.scale( 72d / deviceRenderer.getDisplayServer( ) .getDpiResolution( ) ); // CONVERT // TO // POINTS // GENERATE AND RENDER THE CHART final Generator gr = Generator.instance( ); RunTimeContext rtc = new RunTimeContext( ); GeneratedChartState state = gr.build( deviceRenderer.getDisplayServer( ), cm, bo, null, rtc, null ); gr.render( deviceRenderer, state ); } catch ( Exception ex ) { paintError = ex; } finally { gc.dispose( ); if ( deviceRenderer != null ) { deviceRenderer.dispose( ); } } if ( paintError != null ) { buffer = oldBuffer; } if ( oldBuffer != null && oldBuffer != buffer ) { oldBuffer.dispose( ); } bIsPainting = false; } } 然后我在我的SWT程序里用如下调用: paintCanvas = new Canvas( parent, SWT.BORDER ); paintCanvas.setLayoutData( new GridData( GridData.FILL_BOTH ) ); paintCanvas.setBackground( Display.getDefault( ) .getSystemColor( SWT.COLOR_WHITE ) ); preview = new ChartPreview( ); paintCanvas.addPaintListener( preview ); paintCanvas.addControlListener( preview ); preview.setPreview( paintCanvas ); preview.renderModel(createBar( )); //createBar( )我要调的图表。用这种方法不能正确显示加了事件的代码。 第二种显示图表的方法:(参见:org.eclipse.birt.chart.examples.api.interactivity.SwtInteractivityViewer) private IDeviceRenderer idr = null; public final void paintControl( PaintEvent e ) { idr = ps.getDevice( "dv.SWT" );//$NON-NLS-1$ idr.setProperty(IDeviceRenderer.UPDATE_NOTIFIER, this ); Rectangle d = this.getClientArea( ); Image imgChart = new Image( this.getDisplay( ), d ); GC gcImage = new GC( imgChart ); idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, gcImage ); Bounds bo = BoundsImpl.create( 0, 0, d.width, d.height ); bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) ); Generator gr = Generator.instance( ); if ( bNeedsGeneration ) { bNeedsGeneration = false; try { gcs = gr.build( idr.getDisplayServer( ), cm, bo, null, null, null ); } catch ( ChartException ce ) { ce.printStackTrace( ); } } try { gr.render( idr, gcs ); GC gc = e.gc; gc.drawImage( imgChart, d.x, d.y ); } catch ( ChartException gex ) { showException( e.gc, gex ); } } 用这种方法可以正确显示图表响应的事件。 ![]() ![]() 现在不明白的是这二种方法显示有何区别。他们分别适用于什么地方。(以上是官方例子里的显示方法)。 |
![]() |
| 主题工具 | 搜索本主题 |
| 显示模式 | |
|
|