Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macro / prototypes / stats / AFCWithStylesMacro.groovy @ 2246

History | View | Annotate | Download (12.8 kB)

1
// STANDARD DECLARATIONS
2
package org.txm.macro
3

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

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

    
27
println "editor: "+editor
28

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

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

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

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

    
52
def data = ReadODS.toTable(patternsODSFile, "rows")
53
def keys = data[0]
54
def row_styles = [:] // reformat data
55
for (int i = 1 ; i < data.size() ; i++) {
56
        def h = data[i]
57
        def style = [:] // create style entry
58
        String s = h[0];
59
        row_styles[/$s/] = style // with a regexp pattern as key
60

    
61
        // fill the style
62
        for (int j = 1 ; j < h.size() ; j++) {
63
                style[keys[j]] = h[j]
64
        }
65
}
66
if (debug > 0) {
67
        println "ROW STYLES=$row_styles"
68
}
69

    
70
data = ReadODS.toTable(patternsODSFile, "cols")
71
keys = data[0]
72
def col_styles = [:] // reformat data
73
for (int i = 1 ; i < data.size() ; i++) {
74
        def h = data[i]
75
        def style = [:] // create style entry
76
        String s = h[0];
77
        col_styles[/$s/] = style // with a regexp pattern as key
78

    
79
        // fill the style
80
        for (int j = 1 ; j < h.size() ; j++) {
81
                style[keys[j]] = h[j]
82
        }
83
}
84
if (debug > 0) {
85
        println "COL STYLES=$col_styles"
86
}
87

    
88
ica = editor.getCA()
89
ca = ica.getCA()
90

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

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

    
103
shapes = [:]
104
shapes["square"] = AbstractRenderer.DEFAULT_SHAPE;
105
shapes["circle"] = AbstractRenderer.DEFAULT_SHAPE;
106
shapes["triangle"] = AbstractRenderer.DEFAULT_SHAPE;
107
shapes["star"] = AbstractRenderer.DEFAULT_SHAPE;
108
shapes["disk"] = AbstractRenderer.DEFAULT_SHAPE;
109

    
110
// styles per col index in dataset
111
// set col label
112
colLabelStyle = new HashMap<Integer, String>();
113
// set col visibility
114
colHiddenStyle = new HashSet<Integer>(); // true false
115
// set col font size
116
colFontSizeStyle = new HashMap<Integer, Integer>(); // 0.0 to 10.0
117
// set row font size
118
colFontColorStyle = new HashMap<Integer, Color>(); // 0.0 to 10.0
119
// set row font size
120
colFontFamillyStyle = new HashMap<Integer, String>(); // 0.0 to 10.0
121
// set col points size
122
colPointSizeStyle = new HashMap<Integer, Double>(); // 0.0 to 10.0
123
// set col points RGBA color
124
colPointColorStyle = new HashMap<Integer, Color>();
125
// set col points shape (circle, square, triangle, ... + color ? + size ?) -> expert
126
colPointShapeStyle = new HashMap<Integer, Shape>(); // circle, square, triangle, etc.
127

    
128
// set row label
129
rowLabelStyle = new HashMap<Integer, String>();
130
// set row visibility
131
rowHiddenStyle = new HashSet<Integer>(); // true false
132
// set row font size
133
rowFontSizeStyle = new HashMap<Integer, Integer>(); // 0.0 to 10.0
134
// set row font size
135
rowFontColorStyle = new HashMap<Integer, Color>(); // 0.0 to 10.0
136
// set row font size
137
rowFontFamillyStyle = new HashMap<Integer, String>(); // 0.0 to 10.0
138
// set row points size
139
rowPointSizeStyle = new HashMap<Integer, Double>(); // 0.0 to 10.0
140
// set row points RGBA color
141
rowPointColorStyle = new HashMap<Integer, Color>();
142
// set row points shape (circle, square, triangle, ... + color ? + size ?) -> expert
143
rowPointShapeStyle = new HashMap<Integer, Shape>(); // circle, square, triangle, etc.
144

    
145
// prepare col style data for the dataset
146
for (int i = 0 ; i < cols.length ; i++) {
147
        for (def p : col_styles.keySet()) {
148
                if (cols[i] ==~ p) {
149
                        def style = col_styles[p]
150
                        if (style["label"] != null && style["label"].length() > 0) {
151
                                colLabelStyle[i] = style["label"]
152
                        }
153
                        if (style["hidden"] != null && style["hidden"].toUpperCase() == "T") {
154
                                colHiddenStyle << i
155
                        }
156
                        if (style["point-size"] != null && style["point-size"].length() > 0) {
157
                                colPointSizeStyle[i] = Double.parseDouble(style["point-size"])
158
                        }
159
                        if (style["point-color"] != null && style["point-color"].length() > 0 ) {
160
                                colPointColorStyle[i] = new Color(Integer.parseInt(style["point-color"]), false)
161
                        }
162
                        if (style["point-shape"] != null && shapes.containsKey(style["point-shape"])) {
163
                                colPointShapeStyle[i] = shapes[style["point-shape"]]
164
                        }
165
                        if (style["font-size"] != null && style["font-size"].length() > 0) {
166
                                colFontSizeStyle[i] = Integer.parseInt(style["font-size"])
167
                        }
168
                        if (style["font-color"] != null && style["font-color"].length() > 0) {
169
                                colFontColorStyle[i] = Integer.parseInt(style["font-color"])
170
                        }
171
                        if (style["font-familly"] != null && style["font-familly"].length() > 0) {
172
                                colFontFamillyStyle[i] = style["font-familly"]
173
                        }
174
                }
175
        }
176
}
177
if (debug > 0) {
178
        println "COL COL=$colPointColorStyle"
179
        println "COL SHP=$colPointShapeStyle"
180
        println "COL LAB=$colLabelStyle"
181
        println "COL FONT-SIZ=$colFontSizeStyle"
182
        println "COL SIZ=$colPointSizeStyle"
183
        println "COL VIZ=$colHiddenStyle"
184
}
185

    
186
// prepare row style data for the dataset
187
for (int i = 0 ; i < rows.length ; i++) {
188
        for (def p : row_styles.keySet()) {
189
                if (rows[i] ==~ p) {
190
                        def style = row_styles[p]
191
                        if (style["hidden"] != null && style["hidden"].toUpperCase() == "T") {
192
                                rowHiddenStyle << i
193
                        }
194
                        if (style["label"] != null && style["label"].length() > 0) {
195
                                rowLabelStyle[i] = style["label"]
196
                        }
197
                        if (style["point-size"] != null && style["point-size"].length() > 0) {
198
                                rowPointSizeStyle[i] = Double.parseDouble(style["point-size"])
199
                        }
200
                        if (style["point-color"] != null && style["point-color"].length() > 0 ) {
201
                                rowPointColorStyle[i] = new Color(Integer.parseInt(style["point-color"]), false)
202
                        }
203
                        if (style["point-shape"] != null && shapes.containsKey(style["point-shape"])) {
204
                                rowPointShapeStyle[i] = shapes[style["point-shape"]]
205
                        }
206
                        if (style["font-size"] != null && style["font-size"].length() > 0) {
207
                                rowFontSizeStyle[i] = Integer.parseInt(style["font-size"])
208
                        }
209
                        if (style["font-color"] != null && style["font-color"].length() > 0) {
210
                                rowFontColorStyle[i] = Integer.parseInt(style["font-color"])
211
                        }
212
                        if (style["font-familly"] != null && style["font-familly"].length() > 0) {
213
                                rowFontFamillyStyle[i] = style["font-familly"]
214
                        }
215
                }
216
        }
217
}
218
if (debug > 0) {
219
        println "ROW COL=$rowPointColorStyle"
220
        println "ROW SHP=$rowPointShapeStyle"
221
        println "ROW LAB=$rowLabelStyle"
222
        println "ROW FONT-SIZ=$rowFontSizeStyle"
223
        println "ROW SIZ=$rowPointSizeStyle"
224
        println "ROW VIZ=$rowHiddenStyle"
225
}
226

    
227

    
228
// UPDATE THE CHART RENDERER
229

    
230
def chartEditor = editor.getEditors()[0]
231
def chartComposite = chartEditor.getComposite()
232

    
233
ddebug = debug
234
monitor.syncExec( new Runnable() {
235
                        public void run() {
236
                                println chartComposite
237
                                def chart = chartEditor.getChart();
238

    
239
                                //println "chart: "+chart
240
                                //println "Plot: "+chart.getPlot()
241
                                //println "Dataset: "+chart.getPlot().getDataset()
242
                                //println "Renderer: "+chart.getPlot().getRenderer()
243

    
244
                                def renderer = new CAItemSelectionRenderer(ica, chart) {
245
                                                        Area EMPTYAREA = new Area()
246

    
247
                                                        @Override
248
                                                        public void initItemLabelGenerator()        {
249
                                                                XYItemLabelGenerator generator = new XYItemLabelGenerator() {
250

    
251
                                                                                        @Override
252
                                                                                        public String generateLabel(XYDataset dataset, int series, int item) {
253
                                                                                                if (series == 0 && rowHiddenStyle.contains(item)) {
254
                                                                                                        if (ddebug > 1) println "HIDDING ROW LABEL '$item' '${rows[item]}'"
255
                                                                                                        return ""
256
                                                                                                } else if (series == 1 && colHiddenStyle.contains(item)) {
257
                                                                                                        if (ddebug > 1) println "HIDDING COL LABEL '$item' '${cols[item]}'"
258
                                                                                                        return ""
259
                                                                                                } else if (series == 0 && rowLabelStyle.containsKey(item)) {
260
                                                                                                        if (ddebug > 1) println "RENAME ROW LABEL '$item' '${rows[item]}' -> '${rowLabelStyle[item]}'"
261
                                                                                                        return rowLabelStyle[item]
262
                                                                                                } else if (series == 1 && colLabelStyle.containsKey(item)) {
263
                                                                                                        if (ddebug > 1) println "RENAME COL LABEL '$item' '${cols[item]}' -> '${colLabelStyle[item]}'"
264
                                                                                                        return colLabelStyle[item]
265
                                                                                                } else {
266
                                                                                                        CAXYDataset caDataset = (CAXYDataset) dataset;
267
                                                                                                        return caDataset.getLabel(series, item);
268
                                                                                                }
269
                                                                                        }
270
                                                                                };
271

    
272
                                                                this.setBaseItemLabelGenerator(generator);
273
                                                                this.setBaseItemLabelsVisible(true);
274
                                                        }
275

    
276
                                                        @Override
277
                                                        public Font getItemLabelFont(int series, int item) {
278
                                                                Font d = super.getItemLabelFont(series, item);
279
                                                                
280
                                                                if (series == 0 && rowFontSizeStyle.containsKey(item)) {
281
                                                                        if (ddebug > 1) println "FIX ROW FONT-SIZE $item ${rows[item]} -> ${rowFontSizeStyle[item]}"
282
                                                                        d = new Font(d.getFontName(), d.getStyle(), rowFontSizeStyle[item]);
283
                                                                } else if (series == 1 && colFontSizeStyle.containsKey(item)) {
284
                                                                        if (ddebug > 1) println "FIX COL FONT-SIZE $item ${cols[item]} -> ${colFontSizeStyle[item]}"
285
                                                                        d = new Font(d.getFontName(), d.getStyle(), colFontSizeStyle[item]);
286
                                                                }
287

    
288
                                                                if (series == 0 && rowFontFamillyStyle.containsKey(item)) {
289
                                                                        if (ddebug > 1) println "FIX ROW FONT-FAMILLY $item ${rows[item]} -> ${rowFontFamillyStyle[item]}"
290
                                                                        d = new Font(rowFontFamillyStyle[item], d.getStyle(), d.getSize());
291
                                                                } else if (series == 1 && colFontFamillyStyle.containsKey(item)) {
292
                                                                        if (ddebug > 1) println "FIX COL FONT-FAMILLY $item ${cols[item]} -> ${colFontFamillyStyle[item]}"
293
                                                                        d = new Font(colFontFamillyStyle[item], d.getStyle(), d.getSize());
294
                                                                }
295

    
296
                                                                return d;
297
                                                        }
298

    
299
                                                        @Override
300
                                                        public Shape getItemShape(int series, int item) {
301
                                                                // Rows (series == 0), Cols (series == 1)
302
                                                                if (series == 0 && rowHiddenStyle.contains(item)) {
303
                                                                        return EMPTYAREA;
304
                                                                } else if (series == 1 && colHiddenStyle.contains(item)) {
305
                                                                        return EMPTYAREA;
306
                                                                } else {
307
                                                                        AffineTransform t = new AffineTransform();
308
                                                                        Shape shape =  super.getItemShape(series, item);
309
                                                                        if (series == 0 && rowPointSizeStyle.containsKey(item)) {
310
                                                                                if (ddebug > 1) println "FIX ROW POINT SIZE $item ${rows[item]} -> ${rowPointSizeStyle[item]}"
311
                                                                                t.setToScale(rowPointSizeStyle.get(item), rowPointSizeStyle.get(item));
312
                                                                                shape = t.createTransformedShape(shape);
313
                                                                        } else if (series == 1 && colPointSizeStyle.containsKey(item)) {
314
                                                                                if (ddebug > 1) println "FIX COL POINT SIZE $item ${cols[item]} -> ${colPointSizeStyle[item]}"
315
                                                                                t.setToScale(colPointSizeStyle.get(item), colPointSizeStyle.get(item));
316
                                                                                shape = t.createTransformedShape(shape);
317
                                                                        }
318
                                                                        return shape;
319
                                                                }
320
                                                        }
321

    
322
                                                        @Override
323
                                                        public Paint getItemPaint(int series, int item) {
324
                                                                if (series == 0 && rowPointColorStyle.containsKey(item)) {
325
                                                                        if (ddebug > 1) println "FIX ROW POINT COLOR $item ${rows[item]} -> ${rowPointColorStyle[item]}"
326
                                                                        return rowPointColorStyle.get(item);
327
                                                                } else if (series == 1 && colPointColorStyle.containsKey(item)) {
328
                                                                        if (ddebug > 1) println "FIX COL POINT COLOR $item ${cols[item]} -> ${colPointColorStyle[item]}"
329
                                                                        return colPointColorStyle.get(item);
330
                                                                } else {
331
                                                                        return super.getItemPaint(series, item);
332
                                                                }
333
                                                        }
334
                                                };
335

    
336
                                def cp = editor.editors[0].getChart();
337
                                renderer.chart = cp
338
                                renderer.setSeriesVisible(0, ica.isShowVariables()); // Rows
339
                                renderer.setSeriesVisible(1, ica.isShowIndividuals()); // Columns
340
                                chart.getXYPlot().setRenderer(renderer)
341

    
342
                                ica.getChartCreator().getChartsEngine().getJFCTheme().apply(chart); // need to be call AFTER setRenderer() cause this method changes some renderering parameters
343
                        }
344
                });