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 2246 mdecorde
// STANDARD DECLARATIONS
2 2246 mdecorde
package org.txm.macro
3 2246 mdecorde
4 2246 mdecorde
import org.kohsuke.args4j.*
5 2246 mdecorde
import groovy.transform.Field
6 2246 mdecorde
import org.txm.rcpapplication.swt.widget.parameters.*
7 2246 mdecorde
import org.txm.searchengine.cqp.clientExceptions.*
8 2246 mdecorde
import org.txm.searchengine.cqp.corpus.*
9 2246 mdecorde
import org.txm.searchengine.cqp.corpus.query.*
10 2246 mdecorde
import org.apache.commons.lang.time.StopWatch
11 2246 mdecorde
import java.util.Arrays
12 2246 mdecorde
import org.jfree.chart.renderer.xy.*
13 2246 mdecorde
import org.jfree.chart.renderer.*
14 2246 mdecorde
import org.jfree.chart.plot.*
15 2246 mdecorde
import org.jfree.data.xy.*
16 2246 mdecorde
import org.jfree.chart.axis.*
17 2246 mdecorde
import java.awt.*;
18 2246 mdecorde
import java.awt.geom.*;
19 2246 mdecorde
import org.jfree.chart.labels.*
20 2246 mdecorde
21 2246 mdecorde
import org.txm.ca.core.chartsengine.jfreechart.themes.highcharts.renderers.*
22 2246 mdecorde
import org.txm.ca.rcp.editors.*
23 2246 mdecorde
import org.txm.libs.office.ReadODS
24 2246 mdecorde
import org.txm.ca.core.chartsengine.jfreechart.datasets.*
25 2246 mdecorde
import org.jfree.chart.renderer.AbstractRenderer
26 2246 mdecorde
27 2246 mdecorde
println "editor: "+editor
28 2246 mdecorde
29 2246 mdecorde
if (!(editor instanceof CAEditor)) {
30 2246 mdecorde
        println "editor is not a CA editor: $editor, Run the macro with F12 when the editor is selected :-)"
31 2246 mdecorde
        return
32 2246 mdecorde
}
33 2246 mdecorde
34 2246 mdecorde
@Field @Option(name="patternsODSFile", usage="The starting word", widget="FileOpen", required=true, def='patterns.ods')
35 2246 mdecorde
                def patternsODSFile
36 2246 mdecorde
@Field @Option(name="debug", usage="Show internal variable content", widget="StringArray", metaVar="OFF        ON        ALL        REALLY ALL", required=true, def="OFF")
37 2246 mdecorde
                debug
