Statistics
| Revision:

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

History | View | Annotate | Download (9.5 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
println cols
99
colssinfo = ica.getColInfos()
100
colscos2 = ca.getColCos2()
101
D1 = ica.getFirstDimension() -1;
102
D2 = ica.getSecondDimension() -1;
103

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

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

    
125

    
126
rowLabelStyle = new HashMap<Integer, String>();
127
// set row points visibility
128
rowHiddenStyle = new HashSet<Integer>(); // true false
129
// set row font size
130
rowFontSizeStyle = new HashMap<Integer, Integer>(); // 0.0 to 10.0
131
// set row points size
132
rowSizeStyle = new HashMap<Integer, Double>(); // 0.0 to 10.0
133
// set row points RGBA color
134
rowColorStyle = new HashMap<Integer, Color>();
135
// set row points shape (circle, square, triangle, ... + color ? + size ?) -> expert
136
rowShapeStyle = new HashMap<Integer, Shape>(); // circle, square, triangle, etc.
137

    
138
// prepare col style data for the dataset
139
for (int i = 0 ; i < cols.length ; i++) {
140
        for (def p : col_styles.keySet()) {
141
                if (cols[i] ==~ p) {
142
                        def style = col_styles[p]
143
                        if (style["shape"] != null && shapes.containsKey(style["shape"])) {
144
                                colShapeStyle[i] = shapes[style["shape"]]
145
                        }
146
                        if (style["color"] != null && style["color"].length() > 0 ) {
147
                                colColorStyle[i] = new Color(Integer.parseInt(style["color"]), true)
148
                        }
149
                        if (style["label"] != null && style["label"].length() > 0) {
150
                                colLabelStyle[i] = style["label"]
151
                        }
152
                        if (style["size"] != null && style["size"].length() > 0) {
153
                                colSizeStyle[i] = Double.parseDouble(style["size"])
154
                        }
155
                        if (style["font-size"] != null && style["font-size"].length() > 0) {
156
                                colFontSizeStyle[i] = Integer.parseInt(style["font-size"])
157
                        }
158
                        if (style["hidden"] != null && style["hidden"].toUpperCase() == "T") {
159
                                colHiddenStyle << i
160
                        }
161
                }
162
        }
163
}
164
if (debug > 0) {
165
        println "COL COL=$colColorStyle"
166
        println "COL SHP=$colShapeStyle"
167
        println "COL LAB=$colLabelStyle"
168
        println "COL FONT-SIZ=$colFontSizeStyle"
169
        println "COL SIZ=$colSizeStyle"
170
        println "COL VIZ=$colHiddenStyle"
171
}
172

    
173

    
174

    
175
// UPDATE THE CHART RENDERER
176

    
177
def chartEditor = editor.getEditors()[0]
178
def chartComposite = chartEditor.getComposite()
179

    
180
ddebug = debug
181
monitor.syncExec( new Runnable() {
182
                        public void run() {
183
                                println chartComposite
184
                                def chart = chartEditor.getChart();
185

    
186
                                //println "chart: "+chart
187
                                //println "Plot: "+chart.getPlot()
188
                                //println "Dataset: "+chart.getPlot().getDataset()
189
                                //println "Renderer: "+chart.getPlot().getRenderer()
190

    
191
                                def renderer = new CAItemSelectionRenderer(ica, chart) {
192
                                                        Area EMPTYAREA = new Area()
193

    
194
                                                        @Override
195
                                                        public void initItemLabelGenerator()        {
196
                                                                XYItemLabelGenerator generator = new XYItemLabelGenerator() {
197

    
198
                                                                                        @Override
199
                                                                                        public String generateLabel(XYDataset dataset, int series, int item) {
200
                                                                                                if (series == 0 && rowHiddenStyle.contains(item)) {
201
                                                                                                        if (ddebug > 1) println "HIDDING ROW LABEL '$item' '${rows[item]}'"
202
                                                                                                        return ""
203
                                                                                                } else if (series == 1 && colHiddenStyle.contains(item)) {
204
                                                                                                        if (ddebug > 1) println "HIDDING COL LABEL '$item' '${cols[item]}'"
205
                                                                                                        return ""
206
                                                                                                } else if (series == 0 && rowLabelStyle.containsKey(item)) {
207
                                                                                                        if (ddebug > 1) println "RENAME ROW LABEL '$item' '${rows[item]}' -> '${rowLabelStyle[item]}'"
208
                                                                                                        return rowLabelStyle[item]
209
                                                                                                } else if (series == 1 && colLabelStyle.containsKey(item)) {
210
                                                                                                        if (ddebug > 1) println "RENAME COL LABEL '$item' '${cols[item]}' -> '${colLabelStyle[item]}'"
211
                                                                                                        return colLabelStyle[item]
212
                                                                                                } else {
213
                                                                                                        CAXYDataset caDataset = (CAXYDataset) dataset;
214
                                                                                                        return caDataset.getLabel(series, item);
215
                                                                                                }
216
                                                                                        }
217
                                                                                };
218

    
219
                                                                this.setBaseItemLabelGenerator(generator);
220
                                                                this.setBaseItemLabelsVisible(true);
221
                                                        }
222

    
223
                                                        @Override
224
                                                        public Font getItemLabelFont(int series, int item) {
225
                                                                Font d = super.getItemLabelFont(series, item);
226
                                                                if (series == 0 && rowFontSizeStyle.containsKey(item)) {
227
                                                                        if (ddebug > 1) println "FIX ROW FONT-SIZE $item "+rows[item]
228
                                                                        return new Font(d.getFontName(), d.getStyle(), rowFontSizeStyle[item]);
229
                                                                } else if (series == 1 && colFontSizeStyle.containsKey(item)) {
230
                                                                        if (ddebug > 1) println "FIX COL FONT-SIZE $item "+cols[item]
231
                                                                        return new Font(d.getFontName(), d.getStyle(), colFontSizeStyle[item]);
232
                                                                } else {
233
                                                                        return d;
234
                                                                }
235
                                                        }
236

    
237
                                                        @Override
238
                                                        public Shape getItemShape(int series, int item) {
239
                                                                // Rows (series == 0), Cols (series == 1)
240
                                                                if (series == 0 && rowHiddenStyle.contains(item)) {
241
                                                                        return EMPTYAREA;
242
                                                                } else if (series == 1 && colHiddenStyle.contains(item)) {
243
                                                                        return EMPTYAREA;
244
                                                                } else {
245
                                                                        AffineTransform t = new AffineTransform();
246
                                                                        Shape shape =  super.getItemShape(series, item);
247
                                                                        if (series == 0 && rowSizeStyle.containsKey(item)) {
248
                                                                                t.setToScale(rowSizeStyle.get(item), rowSizeStyle.get(item));
249
                                                                                shape = t.createTransformedShape(shape);
250
                                                                        } else if (series == 1 && colSizeStyle.containsKey(item)) {
251
                                                                                t.setToScale(colSizeStyle.get(item), colSizeStyle.get(item));
252
                                                                                shape = t.createTransformedShape(shape);
253
                                                                        }
254
                                                                        return shape;
255
                                                                }
256
                                                        }
257

    
258
                                                        @Override
259
                                                        public Paint getItemPaint(int series, int item) {
260
                                                                if (series == 0 && rowColorStyle.containsKey(item)) {
261
                                                                        return rowColorStyle.get(item);
262
                                                                } else if (series == 1 && colColorStyle.containsKey(item)) {
263
                                                                        return colColorStyle.get(item);
264
                                                                } else {
265
                                                                        return super.getItemPaint(series, item);
266
                                                                }
267
                                                        }
268
                                                };
269

    
270
                                def cp = editor.editors[0].getChart();
271
                                renderer.chart = cp
272
                                renderer.setSeriesVisible(0, ica.isShowVariables()); // Rows
273
                                renderer.setSeriesVisible(1, ica.isShowIndividuals()); // Columns
274
                                chart.getXYPlot().setRenderer(renderer)
275

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