<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

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

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

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

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

    其中顏色數據可以是ColorDefinitionImpl.RED( ),也可以調用其構造方法輸入三原色創造顏色:ColorDefinitionImpl.create(128, 128, 255)。

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

    以下是利用birt的api畫pie圖的代碼,在Base Series步驟,我改變了默認的顏色,將意大利藍色設置為第一色。
    見代碼第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


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

    Feedback

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

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

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

    2014-01-07 11:20 by lvlv
    請問我想設置每塊的現實數字label的顏色不同可以么?
    主站蜘蛛池模板: 免费又黄又爽又猛大片午夜 | 国产免费的野战视频| 免费人成网站永久| 亚洲精品456人成在线| 亚洲爆乳无码专区| 亚洲日本一区二区一本一道| 成人免费毛片观看| 日本视频一区在线观看免费| a毛片在线看片免费| 午夜在线免费视频| 另类小说亚洲色图| 亚洲AV无码AV男人的天堂不卡| 亚洲人成综合在线播放| 久久精品国产亚洲av麻豆色欲 | 国产免费一级高清淫曰本片| 小说专区亚洲春色校园| 久久久国产亚洲精品| 亚洲伦理一二三四| 亚洲国产综合第一精品小说| 久久精品国产亚洲AV香蕉| 亚洲av无码精品网站| 亚洲AV一宅男色影视| 亚洲精品国产精品乱码视色| 亚洲一区视频在线播放| 亚洲日韩在线第一页| 又黄又大又爽免费视频| 国产一区在线观看免费| 成人爱做日本视频免费| 日韩免费无砖专区2020狼| 黄网址在线永久免费观看| 免费观看毛片视频| 国产成人精品免费直播| 日本无卡码免费一区二区三区| 在线播放高清国语自产拍免费| 毛片a级三毛片免费播放| 免费无码A片一区二三区| 性感美女视频免费网站午夜| 四虎成人精品一区二区免费网站 | 亚洲熟妇AV日韩熟妇在线| 中文字幕亚洲精品无码| 亚洲日韩一区二区一无码|