Révision 3176

tmp/org.txm.groovy.core/src/groovy/org/txm/macro/prototypes/stats/AFCWithStylesMacro.groovy (revision 3176)
1
// STANDARD DECLARATIONS
2
// @author mdecorde sjacquot sheiden
3
package org.txm.macro
4

  
5
import org.kohsuke.args4j.*
6
import groovy.transform.Field
7
import org.txm.rcpapplication.swt.widget.parameters.*
8
import org.txm.searchengine.cqp.clientExceptions.*
9
import org.txm.searchengine.cqp.corpus.*
10
import org.txm.searchengine.cqp.corpus.query.*
11
import org.apache.commons.lang.time.StopWatch
12
import java.util.Arrays
13
import org.jfree.chart.renderer.xy.*
14
import org.jfree.chart.renderer.*
15
import org.jfree.chart.plot.*
16
import org.jfree.data.xy.*
17
import org.jfree.chart.axis.*
18
import java.awt.*;
19
import java.awt.geom.*;
20
import org.jfree.chart.labels.*
21

  
22
import org.txm.ca.core.functions.CA
23
import org.txm.ca.core.chartsengine.jfreechart.themes.highcharts.renderers.*
24
import org.txm.ca.rcp.editors.*
25
import org.txm.libs.office.ReadODS
26
import org.txm.ca.core.chartsengine.jfreechart.datasets.*
27
import org.jfree.chart.renderer.AbstractRenderer
28

  
29
println "editor: "+editor
30

  
31
if (!(editor instanceof CAEditor)) {
32
	println "editor is not a CA editor: $editor, Run the macro with F12 when the editor is selected :-)"
33
	return
34
}
35

  
36
@Field @Option(name="patternsODSFile", usage="The starting word", widget="FileOpen", required=true, def='patterns.ods')
37
def patternsODSFile
38
@Field @Option(name="debug", usage="Show internal variable content", widget="StringArray", metaVar="OFF	ON	ALL	REALLY ALL", required=true, def="OFF")
39
debug
40

  
41
// Open the parameters input dialog box
42
if (!ParametersDialog.open(this)) return;
43
if (debug == "OFF") debug = 0; else if (debug == "ON") debug = 1; else if (debug == "ALL") debug = 2 else if (debug == "REALLY ALL") debug = 3
44

  
45
if (!patternsODSFile.exists()) {
46
	println "Pattern file not found: $patternsODSFile"
47
	return false;
48
}
49
if (!patternsODSFile.isFile() || !patternsODSFile.getName().toLowerCase().endsWith(".ods")) {
50
	println "Wrong pattern file: $patternsODSFile"
51
	return false;
52
}
53

  
54
def data = ReadODS.toTable(patternsODSFile, "rows")
55
def keys = data[0]
56
		def row_styles = [:] // reformat data
57
				for (int i = 1 ; i < data.size() ; i++) {
58
					def h = data[i]
59
							def style = [:] // create style entry
60
									String s = h[0];
61
					row_styles[/$s/] = style // with a regexp pattern as key
62
							
63
							// fill the style
64
							for (int j = 1 ; j < h.size() ; j++) {
65
								style[keys[j]] = h[j]
66
							}
67
				}
68
if (debug > 0) {
69
	println "ROW STYLES=$row_styles"
70
}
71

  
72
data = ReadODS.toTable(patternsODSFile, "cols")
73
keys = data[0]
74
def col_styles = [:] // reformat data
75

  
76
for (int i = 1 ; i < data.size() ; i++) {
77
		def h = data[i]
78
							def style = [:] // create style entry
79
									String s = h[0];
80
					col_styles[/$s/] = style // with a regexp pattern as key
81
							
82
							// fill the style
83
							for (int j = 1 ; j < h.size() ; j++) {
84
								style[keys[j]] = h[j]
85
							}
86
				}
