posts - 20, comments - 16, trackbacks - 0, articles - 0

          birt中改變pie圖顏色的方法。

          Posted on 2008-01-21 09:54 Raul Gong 閱讀(2846) 評論(2)  編輯  收藏 所屬分類: eclipsebirt

          在使用birt構(gòu)建各種報表的過程中,也許希望自定義輸出的圖形的顏色,例如餅圖的各個切片的顏色。如果使用的報表模板的情況,可以很方便的在制作報表的時候通過birt提供的界面設(shè)置;當使用birt提供的api進行報表建立的時候,該怎么做呢?經(jīng)過一番研究,我終于找到了解決方法。

          修改pie中每個切片顏色的方法其實很簡單,在構(gòu)造pie之中,在"Base Series"步驟中,構(gòu)造一個顏色數(shù)組Fill[] fiaBase,里面存儲顏色,pie使用的時候按照數(shù)據(jù)順序取出,與切片大小無關(guān)。

          其中顏色數(shù)據(jù)可以是ColorDefinitionImpl.RED( ),也可以調(diào)用其構(gòu)造方法輸入三原色創(chuàng)造顏色:ColorDefinitionImpl.create(128, 128, 255)。

          實際使用中,可以嘗試在自己封裝的方法中包含這個色彩數(shù)組參數(shù)。

          以下是利用birt的api畫pie圖的代碼,在Base Series步驟,我改變了默認的顏色,將意大利藍色設(shè)置為第一色。
          見代碼第90行。

            1package com.zte.audit.core;
            2
            3import org.eclipse.birt.chart.device.IDeviceRenderer;
            4import org.eclipse.birt.chart.factory.GeneratedChartState;
            5import org.eclipse.birt.chart.factory.Generator;
            6import org.eclipse.birt.chart.factory.RunTimeContext;
            7import org.eclipse.birt.chart.model.Chart;
            8import org.eclipse.birt.chart.model.ChartWithoutAxes;
            9import org.eclipse.birt.chart.model.attribute.Bounds;
           10import org.eclipse.birt.chart.model.attribute.ChartDimension;
           11import org.eclipse.birt.chart.model.attribute.Fill;
           12import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
           13import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
           14import org.eclipse.birt.chart.model.attribute.impl.GradientImpl;
           15import org.eclipse.birt.chart.model.component.Series;
           16import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
           17import org.eclipse.birt.chart.model.data.BaseSampleData;
           18import org.eclipse.birt.chart.model.data.DataFactory;
           19import org.eclipse.birt.chart.model.data.NumberDataSet;
           20import org.eclipse.birt.chart.model.data.OrthogonalSampleData;
           21import org.eclipse.birt.chart.model.data.SampleData;
           22import org.eclipse.birt.chart.model.data.SeriesDefinition;
           23import org.eclipse.birt.chart.model.data.TextDataSet;
           24import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
           25import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
           26import org.eclipse.birt.chart.model.data.impl.TextDataSetImpl;
           27import org.eclipse.birt.chart.model.impl.ChartWithoutAxesImpl;
           28import org.eclipse.birt.chart.model.layout.Legend;
           29import org.eclipse.birt.chart.model.type.PieSeries;
           30import org.eclipse.birt.chart.model.type.impl.PieSeriesImpl;
           31import org.eclipse.birt.chart.util.PluginSettings;
           32import org.eclipse.swt.SWT;
           33import org.eclipse.swt.events.ControlEvent;
           34import org.eclipse.swt.events.ControlListener;
           35import org.eclipse.swt.events.PaintEvent;
           36import org.eclipse.swt.events.PaintListener;
           37import org.eclipse.swt.graphics.GC;
           38import org.eclipse.swt.graphics.Image;
           39import org.eclipse.swt.graphics.Rectangle;
           40import org.eclipse.swt.layout.FillLayout;
           41import org.eclipse.swt.widgets.Canvas;
           42import org.eclipse.swt.widgets.Display;
           43import org.eclipse.swt.widgets.Shell;
           44
           45public class Pie {
           46    public static final Chart createPie( )
           47    {
           48        ChartWithoutAxes cwoaPie = ChartWithoutAxesImpl.create( );
           49        cwoaPie.setDimension( ChartDimension.TWO_DIMENSIONAL_WITH_DEPTH_LITERAL );
           50        cwoaPie.setType( "Pie Chart" ); //$NON-NLS-1$    
           51        cwoaPie.setSubType( "Standard Pie Chart" ); //$NON-NLS-1$
           52        
           53        // Plot
           54        cwoaPie.setSeriesThickness( 10 );
           55
           56        // Legend
           57        Legend lg = cwoaPie.getLegend( );
           58        lg.getOutline( ).setVisible( true );
           59    
           60        
           61        // Title
           62        cwoaPie.getTitle( ).getLabel( ).getCaption( ).setValue( "Pie Chart" );//$NON-NLS-1$
           63
           64        // Data Set
           65        TextDataSet categoryValues = TextDataSetImpl.create( new String[]{
           66                "New York""Boston""Chicago""San Francisco""Dallas","cs"}
           );//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
           67        NumberDataSet seriesOneValues = NumberDataSetImpl.create( new double[]{
           68                54.652175.9591.2837.43,2.2
           69        }
           );
           70        
           71        SampleData sdata = DataFactory.eINSTANCE.createSampleData( );
           72        BaseSampleData sdBase = DataFactory.eINSTANCE.createBaseSampleData( );
           73        sdBase.setDataSetRepresentation( "" );//$NON-NLS-1$
           74        sdata.getBaseSampleData( ).add( sdBase );
           75
           76        OrthogonalSampleData sdOrthogonal = DataFactory.eINSTANCE.createOrthogonalSampleData( );
           77        sdOrthogonal.setDataSetRepresentation( "" );//$NON-NLS-1$
           78        sdOrthogonal.setSeriesDefinitionIndex( 0 );
           79        sdata.getOrthogonalSampleData( ).add( sdOrthogonal );
           80
           81        cwoaPie.setSampleData( sdata );
           82
           83        // Base Series
           84        Series seCategory = SeriesImpl.create( );
           85        seCategory.setDataSet( categoryValues );
           86
           87        SeriesDefinition sd = SeriesDefinitionImpl.create( );
           88        cwoaPie.getSeriesDefinitions( ).add( sd );
           89        //sd.getSeriesPalette( ).shift( 0 );
           90        final Fill[] fiaBase = {
           91                ColorDefinitionImpl.create( 45150225 ),
           92                ColorDefinitionImpl.ORANGE( ),
           93                ColorDefinitionImpl.CREAM( ),
           94                ColorDefinitionImpl.RED( ),
           95                ColorDefinitionImpl.GREEN( ),
           96                GradientImpl.create( ColorDefinitionImpl.create( 225225255 ),
           97                        ColorDefinitionImpl.create( 255255225 ),
           98                        -35,
           99                        false ),
          100                ColorDefinitionImpl.CYAN( ).darker( ),
          101        }
          ;
          102        sd.getSeriesPalette( ).getEntries( ).clear( );
          103        for ( int i = 0; i < fiaBase.length; i++ )
          104        {
          105            sd.getSeriesPalette( ).getEntries( ).add( fiaBase[i] );
          106        }

          107        
          108        
          109        sd.getSeriesPalette( ).getEntries( );
          110        sd.getSeries( ).add( seCategory );
          111
          112        // Orthogonal Series
          113        PieSeries sePie = (PieSeries) PieSeriesImpl.create( );
          114        sePie.setDataSet( seriesOneValues );
          115        sePie.setSeriesIdentifier( "Cities" );//$NON-NLS-1$ 
          116        sePie.setExplosion( 5 );
          117        
          118        SeriesDefinition sdCity = SeriesDefinitionImpl.create( );
          119        sd.getSeriesDefinitions( ).add( sdCity );
          120        sdCity.getSeries( ).add( sePie );
          121        
          122
          123        return cwoaPie;
          124    }

          125
          126    public static void main(String args[]){
          127        Display display = Display.getDefault();
          128        Shell shell = new Shell(display);
          129        shell.setLayout(new FillLayout());
          130        
          131        final Shell finalShell = shell;
          132        
          133        Image image = null;
          134        
          135        
          136        
          137        Rectangle re = shell.getClientArea( );
          138        final Rectangle adjustedRe = new Rectangle( 00, re.width , re.height  );
          139        image = new Image(display,adjustedRe);
          140        
          141        update(image,re);
          142        
          143        final Image imageFinal = image;
          144        final Canvas canvas = new Canvas(shell,SWT.BORDER);
          145        canvas.addPaintListener(new PaintListener(){
          146
          147            public void paintControl(PaintEvent e) {
          148                e.gc.drawImage(imageFinal, 00);
          149                
          150            }

          151            
          152        }
          );
          153        
          154        canvas.addControlListener(new ControlListener(){
          155
          156            public void controlMoved(ControlEvent e) {
          157            }

          158
          159            public void controlResized(ControlEvent e) {
          160                
          161                //update(imageFinal,adjustedRe);
          162            }

          163            
          164        }
          );
          165        
          166        
          167        shell.open();
          168        
          169        while(!shell.isDisposed()){
          170            if(!display.readAndDispatch()){
          171                display.sleep();
          172            }

          173        }

          174        
          175        display.dispose();
          176    }

          177    
          178    private static int X_OFFSET = 3;
          179
          180    private static int Y_OFFSET = 3;
          181    
          182    public static void update(Image buffer,Rectangle adjustedRe){
          183        GC gc = new GC( buffer );
          184
          185        // fill default backgournd as white.
          186        gc.setForeground( Display.getDefault( )
          187                .getSystemColor( SWT.COLOR_WHITE ) );
          188        gc.fillRectangle( buffer.getBounds( ) );
          189
          190        final Bounds bo = BoundsImpl.create( X_OFFSET,
          191                Y_OFFSET,
          192                adjustedRe.width - 2 * X_OFFSET,
          193                adjustedRe.height - 2 * Y_OFFSET );
          194
          195        IDeviceRenderer deviceRenderer = null;
          196        try
          197        {
          198            deviceRenderer = PluginSettings.instance( )
          199                    .getDevice( "dv.SWT" ); //$NON-NLS-1$
          200            deviceRenderer.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT,
          201                    gc );
          202            bo.scale( 72d / deviceRenderer.getDisplayServer( )
          203                    .getDpiResolution( ) ); // CONVERT
          204            deviceRenderer.getDisplayServer().getObserver();
          205            // TO
          206            // POINTS
          207
          208            // GENERATE AND RENDER THE CHART
          209            final Generator gr = Generator.instance( );
          210            RunTimeContext rtc = new RunTimeContext( );
          211            
          212            Chart cm = createPie();
          213            cm.getBlock().getChildren();
          214            GeneratedChartState state = gr.build( deviceRenderer.getDisplayServer( ),
          215                    cm,
          216                    bo,
          217                    null,
          218                    rtc,
          219                    null );
          220
          221            gr.render( deviceRenderer, state );
          222        }

          223        catch ( Exception ex )
          224        {
          225            ex.printStackTrace();
          226        }

          227        finally
          228        {
          229            gc.dispose( );
          230            if ( deviceRenderer != null )
          231            {
          232                deviceRenderer.dispose( );
          233            }

          234        }

          235    }

          236}

          237


          在需要調(diào)用的地方,例如一個editor中,new Pie() 一下既可。

          Feedback

          # re: birt中改變pie圖顏色的方法。  回復  更多評論   

          2011-12-16 18:26 by xhlieksuccess
          這怎么直接運行報錯呀,如果new Pei()沒有結(jié)果呀

          # re: birt中改變pie圖顏色的方法。  回復  更多評論   

          2014-01-07 11:20 by lvlv
          請問我想設(shè)置每塊的現(xiàn)實數(shù)字label的顏色不同可以么?
          主站蜘蛛池模板: 锦州市| 宿迁市| 新野县| 潜江市| 南丰县| 安塞县| 盐城市| 郓城县| 乐都县| 怀安县| 兴城市| 天长市| 岱山县| 卢氏县| 东至县| 蒙城县| 阳高县| 乌苏市| 宜良县| 沁阳市| 盖州市| 巴青县| 鲁山县| 香港 | 崇明县| 牟定县| 武汉市| 香港| 陈巴尔虎旗| 平定县| 德州市| 怀安县| 磐安县| 易门县| 湘西| 二连浩特市| 井研县| 罗平县| 陇南市| 敖汉旗| 客服|