Statistics
| Revision:

root / arquivos_versoes / v60 / saiku-server / tomcat / webapps / ROOT / js / saiku / plugins / i3GeoMap / plugin.js @ 5276

History | View | Annotate | Download (26 KB)

1
/*
2
 *   Esse plugin e parte do software livre i3Geo
3
 *
4
 *   Baseado em ChartPlus IT4biz IT Solutions Ltda
5
 *
6
 *   Licensed under the Apache License, Version 2.0 (the "License");
7
 *   you may not use this file except in compliance with the License.
8
 *   You may obtain a copy of the License at
9
 *
10
 *       http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 *   Unless required by applicable law or agreed to in writing, software
13
 *   distributed under the License is distributed on an "AS IS" BASIS,
14
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 *   See the License for the specific language governing permissions and
16
 *   limitations under the License.
17
 * changed by edmar.moretti@gmail.com
18
 */
19

    
20
var i3GeoMap = Backbone.View.extend({
21
        opcoes: {cores:"",size:0,tipo:"",indicecoluna:"",cores:"",coluna:"",locaplic:"",outlinecolor:""},
22
        coreshex:  ["#1f77b4", "#aec7e8", "#ff7f0e", "#ffbb78", "#2ca02c", "#98df8a", "#d62728", "#ff9896", "#9467bd", "#c5b0d5", "#8c564b", "#c49c94", "#e377c2", "#f7b6d2", "#7f7f7f", "#c7c7c7", "#bcbd22", "#dbdb8d", "#17becf", "#9edae5" ],
23
        initialize: function(args) {
24
                this.workspace = args.workspace;
25

    
26
                // Create a unique ID for use as the CSS selector
27
                this.id = _.uniqueId("i3GeoMap_");
28
                $(this.el).attr({ id: this.id });
29

    
30
                // Bind map rendering to query result event
31
                _.bindAll(this, "render", "receive_data", "process_data", "show",
32
                                "setOptions");
33

    
34
                this.workspace.bind('query:result', this.receive_data);
35

    
36
                // Add chart button
37
                this.add_button();
38
                this.workspace.toolbar.i3GeoMap = this.show;
39

    
40
                // Listen to adjust event and rerender chart
41
                //this.workspace.bind('workspace:adjust', this.render);
42
                // Create navigation
43
                this.nav = $("" +
44
                        "<div style='overflow: hidden;padding: 10px 5px;' >"+
45
                        "<div class='fields_list' style='height:34px;'>"+
46
                                "<div class='fields_list_header limit'><span class='i18n_translated'>Mapa</span></div>"+
47
                                "<div class='fields_list_body filter' style='margin:4px 5px 5px 5px'>"+
48
                                        //"<ul class='connectable ui-sortable' style=''>"+
49
                                                "<input type=button name=mapaBarras value='Barras' />"+
50
                                                "<input type=button name=mapaPizzas value='Pizzas' />"+
51
                                                "&nbsp;<input type=button name=raiosProporcionais value='Raios' />"+
52
                                                "&nbsp;<input type=button name=circulosProporcionais value='C&iacute;rculos' />"+
53
                                                "&nbsp;<input type=button name=coresChapadas value='Cores' />"+
54
                                                "&nbsp;<input type=button name=calor value='Calor' />"+
55
                                                "&nbsp;<input type=button name=atualizarMapa value='Atualiza' />"+
56
                                                "&nbsp;<input type=button name=reiniciarMapa value='Reinicia' />"+
57
                                                "&nbsp;<input type=button name=fecharMapa value='Fecha' />"+
58
                                        //"</ul>"+
59
                                "</div>"+
60
                        "</div>"+
61
                        "</div>"+
62
                        "<div id='message'></div>"+
63
                        "<form id='formi3GeoMap' method='post' target='iframei3GeoMap' action='"+parametroUrl("locaplic")+"/ferramentas/saiku/cartograma.php' >"+
64
                        "<input type=hidden name=opcoes id=formi3GeoMapOpcoes value='' />"+
65
                        "<input type=hidden name=g_sid id=formi3GeoMapg_sid value='"+parametroUrl("g_sid")+"' />"+
66
                        "<input type=hidden name=dados id=formi3GeoMapdados value='' />"+
67
                        "<input type=hidden name=metadados id=formi3GeoMapmetadados value='' />"+
68
                        "</form>" +
69
                        "<iframe id='iframei3GeoMap' name='iframei3GeoMap' style='width:100%;height:200px;display:block' />"
70
                                );
71
                this.nav.find('input').click(this.setOptions);
72

    
73
                // Append chart to workspace
74
                $(this.workspace.el).find('.workspace_results')
75
                        .prepend($(this.el).hide())
76
                        .prepend(this.nav.hide());
77
        },
78
        add_button: function() {
79
                var $chart_button =
80
                                $('<a href="#i3GeoMap" class="i3GeoMap button disabled_toolbar i18n" title="Mapa com i3Geo"></a>')
81
                                .css({  'background-image': "url('js/saiku/plugins/i3GeoMap/mapa.png')",
82
                                                                'background-repeat':'no-repeat',
83
                                                                'background-position':'20% 50%'
84
                                                });
85

    
86
                var $chart_li = $('<li class="seperator"></li>').append($chart_button);
87
                $(this.workspace.toolbar.el).find("ul").append($chart_li);
88
        },
89
        fecha: function(){
90
                $(this.workspace.toolbar.el).find('.i3GeoMap').removeClass('on');
91
                $(this.el).hide();
92
                $(this.nav).hide();
93
                this.workspace.processing.hide();
94
                this.workspace.adjust();
95
                $(this.workspace.el).find('.workspace_results table').show();
96
                this.workspace.table.render({ data: this.workspace.query.result.lastresult() });
97
        },
98
        atualizarMapa: function(){
99
                document.getElementById('formi3GeoMap').submit();
100
        },
101
        reiniciarMapa: function(){
102
                document.getElementById('formi3GeoMapOpcoes').value = "reinicia";
103
                document.getElementById('formi3GeoMapdados').value = "";
104
                this.opcoes.locaplic = parametroUrl("locaplic");
105
                this.opcoes.mapext = parametroUrl("mapext");
106
                document.getElementById("formi3GeoMapOpcoes").value = JSON.stringify(this.opcoes);
107
                document.getElementById('formi3GeoMap').submit();
108
        },
109
        show: function(event, ui) {
110
                //$(this.workspace.el).find('.workspace_results table').toggle();
111
                $(this.el).toggle();
112
                $(this.nav).toggle();
113
                $(event.target).toggleClass('on');
114
                $("#message").html("");
115
                this.nav.find('input').show();
116
                if ($(event.target).hasClass('on')) {
117
                        this.process_data({ data: this.workspace.query.result.lastresult() });
118
                        //this.render();
119
                }
120
        },
121

    
122
        setOptions: function(event) {
123
                var type = $(event.target).attr('name');
124
                this.opcoes.coluna = "";
125
                this.opcoes.indicecoluna = "";
126
                this.opcoes.cores = "";
127
                this.opcoes.outlinecolor = "";
128
                this[type](this);
129
                return false;
130
        },
131
        fecharMapa: function(){
132
                var i = document.getElementById("iframei3GeoMap");
133
                if(i){
134
                        i.src = "";
135
                        i.style.display = "none";
136
                        i.style.height = "0px";
137
                }
138
        },
139
        mapaPizzas: function(s){
140
                this.opcoes.size = 50;
141
                this.opcoes.cores = coresHex2Rgb(this.coreshex);
142
                $.fancybox("Cor do contorno:  "+this.cores("corContorno")+
143
                                "Tamanho das pizzas (em pixels):<br><input type=text value='"+this.opcoes.size+"' size=6 id='mapaPizzasSize' />" +
144
                                "<br><input type=button value='OK' id='mapaPizzasOk' />" +
145
                                "&nbsp; <input type=button value='Cancela' id='mapaPizzasCancela' />"
146
                                ,
147
                                {
148
                                        'autoDimensions'    : false,
149
                                        'autoScale'         : false,
150
                                        'height'            :  250,
151
                                        'width'             :  350,
152
                                        'transitionIn'      : 'none',
153
                                        'transitionOut'     : 'none',
154
                                        'showCloseButton'        : false,
155
                                        'modal'                                : false
156
                                }
157
                );
158
                document.getElementById("mapaPizzasOk").onclick = function(){
159
                        s.opcoes.tipo = "mapaPizzas";
160
                        s.opcoes.size = document.getElementById("mapaPizzasSize").value;
161
                        s.opcoes.indicecoluna = [];
162
                        var cori = $('input[name="corContorno"]').val();
163
                        s.opcoes.outlinecolor = hex2rgb(cori);
164
                        s.render();
165
                        $.fancybox.close();
166
                };
167
                document.getElementById("mapaPizzasCancela").onclick = function(){
168
                        $.fancybox.close();
169
                        s.fecharMapa();
170
                };
171
                jQuery('select[name="corContorno"]').colourPicker({
172
                                ico:    'js/jquery/jquery.colourPicker.gif',
173
                                title:    false
174
                });
175
        },
176

    
177
        mapaBarras: function(s){
178
                this.opcoes.size = 50;
179
                this.opcoes.cores = coresHex2Rgb(this.coreshex);
180
                $.fancybox("Cor do contorno:  "+this.cores("corContorno")+
181
                                "Tamanho das barras (em pixels):<br><input type=text value='"+this.opcoes.size+"' size=6 id='mapaBarrasSize' />" +
182
                                "<br><input type=button value='OK' id='mapaBarrasOk' />" +
183
                                "&nbsp; <input type=button value='Cancela' id='mapaBarrasCancela' />"
184
                                ,
185
                                {
186
                                        'autoDimensions'    : false,
187
                                        'autoScale'         : false,
188
                                        'height'            :  250,
189
                                        'width'             :  350,
190
                                        'transitionIn'      : 'none',
191
                                        'transitionOut'     : 'none',
192
                                        'showCloseButton'        : false,
193
                                        'modal'                                : false
194
                                }
195
                );
196
                document.getElementById("mapaBarrasOk").onclick = function(){
197
                        s.opcoes.tipo = "mapaBarras";
198
                        s.opcoes.size = document.getElementById("mapaBarrasSize").value;
199
                        s.opcoes.indicecoluna = [];
200
                        var cori = $('input[name="corContorno"]').val();
201
                        s.opcoes.outlinecolor = hex2rgb(cori);
202
                        s.render();
203
                        $.fancybox.close();
204
                };
205
                document.getElementById("mapaBarrasCancela").onclick = function(){
206
                        $.fancybox.close();
207
                        s.fecharMapa();
208
                };
209
                jQuery('select[name="corContorno"]').colourPicker({
210
                                ico:    'js/jquery/jquery.colourPicker.gif',
211
                                title:    false
212
                });
213
        },
214
        coresChapadas: function(s){
215
                this.opcoes.size = 0;
216
                $.fancybox("Cor inicial:  "+this.cores("corInicial")+
217
                                "Cor final:  "+this.cores("corFinal")+
218
                                "Coluna com os valores:<br>"+
219
                                "<select id='coresChapadasColuna' style='border:1px solid #BBBBBB;'>"+this.opcoesColunas()+"</select>" +
220
                                "<br><input type=button value='OK' id='coresChapadasOk' />" +
221
                                "&nbsp; <input type=button value='Cancela' id='coresChapadasCancela' />"
222
                                ,
223
                                {
224
                                        'autoDimensions'    : false,
225
                                        'autoScale'         : false,
226
                                        'height'            :  250,
227
                                        'width'             :  350,
228
                                        'transitionIn'      : 'none',
229
                                        'transitionOut'     : 'none',
230
                                        'showCloseButton'        : false,
231
                                        'modal'                                : false
232
                                }
233
                );
234
                document.getElementById("coresChapadasOk").onclick = function(){
235
                        s.opcoes.tipo = "coresChapadas";
236
                        s.opcoes.size = 0;
237
                        s.opcoes.indicecoluna = [parseInt(document.getElementById("coresChapadasColuna").value,10)];
238
                        var cori = $('input[name="corInicial"]').val();
239
                        var corf = $('input[name="corFinal"]').val();
240
                        s.opcoes.cores = [hex2rgb(cori),hex2rgb(corf)];
241
                        s.render();
242
                        $.fancybox.close();
243
                };
244
                document.getElementById("coresChapadasCancela").onclick = function(){
245
                        $.fancybox.close();
246
                        s.fecharMapa();
247
                };
248
                jQuery('select[name="corInicial"]').colourPicker({
249
                                ico:    'js/jquery/jquery.colourPicker.gif',
250
                                title:    false
251
                });
252
                $('input[name="corInicial"]')[0].value = "ffffc7";
253

    
254
                jQuery('select[name="corFinal"]').colourPicker({
255
                                ico:    'js/jquery/jquery.colourPicker.gif',
256
                                title:    false
257
                });
258
                $('input[name="corFinal"]')[0].value = "643403";
259
        },
260
        raiosProporcionais: function(s){
261
                this.opcoes.size = 10;
262
                $.fancybox("Escolha uma cor:  "+this.cores()+
263
                                "Tamanho inicial do c&iacute;rculo (em pixels):<br><input type=text value='"+this.opcoes.size+"' size=6 id='raiosProporcionaisSize' />" +
264
                                "<br>Coluna com os valores:<br>"+
265
                                "<select id='raiosProporcionaisColuna' style='border:1px solid #BBBBBB;'>"+this.opcoesColunas()+"</select>" +
266
                                "<br><input type=button value='OK' id='raiosProporcionaisOk' />" +
267
                                "&nbsp; <input type=button value='Cancela' id='raiosProporcionaisCancela' />"
268
                                ,
269
                                {
270
                                        'autoDimensions'    : false,
271
                                        'autoScale'         : false,
272
                                        'height'            :  250,
273
                                        'width'             :  350,
274
                                        'transitionIn'      : 'none',
275
                                        'transitionOut'     : 'none',
276
                                        'showCloseButton'        : false,
277
                                        'modal'                                : false
278
                                }
279
                );
280
                document.getElementById("raiosProporcionaisOk").onclick = function(){
281
                        s.opcoes.tipo = "raiosProporcionais";
282
                        s.opcoes.size = document.getElementById("raiosProporcionaisSize").value;
283
                        s.opcoes.indicecoluna = [parseInt(document.getElementById("raiosProporcionaisColuna").value,10)];
284
                        var cor = $('input[name="colour"]').val();
285
                        s.opcoes.cores = [hex2rgb(cor)];
286
                        s.render();
287
                        $.fancybox.close();
288
                };
289
                document.getElementById("raiosProporcionaisCancela").onclick = function(){
290
                        $.fancybox.close();
291
                        s.fecharMapa();
292
                };
293
                jQuery('select[name="colour"]').colourPicker({
294
                                ico:    'js/jquery/jquery.colourPicker.gif',
295
                                title:    false
296
                });
297
        },
298
        circulosProporcionais: function(s){
299
                this.opcoes.size = 10;
300
                $.fancybox("Escolha uma cor:  "+this.cores()+
301
                                "Tamanho inicial do c&iacute;rculo (em pixels):<br><input type=text value='"+this.opcoes.size+"' size=6 id='circulosProporcionaisSize' />" +
302
                                "<br>Coluna com os valores:<br>"+
303
                                "<select id='circulosProporcionaisColuna' style='border:1px solid #BBBBBB;'>"+this.opcoesColunas()+"</select>" +
304
                                "<br><input type=button value='OK' id='circulosProporcionaisOk' />" +
305
                                "&nbsp; <input type=button value='Cancela' id='circulosProporcionaisCancela' />"
306
                                ,
307
                                {
308
                                        'autoDimensions'    : false,
309
                                        'autoScale'         : false,
310
                                        'height'            :  250,
311
                                        'width'             :  350,
312
                                        'transitionIn'      : 'none',
313
                                        'transitionOut'     : 'none',
314
                                        'showCloseButton'        : false,
315
                                        'modal'                                : false
316
                                }
317
                );
318
                document.getElementById("circulosProporcionaisOk").onclick = function(){
319
                        s.opcoes.tipo = "circulosProporcionais";
320
                        s.opcoes.size = document.getElementById("circulosProporcionaisSize").value;
321
                        s.opcoes.indicecoluna = [parseInt(document.getElementById("circulosProporcionaisColuna").value,10)];
322
                        var cor = $('input[name="colour"]').val();
323
                        s.opcoes.cores = [hex2rgb(cor)];
324
                        s.render();
325
                        $.fancybox.close();
326
                };
327
                document.getElementById("circulosProporcionaisCancela").onclick = function(){
328
                        $.fancybox.close();
329
                        s.fecharMapa();
330
                };
331
                jQuery('select[name="colour"]').colourPicker({
332
                                ico:    'js/jquery/jquery.colourPicker.gif',
333
                                title:    false
334
                });
335
        },
336
        calor: function(s){
337
                this.opcoes.size = 10;
338
                $.fancybox("Raio de um ponto em pixels:<br><input type=text value='10' size=6 id='calorRaio' /><br>" +
339
                                "<br>Coluna com os valores:<br>"+
340
                                "<select id='calorColuna' style='border:1px solid #BBBBBB;'>" +
341
                                "<option value=''>---</option>" +
342
                                this.opcoesColunas() +
343
                                "</select>" +
344
                                "<br>Ou valor de cada ponto:<br><input type=text value='1' size=6 id='valorPonto' /><br>" +
345
                                "<br><input type=button value='OK' id='calorOk' />" +
346
                                "&nbsp; <input type=button value='Cancela' id='calorCancela' />"
347
                                ,
348
                                {
349
                                        'autoDimensions'    : false,
350
                                        'autoScale'         : false,
351
                                        'height'            :  250,
352
                                        'width'             :  350,
353
                                        'transitionIn'      : 'none',
354
                                        'transitionOut'     : 'none',
355
                                        'showCloseButton'        : false,
356
                                        'modal'                                : false
357
                                }
358
                );
359
                document.getElementById("calorOk").onclick = function(){
360
                        s.opcoes.tipo = "calor";
361
                        s.opcoes.valorPonto = document.getElementById("valorPonto").value;
362
                        s.opcoes.indicecoluna = [parseInt(document.getElementById("calorColuna").value,10)];
363
                        s.opcoes.raio = document.getElementById("calorRaio").value;
364
                        s.render();
365
                        $.fancybox.close();
366
                };
367
                document.getElementById("calorCancela").onclick = function(){
368
                        $.fancybox.close();
369
                        s.fecharMapa();
370
                };
371
                jQuery('select[name="colour"]').colourPicker({
372
                                ico:    'js/jquery/jquery.colourPicker.gif',
373
                                title:    false
374
                });
375
        },
376

    
377
        opcoesColunas: function(){
378
                var m = this.data.metadata,
379
                        n = m.length,
380
                        i = 0,
381
                        ins = [];
382
                for(i=1;i<n;i++){
383
                        ins.push("<option value='"+m[i].colIndex+"' >"+m[i].colName+"</option>");
384
                }
385
                return ins.join("");
386
        },
387
        cores: function(nome){
388
                if(!nome){
389
                        nome = "colour";
390
                }
391
                var cor = "<div id='jquery-colour-picker-example' >" +
392
                        "<p><select name='"+nome+"'>"+
393
                        "<option value='ffffff' selected='selected'>#ffffff</option>"+
394
                        "<option value='ffccc9'>#ffccc9</option>"+
395
                        "<option value='ffce93'>#ffce93</option>"+
396
                        "<option value='fffc9e'>#fffc9e</option>"+
397
                        "<option value='ffffc7'>#ffffc7</option>"+
398
                        "<option value='9aff99'>#9aff99</option>"+
399
                        "<option value='96fffb'>#96fffb</option>"+
400
                        "<option value='cdffff'>#cdffff</option>"+
401
                        "<option value='cbcefb'>#cbcefb</option>"+
402
                        "<option value='cfcfcf'>#cfcfcf</option>"+
403
                        "<option value='fd6864'>#fd6864</option>"+
404
                        "<option value='fe996b'>#fe996b</option>"+
405
                        "<option value='fffe65'>#fffe65</option>"+
406
                        "<option value='fcff2f'>#fcff2f</option>"+
407
                        "<option value='67fd9a'>#67fd9a</option>"+
408
                        "<option value='38fff8'>#38fff8</option>"+
409
                        "<option value='68fdff'>#68fdff</option>"+
410
                        "<option value='9698ed'>#9698ed</option>"+
411
                        "<option value='c0c0c0'>#c0c0c0</option>"+
412
                        "<option value='fe0000'>#fe0000</option>"+
413
                        "<option value='f8a102'>#f8a102</option>"+
414
                        "<option value='ffcc67'>#ffcc67</option>"+
415
                        "<option value='f8ff00'>#f8ff00</option>"+
416
                        "<option value='34ff34'>#34ff34</option>"+
417
                        "<option value='68cbd0'>#68cbd0</option>"+
418
                        "<option value='34cdf9'>#34cdf9</option>"+
419
                        "<option value='6665cd'>#6665cd</option>"+
420
                        "<option value='9b9b9b'>#9b9b9b</option>"+
421
                        "<option value='cb0000'>#cb0000</option>"+
422
                        "<option value='f56b00'>#f56b00</option>"+
423
                        "<option value='ffcb2f'>#ffcb2f</option>"+
424
                        "<option value='ffc702'>#ffc702</option>"+
425
                        "<option value='32cb00'>#32cb00</option>"+
426
                        "<option value='00d2cb'>#00d2cb</option>"+
427
                        "<option value='3166ff'>#3166ff</option>"+
428
                        "<option value='6434fc'>#6434fc</option>"+
429
                        "<option value='656565'>#656565</option>"+
430
                        "<option value='9a0000'>#9a0000</option>"+
431
                        "<option value='ce6301'>#ce6301</option>"+
432
                        "<option value='cd9934'>#cd9934</option>"+
433
                        "<option value='999903'>#999903</option>"+
434
                        "<option value='009901'>#009901</option>"+
435
                        "<option value='329a9d'>#329a9d</option>"+
436
                        "<option value='3531ff'>#3531ff</option>"+
437
                        "<option value='6200c9'>#6200c9</option>"+
438
                        "<option value='343434'>#343434</option>"+
439
                        "<option value='680100'>#680100</option>"+
440
                        "<option value='963400'>#963400</option>"+
441
                        "<option value='986536'>#986536</option>"+
442
                        "<option value='646809'>#646809</option>"+
443
                        "<option value='036400'>#036400</option>"+
444
                        "<option value='34696d'>#34696d</option>"+
445
                        "<option value='00009b'>#00009b</option>"+
446
                        "<option value='303498'>#303498</option>"+
447
                        "<option value='000000'>#000000</option>"+
448
                        "<option value='330001'>#330001</option>"+
449
                        "<option value='643403'>#643403</option>"+
450
                        "<option value='663234'>#663234</option>"+
451
                        "<option value='343300'>#343300</option>"+
452
                        "<option value='013300'>#013300</option>"+
453
                        "<option value='003532'>#003532</option>"+
454
                        "<option value='010066'>#010066</option>"+
455
                        "<option value='340096'>#340096</option></select></p></div>";
456
                return cor;
457
        },
458
        render: function() {
459
                if (! $(this.workspace.toolbar.el).find('.i3GeoMap').hasClass('on')) {
460
                        this.fecha();
461
                        this.fecharMapa();
462
                        return;
463
                }
464
                var series=[];
465
                //nome das colunas
466
                var column=[];
467
                var columnGeoCod = "";
468
                var indiceColumnGeoCod = "";
469
                var metadata = [];
470
                var indicecoluna = "";
471
                for(var i=0; i < this.data.metadata.length; i++){
472
                        if(this.opcoes.indicecoluna == this.data.metadata[i].colIndex){
473
                                indicecoluna = i;
474
                        }
475
                        column[i] = this.data.metadata[i].colName;
476
                        //se o usuario escolheu uma coluna, as demais sao marcadas com o nome vazio
477
                        if(this.opcoes.indicecoluna != ""){
478
                                //if(this.opcoes.indicecoluna.indexOf(this.data.metadata[i].colIndex) != -1)
479
                                if(this.opcoes.indicecoluna.indexOf(this.data.metadata[i].colIndex) != -1 || column[i].search("GeoCod") > 0){
480
                                        metadata.push(this.data.metadata[i]);
481
                                }
482
                        }
483
                        else{
484
                                metadata.push(this.data.metadata[i]);
485
                        }
486
                        //pega a coluna GeoCod
487
                        if(column[i].search("GeoCod") > 0){
488
                                //verifica se tem mais de um GeoCod
489
                                if(columnGeoCod != ""){
490
                                        alert("Existem mais de uma coluna GeoCod!");
491
                                        this.fecha();
492
                                        $.fancybox.close();
493
                                        return;
494
                                }
495
                                columnGeoCod = column[i];
496
                                indiceColumnGeoCod = i;
497
                        }
498
                }
499
                if(columnGeoCod === ""){
500
                        alert("Coluna GeoCod nao existe!");
501
                        this.fecha();
502
                        $.fancybox.close();
503
                        //this.nav.find('input').hide();
504
                        return;
505
                }
506
                series[0]=column;
507
                var dados = [];
508
                if(indicecoluna != ""){
509
                        if (this.data.resultset.length > 0 ) {
510
                                $.each(this.data.resultset, function(key, value) {
511
                                        dados.push([value[indiceColumnGeoCod],value[indicecoluna]]);
512
                                });
513
                        }
514
                }
515
                else{
516
                        if (this.data.resultset.length > 0 ) {
517
                                $.each(this.data.resultset, function(key, value) {
518
                                        var aux=value[0]+'';
519
                                        aux = aux.split(" \/ ");
520
                                        aux = aux[indiceColumnGeoCod];
521
                                        value[0]=aux;
522
                                        dados.push(value);
523
                                });
524
                        }
525
                }
526
                //preenche os dados do formulario
527
                this.opcoes.locaplic = parametroUrl("locaplic");
528
                this.opcoes.mapext = parametroUrl("mapext");
529
                //alert(JSON.stringify(this.opcoes))
530
                document.getElementById("formi3GeoMapOpcoes").value = JSON.stringify(this.opcoes);
531
                document.getElementById("formi3GeoMapdados").value = JSON.stringify(dados);
532
                document.getElementById("formi3GeoMapmetadados").value = JSON.stringify(metadata);
533
                document.getElementById("iframei3GeoMap").style.height = parseInt($(this.workspace.el).find('.workspace_results')[0].style.height) - 50 +"px";
534
                document.getElementById("formi3GeoMap").submit();
535
                document.getElementById("iframei3GeoMap").style.display = "block";
536
        },
537
        receive_data: function(args) {
538
                if ($(this.workspace.toolbar.el).find('.i3GeoMap').hasClass('on')) {
539
                        this.fecha();
540
                        this.fecharMapa();
541
                        return false;
542
                }
543
                return _.delay(this.process_data, 0, args);
544
        },
545
        process_data: function(args) {
546
                this.data = {};
547
                this.data.resultset = [];
548
                this.data.metadata = [];
549
                this.data.height = 0;
550
                this.data.width = 0;
551
                if (! $(this.workspace.toolbar.el).find('.i3GeoMap').hasClass('on')) {
552
                        this.fecha();
553
                        this.fecharMapa();
554
                        return;
555
                }
556
                var cellset = args.data.cellset;
557
                var nome = "";
558
                var colunaGeocod = "";
559
                var colunaGeocodIndice = "";
560
                if (cellset && cellset.length > 0) {
561
                        var lowest_level = 0;
562
                        var data_start = 0;
563
                        for (var row = 0; data_start == 0 && row < cellset.length; row++) {
564
                                this.data.metadata = [];
565
                                for (var field = 0; field < cellset[row].length; field++) {
566
                                        var firstHeader = [];
567

    
568
                                        while (cellset[row][field].type == "COLUMN_HEADER" && cellset[row][field].value == "null") {
569
                                                row++;
570
                                        }
571
                                        nome = "";
572
                                        //colunaGeocod = "";
573
                                        //colunaGeocodIndice = "";
574
                                        if (cellset[row][field].type == "ROW_HEADER_HEADER") {
575
                                                while(cellset[row][field].type == "ROW_HEADER_HEADER") {
576
                                                        nome = cellset[row][field].value;
577
                                                        firstHeader.push(nome);
578
                                                        if(nome.search("GeoCod") > 0){
579
                                                                colunaGeocod = nome;
580
                                                                colunaGeocodIndice = field;
581
                                                        }
582
                                                        field++;
583
                                                }
584
                                                tipo = "linha";
585
                                                identificador = cellset[row][field-1].properties.level;
586
                                                if(firstHeader.join('/').search("GeoCod") > 0){
587
                                                        this.data.metadata.push({
588
                                                                colIndex: 0,
589
                                                                colType: "String",
590
                                                                colName: colunaGeocod,
591
                                                                tipo: tipo,
592
                                                                identificador: identificador
593
                                                        });
594
                                                        lowest_level = field - 1;
595
                                                }
596

    
597
                                        }
598
                                        if (cellset[row][field].type == "COLUMN_HEADER" && cellset[row][field].value != "null") {
599
                                                var lowest_col_header = 0;
600
                                                var colheader = [];
601
                                                while(lowest_col_header <= row) {
602
                                                        colheader.push(cellset[lowest_col_header][field].value);
603
                                                        lowest_col_header++;
604
                                                }
605

    
606
                                                tipo = "coluna";
607
                                                identificador = cellset[row][lowest_col_header].properties.uniquename;
608

    
609
                                                this.data.metadata.push({
610
                                                        colIndex: field - lowest_level + 1,
611
                                                        colType: "Numeric",
612
                                                        colName: colheader.join('/'),
613
                                                        tipo: tipo,
614
                                                        identificador: identificador
615
                                                });
616
                                                data_start = row+1;
617
                                        }
618
                                }
619
                        }
620

    
621
                        for (var row = data_start; row < cellset.length; row++) {
622
                                if (cellset[row][0].value !== "") {
623
                                        var record = [];
624
                                        this.data.width = cellset[row].length - lowest_level + 1;
625
                                        var label = "";
626
                                        if(cellset[row][colunaGeocodIndice]){
627
                                                label = cellset[row][colunaGeocodIndice].value;
628
                                        }
629
                                        /*
630
                                        if(label in labelsSet) {
631
                                                labelsSet[label] = labelsSet[label]+1;
632
                                                label = label + ' [' + (labelsSet[label] + 1) + ']';
633
                                        } else {
634
                                                labelsSet[label] = 0;
635
                                        }
636
                                        */
637
                                        record.push(label);
638

    
639
                                        for (var col = lowest_level + 1; col < cellset[row].length; col++) {
640
                                                var cell = cellset[row][col];
641
                                                var value = cell.value || 0;
642
                                                // check if the resultset contains the raw value, if not try to parse the given value
643
                                                var raw = cell.properties.raw;
644
                                                if (raw && raw !== "null") {
645
                                                                value = parseFloat(raw);
646
                                                } else if (typeof(cell.value) !== "number" && parseFloat(cell.value.replace(/[^a-zA-Z 0-9.]+/g,''))) {
647
                                                                value = parseFloat(cell.value.replace(/[^a-zA-Z 0-9.]+/g,''));
648
                                                }
649
                                                record.push(value);
650
                                        }
651
                                        this.data.resultset.push(record);
652
                                }
653
                        }
654
                        //makeSureUniqueLabels(this.data.resultset);
655
                        this.hasProcessed = true;
656
                        this.data.height = this.data.resultset.length;
657
                        //this.cccOptions = this.getQuickOptions(this.cccOptions);
658
                        //this.render();
659
                } else {
660
                        $(this.el).find('.canvas_wrapper').text("No results").show();
661
                        this.workspace.processing.hide();
662
                        this.workspace.adjust();
663
                }
664
        }
665
});
666
function hex2rgb(hex,tipo) {
667
        if(!tipo){
668
                tipo = "array";
669
        }
670
        if (hex[0]=="#") hex=hex.substr(1);
671
        if (hex.length==3) {
672
                var temp=hex; hex='';
673
                temp = /^([a-f0-9])([a-f0-9])([a-f0-9])$/i.exec(temp).slice(1);
674
                for (var i=0;i<3;i++) hex+=temp[i]+temp[i];
675
        }
676
        var triplets = /^([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i.exec(hex).slice(1);
677
        if(tipo == "array"){
678
                return {
679
                        red:   parseInt(triplets[0],16),
680
                        green: parseInt(triplets[1],16),
681
                        blue:  parseInt(triplets[2],16)
682
                };
683
        }
684
        else{
685
                return parseInt(triplets[0],16)+ " " + parseInt(triplets[1],16) + " " + parseInt(triplets[2],16);
686
        }
687
}
688
function coresHex2Rgb(lista){
689
        var n = lista.length,
690
                i =0,
691
                f = [];
692
        for(i=0;i<n;i++){
693
                f.push(hex2rgb(lista[i]," "));
694
        }
695
        return f;
696
}
697
function loadCSS(file){
698
        var headID = document.getElementsByTagName("head")[0];
699
        var cssNode = document.createElement('link');
700
        cssNode.type = 'text/css';
701
        cssNode.rel = 'stylesheet';
702
        cssNode.href = file;
703
        cssNode.media = 'screen';
704
        headID.appendChild(cssNode);
705
}
706

    
707
function loadJS(file){
708
        var headID = document.getElementsByTagName("head")[0];
709
        var newScript = document.createElement('script');
710
        newScript.type = 'text/javascript';
711
        newScript.src = file;
712
        headID.appendChild(newScript);
713
}
714

    
715
function messageUser(msg){
716
        $("#message").html(msg).show('slow');
717
        setTimeout(function(){ jQuery("#message").hide('slow'); }, 2000);
718
}
719

    
720
function parametroUrl(parametro){
721
        var params = window.location.search.replace("?","").split("&"),
722
                n = params.length,
723
                valor = "",
724
                i,param;
725
        for(i=0;i<n;i++){
726
                param = params[i].split("=");
727
                if(param[0] == parametro){
728
                        valor = param[1];
729
                }
730
        }
731
        return valor;
732
}
733
/**
734
 * Start Plugin
735
 */
736
Saiku.events.bind('session:new', function(session) {
737
        function new_workspace(args) {
738
                // Add stats element
739
                        if (typeof args.workspace.i3GeoMap == "undefined") {
740
                                args.workspace.i3GeoMap = new i3GeoMap({ workspace: args.workspace });
741
                        }
742
        }
743

    
744
        function clear_workspace(args) {
745
                        if (typeof args.workspace.i3GeoMap != "undefined") {
746
                                $(args.workspace.i3GeoMap.nav).hide();
747
                                        $(args.workspace.i3GeoMap.el).parents().find('.workspace_results table').show();
748
                                        $(args.workspace.i3GeoMap.el).hide();
749
                        }
750
        }
751

    
752

    
753
        // Attach stats to existing tabs
754
        for(var i = 0; i < Saiku.tabs._tabs.length; i++) {
755
                        var tab = Saiku.tabs._tabs[i];
756
                        new_workspace({
757
                                        workspace: tab.content
758
                        });
759
        };
760

    
761
        // Attach stats to future tabs
762
        Saiku.session.bind("workspace:new", new_workspace);
763
        Saiku.session.bind("workspace:clear", clear_workspace);
764
});