87
if (debug > 0) {
88
	println "COL STYLES=$col_styles"
89
}
90

  
91
ica = editor.getCA()
92
ca = ica.getCA()
93

  
94
// http://txm.sourceforge.net/javadoc/TXM/TBX/org/txm/stat/engine/r/function/CA.html
95

  
96
// SOME DATA
97
rows = ica.getRowNames()
98
rowsinfo = ica.getRowInfos()
99
rowscos2 = ca.getRowCos2()
100
cols = ica.getColNames()
101
colssinfo = ica.getColInfos()
102
colscos2 = ca.getColCos2()
103
D1 = ica.getFirstDimension() -1;
104
D2 = ica.getSecondDimension() -1;
105

  
106
shapes = [:]
107
shapes["square"] = AbstractRenderer.DEFAULT_SHAPE;
108
shapes["circle"] = AbstractRenderer.DEFAULT_SHAPE;
109
shapes["triangle"] = AbstractRenderer.DEFAULT_SHAPE;
110
shapes["star"] = AbstractRenderer.DEFAULT_SHAPE;
111
shapes["disk"] = AbstractRenderer.DEFAULT_SHAPE;
112

  
113
// styles per col index in dataset
114
// set col label
115
colLabelStyle = new HashMap<Integer, String>();
116
// set col visibility
117
colHiddenStyle = new HashSet<Integer>(); // true false
118
// set col font size
119
colFontSizeStyle = new HashMap<Integer, Integer>(); // 0.0 to 10.0
120
// set col font color
121
colFontColorStyle = new HashMap<Integer, Color>(); // 0.0 to 10.0
122
// set col font family
123
colFontFamilyStyle = new HashMap<Integer, String>(); // 0.0 to 10.0
124
// set col points size
125
colPointSizeStyle = new HashMap<Integer, Double>(); // 0.0 to 10.0
126
// set col points RGBA color
127
colPointColorStyle = new HashMap<Integer, Color>();
128
// set col points shape (circle, square, triangle, ... + color ? + size ?) -> expert
129
colPointShapeStyle = new HashMap<Integer, Shape>(); // circle, square, triangle, etc.
130
// set col font style (1 = bold, 2 = italic, 3 = bold + italic)
131
colFontStyleStyle = new HashMap<Integer, Integer>();
132

  
133

  
134
// set row label
135
rowLabelStyle = new HashMap<Integer, String>();
136
// set row visibility
137
rowHiddenStyle = new HashSet<Integer>(); // true false
138
// set row font size
139
rowFontSizeStyle = new HashMap<Integer, Integer>(); // 0.0 to 10.0
140
// set row font size
141
rowFontColorStyle = new HashMap<Integer, Color>(); // 0.0 to 10.0
142
// set row font size
143
rowFontFamilyStyle = new HashMap<Integer, String>(); // 0.0 to 10.0
144
// set row points size
145
rowPointSizeStyle = new HashMap<Integer, Double>(); // 0.0 to 10.0
146
// set row points RGBA color
147
rowPointColorStyle = new HashMap<Integer, Color>();
148
// set row points shape (circle, square, triangle, ... + color ? + size ?) -> expert
149
rowPointShapeStyle = new HashMap<Integer, Shape>(); // circle, square, triangle, etc.
150
// set row font style (1 = bold, 2 = italic, 3 = bold + italic)
151
rowFontStyleStyle = new HashMap<Integer, Integer>();
152

  
153
// prepare col style data for the dataset
154
for (int i = 0 ; i < cols.length ; i++) {
155
	for (def p : col_styles.keySet()) {
156
		if (cols[i] ==~ p) {
157
			def style = col_styles[p]
158
			if (style["label"] != null && style["label"].length() > 0) {
159
				colLabelStyle[i] = style["label"]
160
			}
161
			if (style["hidden"] != null && style["hidden"].toUpperCase() == "T") {
162
				colHiddenStyle << i
163
			}
164
			if (style["point-size"] != null && style["point-size"].length() > 0) {
165
				colPointSizeStyle[i] = Double.parseDouble(style["point-size"])
166
			}
167
			if (style["point-color"] != null && style["point-color"].length() > 0 ) {
168
				colPointColorStyle[i] = rgbaStringToColor(style["point-color"])
169
			}
170
			if (style["point-shape"] != null && shapes.containsKey(style["point-shape"])) {
171
				colPointShapeStyle[i] = shapes[style["point-shape"]]
172
			}
173
			if (style["font-size"] != null && style["font-size"].length() > 0) {
174
				colFontSizeStyle[i] = Integer.parseInt(style["font-size"])
175
			}
176
			if (style["font-color"] != null && style["font-color"].length() > 0) {
177
				colFontColorStyle[i] = rgbaStringToColor(style["font-color"])
178
			}
179
			if (style["font-family"] != null && style["font-family"].length() > 0) {
180
				colFontFamilyStyle[i] = style["font-family"]
181
			}
182
			if (style["font-style"] != null && style["font-style"].length() > 0) {
183
				colFontStyleStyle[i] = Integer.parseInt(style["font-style"])
184
			}
185
			
186
		}
187
	}
188
}
189
if (debug > 0) {
190
	println "COL COL=$colPointColorStyle"
191
	println "COL SHP=$colPointShapeStyle"
192
	println "COL LAB=$colLabelStyle"
193
	println "COL FONT-SIZ=$colFontSizeStyle"
194
	println "COL SIZ=$colPointSizeStyle"
195
	println "COL VIZ=$colHiddenStyle"
196
	println "COL STYLE=$colFontStyleStyle"
197
}
198

  
199
// prepare row style data for the dataset
200
for (int i = 0 ; i < rows.length ; i++) {
201
	for (def p : row_styles.keySet()) {
202
		if (rows[i] ==~ p) {
203
			def style = row_styles[p]
204
			if (style["hidden"] != null && style["hidden"].toUpperCase() == "T") {
205
				rowHiddenStyle << i
206
			}
207
			if (style["label"] != null && style["label"].length() > 0) {
208
				rowLabelStyle[i] = style["label"]
209
			}
210
			if (style["point-size"] != null && style["point-size"].length() > 0) {
211
				rowPointSizeStyle[i] = Double.parseDouble(style["point-size"])
212
			}
213
			if (style["point-color"] != null && style["point-color"].length() > 0 ) {
214
				rowPointColorStyle[i] = rgbaStringToColor(style["point-color"])
215
			}
216
			if (style["point-shape"] != null && shapes.containsKey(style["point-shape"])) {
217
				rowPointShapeStyle[i] = shapes[style["point-shape"]]
218
			}
219
			if (style["font-size"] != null && style["font-size"].length() > 0) {
220
				rowFontSizeStyle[i] = Integer.parseInt(style["font-size"])
221
			}
222
			if (style["font-color"] != null && style["font-color"].length() > 0) {
223
				rowFontColorStyle[i] = rgbaStringToColor(style["font-color"])
224
			}
225
			if (style["font-family"] != null && style["font-family"].length() > 0) {
226
				rowFontFamilyStyle[i] = style["font-family"]
227
			}
228
			if (style["font-style"] != null && style["font-style"].length() > 0) {
229
				rowFontStyleStyle[i] = Integer.parseInt(style["font-style"])
230
			}
231
		}
232
	}
233
}
234
if (debug > 0) {
235
	println "ROW COL=$rowPointColorStyle"
236
	println "ROW SHP=$rowPointShapeStyle"
237
	println "ROW LAB=$rowLabelStyle"
238
	println "ROW FONT-SIZ=$rowFontSizeStyle"
239
	println "ROW SIZ=$rowPointSizeStyle"
240
	println "ROW VIZ=$rowHiddenStyle"
241
	println "ROW STYLE=$rowFontStyleStyle"
242
}
243

  
244

  
245
// UPDATE THE CHART RENDERER
246

  
247
def chartEditor = editor.getEditors()[0]
248
		def chartComposite = chartEditor.getComposite()
