在使用birt構建各種報表的過程中,也許希望自定義輸出的圖形的顏色,例如餅圖的各個切片的顏色。如果使用的報表模板的情況,可以很方便的在制作報表的時候通過birt提供的界面設置;當使用birt提供的api進行報表建立的時候,該怎么做呢?經過一番研究,我終于找到了解決方法。
修改pie中每個切片顏色的方法其實很簡單,在構造pie之中,在"Base Series"步驟中,構造一個顏色數組Fill[] fiaBase,里面存儲顏色,pie使用的時候按照數據順序取出,與切片大小無關。
其中顏色數據可以是ColorDefinitionImpl.RED( ),也可以調用其構造方法輸入三原色創造顏色:ColorDefinitionImpl.create(128, 128, 255)。
實際使用中,可以嘗試在自己封裝的方法中包含這個色彩數組參數。
見代碼第90行。
1
package com.zte.audit.core;
2
3
import org.eclipse.birt.chart.device.IDeviceRenderer;
4
import org.eclipse.birt.chart.factory.GeneratedChartState;
5
import org.eclipse.birt.chart.factory.Generator;
6
import org.eclipse.birt.chart.factory.RunTimeContext;
7
import org.eclipse.birt.chart.model.Chart;
8
import org.eclipse.birt.chart.model.ChartWithoutAxes;
9
import org.eclipse.birt.chart.model.attribute.Bounds;
10
import org.eclipse.birt.chart.model.attribute.ChartDimension;
11
import org.eclipse.birt.chart.model.attribute.Fill;
12
import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl;
13
import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
14
import org.eclipse.birt.chart.model.attribute.impl.GradientImpl;
15
import org.eclipse.birt.chart.model.component.Series;
16
import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
17
import org.eclipse.birt.chart.model.data.BaseSampleData;
18
import org.eclipse.birt.chart.model.data.DataFactory;
19
import org.eclipse.birt.chart.model.data.NumberDataSet;
20
import org.eclipse.birt.chart.model.data.OrthogonalSampleData;
21
import org.eclipse.birt.chart.model.data.SampleData;
22
import org.eclipse.birt.chart.model.data.SeriesDefinition;
23
import org.eclipse.birt.chart.model.data.TextDataSet;
24
import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
25
import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
26
import org.eclipse.birt.chart.model.data.impl.TextDataSetImpl;
27
import org.eclipse.birt.chart.model.impl.ChartWithoutAxesImpl;
28
import org.eclipse.birt.chart.model.layout.Legend;
29
import org.eclipse.birt.chart.model.type.PieSeries;
30
import org.eclipse.birt.chart.model.type.impl.PieSeriesImpl;
31
import org.eclipse.birt.chart.util.PluginSettings;
32
import org.eclipse.swt.SWT;
33
import org.eclipse.swt.events.ControlEvent;
34
import org.eclipse.swt.events.ControlListener;
35
import org.eclipse.swt.events.PaintEvent;
36
import org.eclipse.swt.events.PaintListener;
37
import org.eclipse.swt.graphics.GC;
38
import org.eclipse.swt.graphics.Image;
39
import org.eclipse.swt.graphics.Rectangle;
40
import org.eclipse.swt.layout.FillLayout;
41
import org.eclipse.swt.widgets.Canvas;
42
import org.eclipse.swt.widgets.Display;
43
import org.eclipse.swt.widgets.Shell;
44
45
public 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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

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