|
Posted on 2008-01-21 09:54 Raul Gong 阅读(2797) 评论(2) 编辑 收藏 所属分类: eclipse 、 birt
在使用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.65, 21, 75.95, 91.28, 37.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( 45, 150, 225 ),
92 ColorDefinitionImpl.ORANGE( ),
93 ColorDefinitionImpl.CREAM( ),
94 ColorDefinitionImpl.RED( ),
95 ColorDefinitionImpl.GREEN( ),
96 GradientImpl.create( ColorDefinitionImpl.create( 225, 225, 255 ),
97 ColorDefinitionImpl.create( 255, 255, 225 ),
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( 0, 0, 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, 0, 0);
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
这怎么直接运行报错呀,如果new Pei()没有结果呀
# re: birt中改变pie图颜色的方法。 回复 更多评论
2014-01-07 11:20 by
请问我想设置每块的现实数字label的颜色不同可以么?
|