249
		
250
		ddebug = debug
251
		monitor.syncExec( new Runnable() {
252
			public void run() {
253
				println chartComposite
254
				def chart = chartEditor.getChart()
255
				
256
				/*
257
				println "chart: "+chart
258
				println "Plot: "+chart.getPlot()
259
				println "Dataset: "+chart.getPlot().getDataset()
260
				println "Renderer 1: "+chart.getPlot().getRenderer()
261
				 */
262
				
263
				def renderer = new CAItemSelectionRenderer(ica, chart) {
264
					Area EMPTYAREA = new Area()
265
							
266
							@Override
267
							public void initItemLabelGenerator() {
268
							XYItemLabelGenerator generator = new XYItemLabelGenerator() {
269
							
270
							@Override
271
							public String generateLabel(XYDataset dataset, int series, int item) {
272
								if (series == 0 && rowHiddenStyle.contains(item)) {
273
									if (ddebug > 1) println "HIDDING ROW LABEL '$item' '${rows[item]}'"
274
									return ""
275
								} else if (series == 1 && colHiddenStyle.contains(item)) {
276
									if (ddebug > 1) println "HIDDING COL LABEL '$item' '${cols[item]}'"
277
									return ""
278
								} else if (series == 0 && rowLabelStyle.containsKey(item)) {
279
									if (ddebug > 1) println "RENAME ROW LABEL '$item' '${rows[item]}' -> '${rowLabelStyle[item]}'"
280
									return rowLabelStyle[item]
281
								} else if (series == 1 && colLabelStyle.containsKey(item)) {
282
									if (ddebug > 1) println "RENAME COL LABEL '$item' '${cols[item]}' -> '${colLabelStyle[item]}'"
283
									return colLabelStyle[item]
284
								} else {
285
									CAXYDataset caDataset = (CAXYDataset) dataset;
286
									return caDataset.getLabel(series, item);
287
								}
288
							}
289
						};
290
						
291
						// don't use setBaseItemLabelGenerator BUT setDefaultItemLabelGenerator
292
						this.setDefaultItemLabelGenerator(generator);
293
						this.setDefaultItemLabelsVisible(true);
294
					}
295
					
296
					
297
					
298
					@Override
299
					public Font getItemLabelFont(int series, int item) {
300
						Font d = super.getItemLabelFont(series, item);
301
						Integer style = d.getStyle();
302
						Integer size = d.getSize();
303
						String family = d.getFontName();
304
						
305
						// size
306
						if (series == 0 && rowFontSizeStyle.containsKey(item)) {
307
							if (ddebug > 1) println "FIX ROW FONT-SIZE $item ${rows[item]} -> ${rowFontSizeStyle[item]}"
308
							size = rowFontSizeStyle[item];
309
						} else if (series == 1 && colFontSizeStyle.containsKey(item)) {
310
							if (ddebug > 1) println "FIX COL FONT-SIZE $item ${cols[item]} -> ${colFontSizeStyle[item]}"
311
							size = colFontSizeStyle[item];
312
						}
313
						
314
						// family
315
						if (series == 0 && rowFontFamilyStyle.containsKey(item)) {
316
							if (ddebug > 1) println "FIX ROW FONT-Family $item ${rows[item]} -> ${rowFontFamilyStyle[item]}"
317
							family = rowFontFamilyStyle[item];
318
						} else if (series == 1 && colFontFamilyStyle.containsKey(item)) {
319
							if (ddebug > 1) println "FIX COL FONT-Family $item ${cols[item]} -> ${colFontFamilyStyle[item]}"
320
							family = colFontFamilyStyle[item];
321
						}
322
						
323
						// style
324
						if (series == 0 && rowFontStyleStyle.containsKey(item)) {
325
							if (ddebug > 1) println "FIX ROW FONT-Style $item ${rows[item]} -> ${rowFontStyleStyle[item]}"
326
							style = rowFontStyleStyle[item];
327
						} else if (series == 1 && colFontFamilyStyle.containsKey(item)) {
328
							if (ddebug > 1) println "FIX COL FONT-Style $item ${cols[item]} -> ${colFontStyleStyle[item]}"
329
							style = colFontStyleStyle[item];
330
						}
331
						
332
						
333
						return new Font(family, style, size);
334
					}
335
					
336
					
337
					@Override
338
					public Shape getItemShape(int series, int item) {
339
						// Rows (series == 0), Cols (series == 1)
340
						if (series == 0 && rowHiddenStyle.contains(item)) {
341
							return EMPTYAREA;
342
						} else if (series == 1 && colHiddenStyle.contains(item)) {
343
							return EMPTYAREA;
344
						} else {
345
							
346
							Shape shape =  super.getItemShape(series, item);
347
							
348
							// not-visible shapes mode
349
							if(!((CA) this.multipleItemsSelector.getResult()).isShowPointShapes())        {
350
								return shape;
351
							}
352
							
353
							AffineTransform t = new AffineTransform();
354
							if (series == 0 && rowPointSizeStyle.containsKey(item)) {
355
								if (ddebug > 1) println "FIX ROW POINT SIZE $item ${rows[item]} -> ${rowPointSizeStyle[item]}"
356
								t.setToScale(rowPointSizeStyle.get(item), rowPointSizeStyle.get(item));
357
								shape = t.createTransformedShape(shape);
358
							} else if (series == 1 && colPointSizeStyle.containsKey(item)) {
359
								if (ddebug > 1) println "FIX COL POINT SIZE $item ${cols[item]} -> ${colPointSizeStyle[item]}"
360
								t.setToScale(colPointSizeStyle.get(item), colPointSizeStyle.get(item));
361
								shape = t.createTransformedShape(shape);
362
							}
363
							
364
							return shape;
365
						}
366
					}
367
					
368
					
369
					@Override
370
					public Paint getItemPaint(int series, int item) {
371
						
372
						// visible shapes mode
373
						if (!((CA) this.multipleItemsSelector.getResult()).isShowPointShapes()) {
374
							return super.getItemPaint(series, item);
375
						}
376
						
377
						if (series == 0 && rowPointColorStyle.containsKey(item)) {
378
							if (ddebug > 1) println "FIX ROW POINT COLOR $item ${rows[item]} -> ${rowPointColorStyle[item]}"
379
							return rowPointColorStyle.get(item);
380
						} else if (series == 1 && colPointColorStyle.containsKey(item)) {
381
							if (ddebug > 1) println "FIX COL POINT COLOR $item ${cols[item]} -> ${colPointColorStyle[item]}"
382
							return colPointColorStyle.get(item);
383
						}
384
						else {
385
							return super.getItemPaint(series, item);
386
						}
387
					}
388
					
389
					
390
					@Override
391
					public Paint getItemLabelPaint(int series, int item) {
392
						if (series == 0 && rowFontColorStyle.containsKey(item)) {
393
							if (ddebug > 1) println "FIX ROW LABEL COLOR $item ${rows[item]} -> ${rowFontColorStyle[item]}"
394
							return rowFontColorStyle.get(item);
395
						} else if (series == 1 && colFontColorStyle.containsKey(item)) {
396
							if (ddebug > 1) println "FIX COL LABEL COLOR $item ${cols[item]} -> ${colFontColorStyle[item]}"
397
							return colFontColorStyle.get(item);
398
						} else {
399
							return super.getItemLabelPaint(series, item);
400
						}
401
					}
402
					
403
					
404
					
405
				};
406
				
407
				
408
				def cp = editor.editors[0].getChart();
409
				renderer.chart = cp
410
						renderer.setSeriesVisible(0, ica.isShowVariables()); // Rows
411
				renderer.setSeriesVisible(1, ica.isShowIndividuals()); // Columns
412
				chart.getXYPlot().setRenderer(renderer)
413
				
414
				ica.getChartCreator().updateChart(chartEditor.getResult())
415
				
416
			}
417
		});