38 2246 mdecorde
39 2246 mdecorde
// Open the parameters input dialog box
40 2246 mdecorde
if (!ParametersDialog.open(this)) return;
41 2246 mdecorde
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 2246 mdecorde
43 2246 mdecorde
if (!patternsODSFile.exists()) {
44 2246 mdecorde
        println "Pattern file not found: $patternsODSFile"
45 2246 mdecorde
        return false;
46 2246 mdecorde
}
47 2246 mdecorde
if (!patternsODSFile.isFile() || !patternsODSFile.getName().toLowerCase().endsWith(".ods")) {
48 2246 mdecorde
        println "Wrong pattern file: $patternsODSFile"
49 2246 mdecorde
        return false;
50 2246 mdecorde
}
51 2246 mdecorde
52 2246 mdecorde
def data = ReadODS.toTable(patternsODSFile, "rows")
53 2246 mdecorde
def keys = data[0]
54 2246 mdecorde
def row_styles = [:] // reformat data
55 2246 mdecorde
for (int i = 1 ; i < data.size() ; i++) {
56 2246 mdecorde
        def h = data[i]
57 2246 mdecorde
        def style = [:] // create style entry
58 2246 mdecorde
        String s = h[0];
59 2246 mdecorde
        row_styles[/$s/] = style // with a regexp pattern as key
60 2246 mdecorde
61 2246 mdecorde
        // fill the style
62 2246 mdecorde
        for (int j = 1 ; j < h.size() ; j++) {
63 2246 mdecorde
                style[keys[j]] = h[j]
64 2246 mdecorde
        }
65 2246 mdecorde
}
66 2246 mdecorde
if (debug > 0) {
67 2246 mdecorde
        println "ROW STYLES=$row_styles"
68 2246 mdecorde
}
69 2246 mdecorde
70 2246 mdecorde
data = ReadODS.toTable(patternsODSFile, "cols")
71 2246 mdecorde
keys = data[0]
72 2246 mdecorde
def col_styles = [:] // reformat data
73 2246 mdecorde
for (int i = 1 ; i < data.size() ; i++) {
74 2246 mdecorde
        def h = data[i]
75 2246 mdecorde
        def style = [:] // create style entry
76 2246 mdecorde
        String s = h[0];
77 2246 mdecorde
        col_styles[/$s/] = style // with a regexp pattern as key
78 2246 mdecorde
79 2246 mdecorde
        // fill the style
80 2246 mdecorde
        for (int j = 1 ; j < h.size() ; j++) {
81 2246 mdecorde
                style[keys[j]] = h[j]
82 2246 mdecorde
        }
83 2246 mdecorde
}
84 2246 mdecorde
if (debug > 0) {
85 2246 mdecorde
        println "COL STYLES=$col_styles"
86 2246 mdecorde
}
87 2246 mdecorde
88 2246 mdecorde
ica = editor.getCA()
89 2246 mdecorde
ca = ica.getCA()
90 2246 mdecorde
91 2246 mdecorde
// http://txm.sourceforge.net/javadoc/TXM/TBX/org/txm/stat/engine/r/function/CA.html
92 2246 mdecorde
93 2246 mdecorde
// SOME DATA
94 2246 mdecorde
rows = ica.getRowNames()
95 2246 mdecorde
rowsinfo = ica.getRowInfos()
96 2246 mdecorde
rowscos2 = ca.getRowCos2()
97 2246 mdecorde
cols = ica.getColNames()
98 2246 mdecorde
println cols
99 2246 mdecorde
colssinfo = ica.getColInfos()
100 2246 mdecorde
colscos2 = ca.getColCos2()
101 2246 mdecorde
D1 = ica.getFirstDimension() -1;
102 2246 mdecorde
D2 = ica.getSecondDimension() -1;
103 2246 mdecorde
104 2246 mdecorde
shapes = [:]
105 2246 mdecorde
shapes["square"] = AbstractRenderer.DEFAULT_SHAPE;
106 2246 mdecorde
shapes["circle"] = AbstractRenderer.DEFAULT_SHAPE;
107 2246 mdecorde
shapes["triangle"] = AbstractRenderer.DEFAULT_SHAPE;
108 2246 mdecorde
shapes["star"] = AbstractRenderer.DEFAULT_SHAPE;
109 2246 mdecorde
shapes["disk"] = AbstractRenderer.DEFAULT_SHAPE;
110 2246 mdecorde
111 2246 mdecorde
// styles per col index in dataset
112 2246 mdecorde
// set col points label
113 2246 mdecorde
colLabelStyle = new HashMap<Integer, String>();
114 2246 mdecorde
// set col points visibility
115 2246 mdecorde
colHiddenStyle = new HashSet<Integer>(); // true false
116 2246 mdecorde
// set col font size
117 2246 mdecorde
colFontSizeStyle = new HashMap<Integer, Integer>(); // 0.0 to 10.0
118 2246 mdecorde
// set col points size
119 2246 mdecorde
colSizeStyle = new HashMap<Integer, Double>(); // 0.0 to 10.0
120 2246 mdecorde
// set col points RGBA color
121 2246 mdecorde
colColorStyle = new HashMap<Integer, Color>();
122 2246 mdecorde
// set col points shape (circle, square, triangle, ... + color ? + size ?) -> expert
123 2246 mdecorde
colShapeStyle = new HashMap<Integer, Shape>(); // circle, square, triangle, etc.
124 2246 mdecorde
125 2246 mdecorde
126 2246 mdecorde
rowLabelStyle = new HashMap<Integer, String>();
127 2246 mdecorde
// set row points visibility
128 2246 mdecorde
rowHiddenStyle = new HashSet<Integer>(); // true false
129 2246 mdecorde
// set row font size
130 2246 mdecorde
rowFontSizeStyle = new HashMap<Integer, Integer>(); // 0.0 to 10.0
131 2246 mdecorde
// set row points size
132 2246 mdecorde
rowSizeStyle = new HashMap<Integer, Double>(); // 0.0 to 10.0
133 2246 mdecorde
// set row points RGBA color
134 2246 mdecorde
rowColorStyle = new HashMap<Integer, Color>();
135 2246 mdecorde
// set row points shape (circle, square, triangle, ... + color ? + size ?) -> expert
136 2246 mdecorde
rowShapeStyle = new HashMap<Integer, Shape>(); // circle, square, triangle, etc.
137 2246 mdecorde
138 2246 mdecorde
// prepare col style data for the dataset
139 2246 mdecorde
for (int i = 0 ; i < cols.length ; i++) {
140 2246 mdecorde
        for (def p : col_styles.keySet()) {
141 2246 mdecorde
                if (cols[i] ==~ p) {
142 2246 mdecorde
                        def style = col_styles[p]
143 2246 mdecorde
                        if (style["shape"] != null && shapes.containsKey(style["shape"])) {
144 2246 mdecorde
                                colShapeStyle[i] = shapes[style["shape"]]
145 2246 mdecorde
                        }
146 2246 mdecorde
                        if (style["color"] != null && style["color"].length() > 0 ) {
147 2246 mdecorde
                                colColorStyle[i] = new Color(Integer.parseInt(style["color"]), true)
148 2246 mdecorde
                        }
149 2246 mdecorde
                        if (style["label"] != null && style["label"].length() > 0) {
150 2246 mdecorde
                                colLabelStyle[i] = style["label"]
151 2246 mdecorde
                        }
152 2246 mdecorde
                        if (style["size"] != null && style["size"].length() > 0) {
153 2246 mdecorde
                                colSizeStyle[i] = Double.parseDouble(style["size"])
154 2246 mdecorde
                        }
155 2246 mdecorde
                        if (style["font-size"] != null && style["font-size"].length() > 0) {
156 2246 mdecorde
                                colFontSizeStyle[i] = Integer.parseInt(style["font-size"])
157 2246 mdecorde
                        }
158 2246 mdecorde
                        if (style["hidden"] != null && style["hidden"].toUpperCase() == "T") {
159 2246 mdecorde
                                colHiddenStyle << i
160 2246 mdecorde
                        }
161 2246 mdecorde
                }
162 2246 mdecorde
        }
163 2246 mdecorde
}
164 2246 mdecorde
if (debug > 0) {
165 2246 mdecorde
        println "COL COL=$colColorStyle"
166 2246 mdecorde
        println "COL SHP=$colShapeStyle"
167 2246 mdecorde
        println "COL LAB=$colLabelStyle"
168 2246 mdecorde
        println "COL FONT-SIZ=$colFontSizeStyle"
169 2246 mdecorde
        println "COL SIZ=$colSizeStyle"
170 2246 mdecorde
        println "COL VIZ=$colHiddenStyle"
171 2246 mdecorde
}
172 2246 mdecorde
173 2246 mdecorde
174 2246 mdecorde
175 2246 mdecorde
// UPDATE THE CHART RENDERER
176 2246 mdecorde
177 2246 mdecorde
def chartEditor = editor.getEditors()[0]
178 2246 mdecorde
def chartComposite = chartEditor.getComposite()
179 2246 mdecorde
180 2246 mdecorde
ddebug = debug
181 2246 mdecorde
monitor.syncExec( new Runnable() {
182 2246 mdecorde
                        public void run() {
183 2246 mdecorde
                                println chartComposite
184 2246 mdecorde
                                def chart = chartEditor.getChart();
185 2246 mdecorde
186 2246 mdecorde
                                //println "chart: "+chart
187 2246 mdecorde
                                //println "Plot: "+chart.getPlot()
188 2246 mdecorde
                                //println "Dataset: "+chart.getPlot().getDataset()
189 2246 mdecorde
                                //println "Renderer: "+chart.getPlot().getRenderer()
190 2246 mdecorde
191 2246 mdecorde
                                def renderer = new CAItemSelectionRenderer(ica, chart) {
192 2246 mdecorde
                                                        Area EMPTYAREA = new Area()
193 2246 mdecorde
194 2246 mdecorde
                                                        @Override
195 2246 mdecorde
                                                        public void initItemLabelGenerator()        {
196 2246 mdecorde
                                                                XYItemLabelGenerator generator = new XYItemLabelGenerator() {
197 2246 mdecorde
198 2246 mdecorde
                                                                                        @Override
199 2246 mdecorde
                                                                                        public String generateLabel(XYDataset dataset, int series, int item) {
200 2246 mdecorde
                                                                                                if (series == 0 && rowHiddenStyle.contains(item)) {
201 2246 mdecorde
                                                                                                        if (ddebug > 1) println "HIDDING ROW LABEL '$item' '${rows[item]}'"
202 2246 mdecorde
                                                                                                        return ""
203 2246 mdecorde
                                                                                                } else if (series == 1 && colHiddenStyle.contains(item)) {
204 2246 mdecorde
                                                                                                        if (ddebug > 1) println "HIDDING COL LABEL '$item' '${cols[item]}'"
205 2246 mdecorde
                                                                                                        return ""
206 2246 mdecorde
                                                                                                } else if (series == 0 && rowLabelStyle.containsKey(item)) {
207 2246 mdecorde
                                                                                                        if (ddebug > 1) println "RENAME ROW LABEL '$item' '${rows[item]}' -> '${rowLabelStyle[item]}'"
208 2246 mdecorde
                                                                                                        return rowLabelStyle[item]
209 2246 mdecorde
                                                                                                } else if (series == 1 && colLabelStyle.containsKey(item)) {
210 2246 mdecorde
                                                                                                        if (ddebug > 1) println "RENAME COL LABEL '$item' '${cols[item]}' -> '${colLabelStyle[item]}'"
211 2246 mdecorde
                                                                                                        return colLabelStyle[item]
212 2246 mdecorde
                                                                                                } else {
213 2246 mdecorde
                                                                                                        CAXYDataset caDataset = (CAXYDataset) dataset;
214 2246 mdecorde
                                                                                                        return caDataset.getLabel(series, item);
215 2246 mdecorde
                                                                                                }
216 2246 mdecorde
                                                                                        }
217 2246 mdecorde
                                                                                };
218 2246 mdecorde
219 2246 mdecorde
                                                                this.setBaseItemLabelGenerator(generator);
220 2246 mdecorde
                                                                this.setBaseItemLabelsVisible(true);
221 2246 mdecorde
                                                        }
222 2246 mdecorde
223 2246 mdecorde
                                                        @Override
224 2246 mdecorde
                                                        public Font getItemLabelFont(int series, int item) {
225 2246 mdecorde
                                                                Font d = super.getItemLabelFont(series, item);
226 2246 mdecorde
                                                                if (series == 0 && rowFontSizeStyle.containsKey(item)) {
227 2246 mdecorde
                                                                        if (ddebug > 1) println "FIX ROW FONT-SIZE $item "+rows[item]
228 2246 mdecorde
                                                                        return new Font(d.getFontName(), d.getStyle(), rowFontSizeStyle[item]);
229 2246 mdecorde
                                                                } else if (series == 1 && colFontSizeStyle.containsKey(item)) {
230 2246 mdecorde
                                                                        if (ddebug > 1) println "FIX COL FONT-SIZE $item "+cols[item]
231 2246 mdecorde
                                                                        return new Font(d.getFontName(), d.getStyle(), colFontSizeStyle[item]);
232 2246 mdecorde
                                                                } else {
233 2246 mdecorde
                                                                        return d;
234 2246 mdecorde
                                                                }
235 2246 mdecorde
                                                        }
236 2246 mdecorde
237 2246 mdecorde
                                                        @Override
238 2246 mdecorde
                                                        public Shape getItemShape(int series, int item) {
239 2246 mdecorde
                                                                // Rows (series == 0), Cols (series == 1)
240 2246 mdecorde
                                                                if (series == 0 && rowHiddenStyle.contains(item)) {
241 2246 mdecorde
                                                                        return EMPTYAREA;
242 2246 mdecorde
                                                                } else if (series == 1 && colHiddenStyle.contains(item)) {
243 2246 mdecorde
                                                                        return EMPTYAREA;
244 2246 mdecorde
                                                                } else {
245 2246 mdecorde
                                                                        AffineTransform t = new AffineTransform();
246 2246 mdecorde
                                                                        Shape shape =  super.getItemShape(series, item);
247 2246 mdecorde
                                                                        if (series == 0 && rowSizeStyle.containsKey(item)) {
248 2246 mdecorde
                                                                                t.setToScale(rowSizeStyle.get(item), rowSizeStyle.get(item));
249 2246 mdecorde
                                                                                shape = t.createTransformedShape(shape);
250 2246 mdecorde
                                                                        } else if (series == 1 && colSizeStyle.containsKey(item)) {
251 2246 mdecorde
                                                                                t.setToScale(colSizeStyle.get(item), colSizeStyle.get(item));
252 2246 mdecorde
                                                                                shape = t.createTransformedShape(shape);
253 2246 mdecorde
                                                                        }
254 2246 mdecorde
                                                                        return shape;
255 2246 mdecorde
                                                                }
256 2246 mdecorde
                                                        }
257 2246 mdecorde
258 2246 mdecorde
                                                        @Override
259 2246 mdecorde
                                                        public Paint getItemPaint(int series, int item) {
260 2246 mdecorde
                                                                if (series == 0 && rowColorStyle.containsKey(item)) {
261 2246 mdecorde
                                                                        return rowColorStyle.get(item);
262 2246 mdecorde
                                                                } else if (series == 1 && colColorStyle.containsKey(item)) {
263 2246 mdecorde
                                                                        return colColorStyle.get(item);
264 2246 mdecorde
                                                                } else {
265 2246 mdecorde
                                                                        return super.getItemPaint(series, item);
266 2246 mdecorde
                                                                }
267 2246 mdecorde
                                                        }
268 2246 mdecorde
                                                };
269 2246 mdecorde
270 2246 mdecorde
                                def cp = editor.editors[0].getChart();
271 2246 mdecorde
                                renderer.chart = cp
272 2246 mdecorde
                                renderer.setSeriesVisible(0, ica.isShowVariables()); // Rows
273 2246 mdecorde
                                renderer.setSeriesVisible(1, ica.isShowIndividuals()); // Columns
274 2246 mdecorde
                                chart.getXYPlot().setRenderer(renderer)
275 2246 mdecorde
276 2246 mdecorde
                                ica.getChartCreator().getChartsEngine().getJFCTheme().apply(chart); // need to be call AFTER setRenderer() cause this method changes some renderering parameters
277 2246 mdecorde
                        }
278 2246 mdecorde
                });