418

  
419

  
420

  
421

  
422
// creates a Color object from the specified RGB or RGBA string representation separated by spaces ("R G B" or "R G B A") from 0 to 255 for each channel
423
def rgbaStringToColor(String color) {
424
	String[] rgbColor = color.split(" ");
425
	String alpha = "255";
426
	if(rgbColor.length > 2) {
427
		if(rgbColor.length > 3) {
428
			alpha = rgbColor[3];
429
		}
430
		return new Color(Integer.parseInt(rgbColor[0]), Integer.parseInt(rgbColor[1]), Integer.parseInt(rgbColor[2]), Integer.parseInt(alpha))
431
	}
432
	else {
433
		println "Error in color format for string: $color"
434
	}
435
	
436
}
437

  
438

  
tmp/org.txm.groovy.core/src/groovy/org/txm/macro/prototypes/stats/CAStyleMacro.groovy (revision 3176)
1
// STANDARD DECLARATIONS
2
// @author mdecorde sjacquot sheiden
3
package org.txm.macro
4

  
5
import org.kohsuke.args4j.*
6
import groovy.transform.Field
7
import org.txm.rcpapplication.swt.widget.parameters.*
8
import org.txm.searchengine.cqp.clientExceptions.*
9
import org.txm.searchengine.cqp.corpus.*
10
import org.txm.searchengine.cqp.corpus.query.*
11
import org.apache.commons.lang.time.StopWatch
12
import java.util.Arrays
13
import org.jfree.chart.renderer.xy.*
14
import org.jfree.chart.renderer.*
15
import org.jfree.chart.plot.*
16
import org.jfree.data.xy.*
17
import org.jfree.chart.axis.*
18
import java.awt.*;
19
import java.awt.geom.*;
20
import org.jfree.chart.labels.*
21

  
22
import org.txm.ca.core.functions.CA
23
import org.txm.ca.core.chartsengine.jfreechart.themes.highcharts.renderers.*
24
import org.txm.ca.rcp.editors.*
25
import org.txm.libs.office.ReadODS
26
import org.txm.ca.core.chartsengine.jfreechart.datasets.*
27
import org.jfree.chart.renderer.AbstractRenderer
28

  
29
println "editor: "+editor
30

  
31
if (!(editor instanceof CAEditor)) {
32
	println "editor is not a CA editor: $editor, Run the macro with F12 when the editor is selected :-)"
33
	return
34
}
35

  
36
@Field @Option(name="patternsODSFile", usage="The starting word", widget="FileOpen", required=true, def='patterns.ods')
37
def patternsODSFile
38
@Field @Option(name="debug", usage="Show internal variable content", widget="StringArray", metaVar="OFF	ON	ALL	REALLY ALL", required=true, def="OFF")
39
debug
40

  
41
// Open the parameters input dialog box
42
if (!ParametersDialog.open(this)) return;
43
if (debug == "OFF") debug = 0; else if (debug == "ON") debug = 1; else if (debug == "ALL") debug = 2 else if (debug == "REALLY ALL") debug = 3
44

  
45
if (!patternsODSFile.exists()) {
46
	println "Pattern file not found: $patternsODSFile"
47
	return false;
48
}
49
if (!patternsODSFile.isFile() || !patternsODSFile.getName().toLowerCase().endsWith(".ods")) {
50
	println "Wrong pattern file: $patternsODSFile"
51
	return false;
52
}
53

  
54
def data = ReadODS.toTable(patternsODSFile, "rows")
55
def keys = data[0]
56
		def row_styles = [:] // reformat data
57
				for (int i = 1 ; i < data.size() ; i++) {
58
					def h = data[i]
59
							def style = [:] // create style entry
60
									String s = h[0];
61
					row_styles[/$s/] = style // with a regexp pattern as key
62
							
63
							// fill the style
64
							for (int j = 1 ; j < h.size() ; j++) {
65
								style[keys[j]] = h[j]
66
							}
67
				}
68
if (debug > 0) {
69
	println "ROW STYLES=$row_styles"
70
}
71

  
72
data = ReadODS.toTable(patternsODSFile, "cols")
73
keys = data[0]
74
def col_styles = [:] // reformat data
75

  
76
for (int i = 1 ; i < data.size() ; i++) {
77
		def h = data[i]
78
							def style = [:] // create style entry
79
									String s = h[0];
80
					col_styles[/$s/] = style // with a regexp pattern as key
81
							
82
							// fill the style
83
							for (int j = 1 ; j < h.size() ; j++) {
84
								style[keys[j]] = h[j]
85
							}
86
				}
87
if (debug > 0) {
88
	println "COL STYLES=$col_styles"
89
}
90

  
91
ica = editor.getCA()
92
ca = ica.getCA()
93

  
94
// http://txm.sourceforge.net/javadoc/TXM/TBX/org/txm/stat/engine/r/function/CA.html
95

  
96
// SOME DATA
97
rows = ica.getRowNames()
98
rowsinfo = ica.getRowInfos()
99
rowscos2 = ca.getRowCos2()
100
cols = ica.getColNames()
101
colssinfo = ica.getColInfos()
102
colscos2 = ca.getColCos2()
103
D1 = ica.getFirstDimension() -1;
104
D2 = ica.getSecondDimension() -1;
105

  
106
shapes = [:]
107
shapes["square"] = AbstractRenderer.DEFAULT_SHAPE;
108
shapes["circle"] = AbstractRenderer.DEFAULT_SHAPE;
109
shapes["triangle"] = AbstractRenderer.DEFAULT_SHAPE;
110
shapes["star"] = AbstractRenderer.DEFAULT_SHAPE;
111
shapes["disk"] = AbstractRenderer.DEFAULT_SHAPE;
112

  
113
// styles per col index in dataset
114
// set col label
115
colLabelStyle = new HashMap<Integer, String>();
116
// set col visibility
117
colHiddenStyle = new HashSet<Integer>(); // true false
118
// set col font size
119
colFontSizeStyle = new HashMap<Integer, Integer>(); // 0.0 to 10.0
120
// set col font color
121
colFontColorStyle = new HashMap<Integer, Color>(); // 0.0 to 10.0
122
// set col font family
123
colFontFamilyStyle = new HashMap<Integer, String>(); // 0.0 to 10.0
124
// set col points size
125
colPointSizeStyle = new HashMap<Integer, Double>(); // 0.0 to 10.0
126
// set col points RGBA color
127
colPointColorStyle = new HashMap<Integer, Color>();
128
// set col points shape (circle, square, triangle, ... + color ? + size ?) -> expert
129
colPointShapeStyle = new HashMap<Integer, Shape>(); // circle, square, triangle, etc.
130
// set col font style (1 = bold, 2 = italic, 3 = bold + italic)
131
colFontStyleStyle = new HashMap<Integer, Integer>();
132

  
133

  
134
// set row label
135
rowLabelStyle = new HashMap<Integer, String>();
136
// set row visibility
137
rowHiddenStyle = new HashSet<Integer>(); // true false
138
// set row font size
139
rowFontSizeStyle = new HashMap<Integer, Integer>(); // 0.0 to 10.0
140
// set row font size
141
rowFontColorStyle = new HashMap<Integer, Color>(); // 0.0 to 10.0
142
// set row font size
143
rowFontFamilyStyle = new HashMap<Integer, String>(); // 0.0 to 10.0
144
// set row points size
145
rowPointSizeStyle = new HashMap<Integer, Double>(); // 0.0 to 10.0
146
// set row points RGBA color
147
rowPointColorStyle = new HashMap<Integer, Color>();
148
// set row points shape (circle, square, triangle, ... + color ? + size ?) -> expert
149
rowPointShapeStyle = new HashMap<Integer, Shape>(); // circle, square, triangle, etc.
150
// set row font style (1 = bold, 2 = italic, 3 = bold + italic)
151
rowFontStyleStyle = new HashMap<Integer, Integer>();
152

  
153
// prepare col style data for the dataset
154
for (int i = 0 ; i < cols.length ; i++) {
155
	for (def p : col_styles.keySet()) {
156
		if (cols[i] ==~ p) {
157
			def style = col_styles[p]
158
			if (style["label"] != null && style["label"].length() > 0) {
159
				colLabelStyle[i] = style["label"]
160
			}
161
			if (style["hidden"] != null && style["hidden"].toUpperCase() == "T") {
162
				colHiddenStyle << i
163
			}
164
			if (style["point-size"] != null && style["point-size"].length() > 0) {
165
				colPointSizeStyle[i] = Double.parseDouble(style["point-size"])
166
			}
167
			if (style["point-color"] != null && style["point-color"].length() > 0 ) {
168
				colPointColorStyle[i] = rgbaStringToColor(style["point-color"])
169
			}
170
			if (style["point-shape"] != null && shapes.containsKey(style["point-shape"])) {
171
				colPointShapeStyle[i] = shapes[style["point-shape"]]
172
			}
173
			if (style["font-size"] != null && style["font-size"].length() > 0) {
174
				colFontSizeStyle[i] = Integer.parseInt(style["font-size"])
175
			}
176
			if (style["font-color"] != null && style["font-color"].length() > 0) {
177
				colFontColorStyle[i] = rgbaStringToColor(style["font-color"])
178
			}
179
			if (style["font-family"] != null && style["font-family"].length() > 0) {
180
				colFontFamilyStyle[i] = style["font-family"]
181
			}
182
			if (style["font-style"] != null && style["font-style"].length() > 0) {
183
				colFontStyleStyle[i] = Integer.parseInt(style["font-style"])
184
			}
185
			
186
		}
187
	}
188
}
189
if (debug > 0) {
190
	println "COL COL=$colPointColorStyle"
191
	println "COL SHP=$colPointShapeStyle"
192
	println "COL LAB=$colLabelStyle"
193
	println "COL FONT-SIZ=$colFontSizeStyle"
194
	println "COL SIZ=$colPointSizeStyle"
195
	println "COL VIZ=$colHiddenStyle"
196
	println "COL STYLE=$colFontStyleStyle"
197
}
198

  
199
// prepare row style data for the dataset
200
for (int i = 0 ; i < rows.length ; i++) {
201
	for (def p : row_styles.keySet()) {
202
		if (rows[i] ==~ p) {
203
			def style = row_styles[p]
204
			if (style["hidden"] != null && style["hidden"].toUpperCase() == "T") {
205
				rowHiddenStyle << i
206
			}
207
			if (style["label"] != null && style["label"].length() > 0) {
208
				rowLabelStyle[i] = style["label"]
209
			}
210
			if (style["point-size"] != null && style["point-size"].length() > 0) {
211
				rowPointSizeStyle[i] = Double.parseDouble(style["point-size"])
212
			}
213
			if (style["point-color"] != null && style["point-color"].length() > 0 ) {
214
				rowPointColorStyle[i] = rgbaStringToColor(style["point-color"])
215
			}
216
			if (style["point-shape"] != null && shapes.containsKey(style["point-shape"])) {
217
				rowPointShapeStyle[i] = shapes[style["point-shape"]]
218
			}
219
			if (style["font-size"] != null && style["font-size"].length() > 0) {
220
				rowFontSizeStyle[i] = Integer.parseInt(style["font-size"])
221
			}
222
			if (style["font-color"] != null && style["font-color"].length() > 0) {
223
				rowFontColorStyle[i] = rgbaStringToColor(style["font-color"])
224
			}
225
			if (style["font-family"] != null && style["font-family"].length() > 0) {
226
				rowFontFamilyStyle[i] = style["font-family"]
227
			}
228
			if (style["font-style"] != null && style["font-style"].length() > 0) {
229
				rowFontStyleStyle[i] = Integer.parseInt(style["font-style"])
230
			}
231
		}
232
	}
233
}
234
if (debug > 0) {
235
	println "ROW COL=$rowPointColorStyle"
236
	println "ROW SHP=$rowPointShapeStyle"
237
	println "ROW LAB=$rowLabelStyle"
238
	println "ROW FONT-SIZ=$rowFontSizeStyle"
239
	println "ROW SIZ=$rowPointSizeStyle"
240
	println "ROW VIZ=$rowHiddenStyle"
241
	println "ROW STYLE=$rowFontStyleStyle"
242
}
243

  
244

  
245
// UPDATE THE CHART RENDERER
246

  
247
def chartEditor = editor.getEditors()[0]
248
		def chartComposite = chartEditor.getComposite()
249
		
250
		ddebug = debug
251
		monitor.syncExec( new Runnable() {
252
			public void run() {
253
				println chartComposite
254
				def chart = chartEditor.getChart()
255
				
256
				/*
257
				println "chart: "+chart
258
				println "Plot: "+chart.getPlot()
259
				println "Dataset: "+chart.getPlot().getDataset()
260
				println "Renderer 1: "+chart.getPlot().getRenderer()
261
				 */
262
				
263
				def renderer = new CAItemSelectionRenderer(ica, chart) {
264
					Area EMPTYAREA = new Area()
265
							
266
							@Override
267
							public void initItemLabelGenerator() {
268
							XYItemLabelGenerator generator = new XYItemLabelGenerator() {
269
							
270
							@Override
271
							public String generateLabel(XYDataset dataset, int series, int item) {
272
								if (series == 0 && rowHiddenStyle.contains(item)) {
273
									if (ddebug > 1) println "HIDDING ROW LABEL '$item' '${rows[item]}'"
274
									return ""
275
								} else if (series == 1 && colHiddenStyle.contains(item)) {
276
									if (ddebug > 1) println "HIDDING COL LABEL '$item' '${cols[item]}'"
277
									return ""
278
								} else if (series == 0 && rowLabelStyle.containsKey(item)) {
279
									if (ddebug > 1) println "RENAME ROW LABEL '$item' '${rows[item]}' -> '${rowLabelStyle[item]}'"
280
									return rowLabelStyle[item]
281
								} else if (series == 1 && colLabelStyle.containsKey(item)) {
282
									if (ddebug > 1) println "RENAME COL LABEL '$item' '${cols[item]}' -> '${colLabelStyle[item]}'"
283
									return colLabelStyle[item]
284
								} else {
285
									CAXYDataset caDataset = (CAXYDataset) dataset;
286
									return caDataset.getLabel(series, item);
287
								}
288
							}
289
						};
290
						
291
						// don't use setBaseItemLabelGenerator BUT setDefaultItemLabelGenerator
292
						this.setDefaultItemLabelGenerator(generator);
293
						this.setDefaultItemLabelsVisible(true);
294
					}
295
					
296
					
297
					
298
					@Override
299
					public Font getItemLabelFont(int series, int item) {
300
						Font d = super.getItemLabelFont(series, item);
301
						Integer style = d.getStyle();
302
						Integer size = d.getSize();
303
						String family = d.getFontName();
304
						
305
						// size
306
						if (series == 0 && rowFontSizeStyle.containsKey(item)) {
307
							if (ddebug > 1) println "FIX ROW FONT-SIZE $item ${rows[item]} -> ${rowFontSizeStyle[item]}"
308
							size = rowFontSizeStyle[item];
309
						} else if (series == 1 && colFontSizeStyle.containsKey(item)) {
310
							if (ddebug > 1) println "FIX COL FONT-SIZE $item ${cols[item]} -> ${colFontSizeStyle[item]}"
311
							size = colFontSizeStyle[item];
312
						}
313
						
314
						// family
315
						if (series == 0 && rowFontFamilyStyle.containsKey(item)) {
316
							if (ddebug > 1) println "FIX ROW FONT-Family $item ${rows[item]} -> ${rowFontFamilyStyle[item]}"
317
							family = rowFontFamilyStyle[item];
318
						} else if (series == 1 && colFontFamilyStyle.containsKey(item)) {
319
							if (ddebug > 1) println "FIX COL FONT-Family $item ${cols[item]} -> ${colFontFamilyStyle[item]}"
320
							family = colFontFamilyStyle[item];
321
						}
322
						
323
						// style
324
						if (series == 0 && rowFontStyleStyle.containsKey(item)) {
325
							if (ddebug > 1) println "FIX ROW FONT-Style $item ${rows[item]} -> ${rowFontStyleStyle[item]}"
326
							style = rowFontStyleStyle[item];
327
						} else if (series == 1 && colFontFamilyStyle.containsKey(item)) {
328
							if (ddebug > 1) println "FIX COL FONT-Style $item ${cols[item]} -> ${colFontStyleStyle[item]}"
329
							style = colFontStyleStyle[item];
330
						}
331
						
332
						
333
						return new Font(family, style, size);
334
					}
335
					
336
					
337
					@Override
338
					public Shape getItemShape(int series, int item) {
339
						// Rows (series == 0), Cols (series == 1)
340
						if (series == 0 && rowHiddenStyle.contains(item)) {
341
							return EMPTYAREA;
342
						} else if (series == 1 && colHiddenStyle.contains(item)) {
343
							return EMPTYAREA;
344
						} else {
345
							
346
							Shape shape =  super.getItemShape(series, item);
347
							
348
							// not-visible shapes mode
349
							if(!((CA) this.multipleItemsSelector.getResult()).isShowPointShapes())        {
350
								return shape;
351
							}
352
							
353
							AffineTransform t = new AffineTransform();
354
							if (series == 0 && rowPointSizeStyle.containsKey(item)) {
355
								if (ddebug > 1) println "FIX ROW POINT SIZE $item ${rows[item]} -> ${rowPointSizeStyle[item]}"
356
								t.setToScale(rowPointSizeStyle.get(item), rowPointSizeStyle.get(item));
357
								shape = t.createTransformedShape(shape);
358
							} else if (series == 1 && colPointSizeStyle.containsKey(item)) {
359
								if (ddebug > 1) println "FIX COL POINT SIZE $item ${cols[item]} -> ${colPointSizeStyle[item]}"
360
								t.setToScale(colPointSizeStyle.get(item), colPointSizeStyle.get(item));
361
								shape = t.createTransformedShape(shape);
362
							}
363
							
364
							return shape;
365
						}
366
					}
367
					
368
					
369
					@Override
370
					public Paint getItemPaint(int series, int item) {
371
						
372
						// visible shapes mode
373
						if (!((CA) this.multipleItemsSelector.getResult()).isShowPointShapes()) {
374
							return super.getItemPaint(series, item);
375
						}
376
						
377
						if (series == 0 && rowPointColorStyle.containsKey(item)) {
378
							if (ddebug > 1) println "FIX ROW POINT COLOR $item ${rows[item]} -> ${rowPointColorStyle[item]}"
379
							return rowPointColorStyle.get(item);
380
						} else if (series == 1 && colPointColorStyle.containsKey(item)) {
381
							if (ddebug > 1) println "FIX COL POINT COLOR $item ${cols[item]} -> ${colPointColorStyle[item]}"
382
							return colPointColorStyle.get(item);
383
						}
384
						else {
385
							return super.getItemPaint(series, item);
386
						}
387
					}
388
					
389
					
390
					@Override
391
					public Paint getItemLabelPaint(int series, int item) {
392
						if (series == 0 && rowFontColorStyle.containsKey(item)) {
393
							if (ddebug > 1) println "FIX ROW LABEL COLOR $item ${rows[item]} -> ${rowFontColorStyle[item]}"
394
							return rowFontColorStyle.get(item);
395
						} else if (series == 1 && colFontColorStyle.containsKey(item)) {
396
							if (ddebug > 1) println "FIX COL LABEL COLOR $item ${cols[item]} -> ${colFontColorStyle[item]}"
397
							return colFontColorStyle.get(item);
398
						} else {
399
							return super.getItemLabelPaint(series, item);
400
						}
401
					}
402
					
403
					
404
					
405
				};
406
				
407
				
408
				def cp = editor.editors[0].getChart();
409
				renderer.chart = cp
410
						renderer.setSeriesVisible(0, ica.isShowVariables()); // Rows
411
				renderer.setSeriesVisible(1, ica.isShowIndividuals()); // Columns
412
				chart.getXYPlot().setRenderer(renderer)
413
				
414
				ica.getChartCreator().updateChart(chartEditor.getResult())
415
				
416
			}
417
		});
418

  
419

  
420

  
421

  
422
// creates a Color object from the specified RGB or RGBA string representation separated by spaces ("R G B" or "R G B A") from 0 to 255 for each channel
423
def rgbaStringToColor(String color) {
424
	String[] rgbColor = color.split(" ");
425
	String alpha = "255";
426
	if(rgbColor.length > 2) {
427
		if(rgbColor.length > 3) {
428
			alpha = rgbColor[3];
429
		}
430
		return new Color(Integer.parseInt(rgbColor[0]), Integer.parseInt(rgbColor[1]), Integer.parseInt(rgbColor[2]), Integer.parseInt(alpha))
431
	}
432
	else {
433
		println "Error in color format for string: $color"
434
	}
435
	
436
}
437

  
438

  

Formats disponibles : Unified diff