| @@ -1,11 +1,4 @@ | ||
| 1 | -/* global google */ | |
| 2 | -/* global visualizer */ | |
| 3 | -/* global console */ | |
| 4 | - | |
| 5 | -// this will store the images for each chart rendered. | |
| 6 | -var __visualizer_chart_images = []; | |
| 7 | - | |
| 8 | 1 | (function(v, g) { |
| 9 | 2 | var gv; |
| 10 | 3 | |
| 11 | 4 | v.objects = {}; |
| @@ -18,40 +11,24 @@ | ||
| 18 | 11 | data = chart.data; |
| 19 | 12 | settings = chart.settings; |
| 20 | 13 | |
| 21 | 14 | container = document.getElementById(id); |
| 22 | - if (container == null) { | |
| 23 | - return; | |
| 24 | - } | |
| 25 | 15 | table = new gv.DataTable({cols: series}); |
| 26 | 16 | |
| 27 | 17 | render = v.objects[id] || null; |
| 28 | 18 | if (!render) { |
| 29 | - switch (chart.type) { | |
| 30 | - case "gauge": | |
| 31 | - case "table": | |
| 32 | - case "timeline": | |
| 33 | - render = chart.type.charAt(0).toUpperCase() + chart.type.slice(1); | |
| 34 | - break; | |
| 35 | - default: | |
| 36 | - render = chart.type.charAt(0).toUpperCase() + chart.type.slice(1) + 'Chart'; | |
| 37 | - break; | |
| 38 | - } | |
| 19 | + render = chart.type == 'gauge' | |
| 20 | + ? 'Gauge' | |
| 21 | + : chart.type.charAt(0).toUpperCase() + chart.type.slice(1) + 'Chart'; | |
| 39 | 22 | |
| 40 | 23 | render = new gv[render](container); |
| 41 | 24 | } |
| 42 | 25 | |
| 43 | - if (settings['animation'] && parseInt(settings['animation']['startup']) === 1) | |
| 44 | - { | |
| 45 | - settings['animation']['startup'] = true; | |
| 46 | - settings['animation']['duration'] = parseInt(settings['animation']['duration']); | |
| 47 | - } | |
| 48 | - | |
| 49 | - switch (chart.type) { | |
| 26 | + switch (v.charts[id].type) { | |
| 50 | 27 | case 'pie': |
| 51 | 28 | if (settings.slices) { |
| 52 | 29 | for (i in settings.slices) { |
| 53 | - if (settings.slices[i]['color'] === '') { | |
| 30 | + if (settings.slices[i]['color'] == '') { | |
| 54 | 31 | delete settings.slices[i]['color']; |
| 55 | 32 | } |
| 56 | 33 | } |
| 57 | 34 | } |
| @@ -63,65 +40,28 @@ | ||
| 63 | 40 | case 'scatter': |
| 64 | 41 | case 'candlestick': |
| 65 | 42 | if (settings.series) { |
| 66 | 43 | for (i in settings.series) { |
| 67 | - if (settings.series[i]['color'] === '') { | |
| 44 | + if (settings.series[i]['color'] == '') { | |
| 68 | 45 | delete settings.series[i]['color']; |
| 69 | 46 | } |
| 70 | 47 | } |
| 71 | 48 | } |
| 72 | - | |
| 73 | - if (settings.series && settings.legend && settings.legend.position === "left") | |
| 74 | - { | |
| 75 | - settings.targetAxisIndex = 1; | |
| 76 | - } | |
| 77 | 49 | break; |
| 78 | 50 | case 'geo': |
| 79 | - if (settings.region !== undefined && settings.region.replace(/^\s+|\s+$/g, '') === '') { | |
| 51 | + if (settings.region != undefined && settings.region.replace(/^\s+|\s+$/g, '') == '') { | |
| 80 | 52 | settings['region'] = 'world'; |
| 81 | 53 | } |
| 82 | 54 | break; |
| 83 | - case 'table': | |
| 84 | - if (parseInt(settings['pagination']) !== 1) | |
| 85 | - { | |
| 86 | - delete settings['pageSize']; | |
| 87 | - } | |
| 88 | - break; | |
| 89 | 55 | case 'gauge': |
| 90 | 56 | break; |
| 91 | - case 'timeline': | |
| 92 | - settings['timeline'] = []; | |
| 93 | - settings['timeline']['groupByRowLabel'] = settings['groupByRowLabel'] ? true : false; | |
| 94 | - settings['timeline']['colorByRowLabel'] = settings['colorByRowLabel'] ? true : false; | |
| 95 | - settings['timeline']['showRowLabels'] = settings['showRowLabels'] ? true : false; | |
| 96 | - if(settings['singleColor'] !== '') { | |
| 97 | - settings['timeline']['singleColor'] = settings['singleColor']; | |
| 98 | - } | |
| 99 | - break; | |
| 100 | - case 'combo': | |
| 101 | - if (settings.series) { | |
| 102 | - for (i in settings.series) { | |
| 103 | - if (settings.series[i]['type'] === '') { | |
| 104 | - delete settings.series[i]['type']; | |
| 105 | - } | |
| 106 | - if (settings.series[i]['color'] === '') { | |
| 107 | - delete settings.series[i]['color']; | |
| 108 | - } | |
| 109 | - } | |
| 110 | - } | |
| 111 | - | |
| 112 | - if (settings.series && settings.legend && settings.legend.position === "left") | |
| 113 | - { | |
| 114 | - settings.targetAxisIndex = 1; | |
| 115 | - } | |
| 116 | - break; | |
| 117 | 57 | default: |
| 118 | 58 | return; |
| 119 | 59 | } |
| 120 | 60 | |
| 121 | - if (series[0] && (series[0].type === 'date' || series[0].type === 'datetime')) { | |
| 61 | + if (series[0] && (series[0].type == 'date' || series[0].type == 'datetime')) { | |
| 122 | 62 | axis = false; |
| 123 | - switch (chart.type) { | |
| 63 | + switch (v.charts[id].type) { | |
| 124 | 64 | case 'line': |
| 125 | 65 | case 'area': |
| 126 | 66 | case 'scatter': |
| 127 | 67 | case 'candlestick': |
| @@ -147,28 +87,12 @@ | ||
| 147 | 87 | } |
| 148 | 88 | } |
| 149 | 89 | } |
| 150 | 90 | |
| 151 | - if(settings.hAxis){ | |
| 152 | - if(settings.hAxis.textStyle && settings.hAxis.textStyle !== ''){ | |
| 153 | - if(typeof(settings.hAxis.textStyle) === "string") { | |
| 154 | - settings.hAxis.textStyle = {color: settings.hAxis.textStyle}; | |
| 155 | - } | |
| 156 | - } | |
| 157 | - } | |
| 158 | - | |
| 159 | - if(settings.vAxis){ | |
| 160 | - if(settings.vAxis.textStyle && settings.vAxis.textStyle !== ''){ | |
| 161 | - if(typeof(settings.vAxis.textStyle) === "string") { | |
| 162 | - settings.vAxis.textStyle = {color: settings.vAxis.textStyle}; | |
| 163 | - } | |
| 164 | - } | |
| 165 | - } | |
| 166 | - | |
| 167 | 91 | for (i = 0; i < data.length; i++) { |
| 168 | 92 | row = []; |
| 169 | 93 | for (j = 0; j < series.length; j++) { |
| 170 | - if (series[j].type === 'date' || series[j].type === 'datetime') { | |
| 94 | + if (series[j].type == 'date' || series[j].type == 'datetime') { | |
| 171 | 95 | date = new Date(data[i][j]); |
| 172 | 96 | data[i][j] = null; |
| 173 | 97 | if (Object.prototype.toString.call(date) === "[object Date]") { |
| 174 | 98 | if (!isNaN(date.getTime())) { |
| @@ -183,9 +107,9 @@ | ||
| 183 | 107 | |
| 184 | 108 | if (settings.series) { |
| 185 | 109 | for (i = 0; i < settings.series.length; i++) { |
| 186 | 110 | format = settings.series[i].format; |
| 187 | - if (!format || format === '' || !series[i + 1]) { | |
| 111 | + if (!format || format == '') { | |
| 188 | 112 | continue; |
| 189 | 113 | } |
| 190 | 114 | |
| 191 | 115 | formatter = null; |
| @@ -203,41 +127,13 @@ | ||
| 203 | 127 | if (formatter) { |
| 204 | 128 | formatter.format(table, i + 1); |
| 205 | 129 | } |
| 206 | 130 | } |
| 207 | - } else if (chart.type === 'pie' && settings.format && settings.format !== '') { | |
| 208 | - formatter = new g.visualization.NumberFormat({pattern: settings.format}); | |
| 209 | - formatter.format(table, 1); | |
| 210 | - } | |
| 131 | + } | |
| 211 | 132 | |
| 212 | - v.override(settings); | |
| 213 | - | |
| 214 | - g.visualization.events.addListener(render, 'ready', function () { | |
| 215 | - var arr = id.split('-'); | |
| 216 | - try{ | |
| 217 | - var img = render.getImageURI(); | |
| 218 | - __visualizer_chart_images[ arr[0] + '-' + arr[1] ] = img; | |
| 219 | - jQuery('body').trigger('visualizer:render:chart', {id: arr[1], image: img}); | |
| 220 | - }catch(error){ | |
| 221 | - console.warn('render.getImageURI not defined for ' + arr[0] + '-' + arr[1]); | |
| 222 | - } | |
| 223 | - }); | |
| 224 | - | |
| 225 | 133 | render.draw(table, settings); |
| 226 | 134 | }; |
| 227 | 135 | |
| 228 | - v.override = function(settings) { | |
| 229 | - if (settings.manual) { | |
| 230 | - try{ | |
| 231 | - var options = JSON.parse(settings.manual); | |
| 232 | - jQuery.extend(settings, options); | |
| 233 | - delete settings.manual; | |
| 234 | - }catch(error){ | |
| 235 | - console.error("Error while adding manual configuration override " + settings.manual); | |
| 236 | - } | |
| 237 | - } | |
| 238 | - }; | |
| 239 | - | |
| 240 | 136 | v.render = function() { |
| 241 | 137 | for (var id in (v.charts || {})) { |
| 242 | 138 | v.renderChart(id); |
| 243 | 139 | } |
| @@ -242,10 +138,10 @@ | ||
| 242 | 138 | v.renderChart(id); |
| 243 | 139 | } |
| 244 | 140 | }; |
| 245 | 141 | |
| 246 | - g.charts.load("current", {packages: ["corechart", "geochart", "gauge", "table", "timeline"], mapsApiKey: v.map_api_key, 'language' : v.language}); | |
| 247 | - g.charts.setOnLoadCallback(function() { | |
| 142 | + g.load("visualization", "1", {packages: ["corechart", "geochart", "gauge"]}); | |
| 143 | + g.setOnLoadCallback(function() { | |
| 248 | 144 | gv = g.visualization; |
| 249 | 145 | v.render(); |
| 250 | 146 | }); |
| 251 | 147 | })(visualizer, google); |
| @@ -257,154 +153,6 @@ | ||
| 257 | 153 | $(window).resize(function() { |
| 258 | 154 | clearTimeout(resizeTimeout); |
| 259 | 155 | resizeTimeout = setTimeout(v.render, 100); |
| 260 | 156 | }); |
| 261 | - | |
| 262 | - resizeHiddenContainers(true); | |
| 263 | - initActionsButtons(); | |
| 264 | - }); | |
| 265 | - | |
| 266 | - $(window).load(function(){ | |
| 267 | - resizeHiddenContainers(true); | |
| 268 | - }); | |
| 269 | - | |
| 270 | - function initActionsButtons() { | |
| 271 | - if($('a.visualizer-action[data-visualizer-type=copy]').length > 0) { | |
| 272 | - var clipboard = new Clipboard('a.visualizer-action[data-visualizer-type=copy]'); // jshint ignore:line | |
| 273 | - clipboard.on('success', function(e) { | |
| 274 | - window.alert(v.i10n['copied']); | |
| 275 | - }); | |
| 276 | - } | |
| 277 | - $('a.visualizer-action[data-visualizer-type!=copy]').on('click', function(e) { | |
| 278 | - var type = $(this).attr( 'data-visualizer-type' ); | |
| 279 | - var chart = $(this).attr( 'data-visualizer-chart-id' ); | |
| 280 | - var lock = $('.visualizer-front.visualizer-front-' + chart); | |
| 281 | - lock.lock(); | |
| 282 | - e.preventDefault(); | |
| 283 | - $.ajax({ | |
| 284 | - url : v.rest_url.replace('#id#', chart).replace('#type#', type), | |
| 285 | - success: function(data) { | |
| 286 | - if (data && data.data) { | |
| 287 | - switch(type){ | |
| 288 | - case 'csv': | |
| 289 | - var a = document.createElement("a"); | |
| 290 | - document.body.appendChild(a); | |
| 291 | - a.style = "display: none"; | |
| 292 | - var blob = new Blob([data.data.csv], {type: $(this).attr( 'data-visualizer-mime' ) }), | |
| 293 | - url = window.URL.createObjectURL(blob); | |
| 294 | - a.href = url; | |
| 295 | - a.download = data.data.name; | |
| 296 | - a.click(); | |
| 297 | - setTimeout(function () { | |
| 298 | - window.URL.revokeObjectURL(url); | |
| 299 | - }, 100); | |
| 300 | - break; | |
| 301 | - case 'xls': | |
| 302 | - var $a = $("<a>"); | |
| 303 | - $a.attr("href",data.data.csv); | |
| 304 | - $("body").append($a); | |
| 305 | - $a.attr("download",data.data.name); | |
| 306 | - $a[0].click(); | |
| 307 | - $a.remove(); | |
| 308 | - break; | |
| 309 | - case 'print': | |
| 310 | - var iframe = $('<iframe>').attr("name", "print-visualization").attr("id", "print-visualization").css("position", "absolute"); | |
| 311 | - iframe.appendTo($('body')); | |
| 312 | - var iframe_doc = iframe.get(0).contentWindow || iframe.get(0).contentDocument.document || iframe.get(0).contentDocument; | |
| 313 | - iframe_doc.document.open(); | |
| 314 | - iframe_doc.document.write(data.data.csv); | |
| 315 | - iframe_doc.document.close(); | |
| 316 | - setTimeout(function(){ | |
| 317 | - window.frames['print-visualization'].focus(); | |
| 318 | - window.frames['print-visualization'].print(); | |
| 319 | - iframe.remove(); | |
| 320 | - }, 500); | |
| 321 | - break; | |
| 322 | - default: | |
| 323 | - if(window.visualizer_perform_action) { | |
| 324 | - window.visualizer_perform_action(type, chart, data.data); | |
| 325 | - } | |
| 326 | - break; | |
| 327 | - } | |
| 328 | - } | |
| 329 | - lock.unlock(); | |
| 330 | - } | |
| 331 | - }); | |
| 332 | - }); | |
| 333 | - } | |
| 334 | - | |
| 335 | - function resizeHiddenContainers(everytime){ | |
| 336 | - $(".visualizer-front").parents().each(function(){ | |
| 337 | - if(!$(this).is(":visible") && !$(this).hasClass("visualizer-hidden-container")){ | |
| 338 | - $(this).addClass("visualizer-hidden-container"); | |
| 339 | - } | |
| 340 | - }); | |
| 341 | - | |
| 342 | - var mutateObserver = new MutationObserver(function(records) { | |
| 343 | - records.forEach(function(record) { | |
| 344 | - if(record.attributeName === "style" || record.attributeName === "class"){ | |
| 345 | - var element = $(record.target); | |
| 346 | - var displayStyle = window.getComputedStyle(element[0]).getPropertyValue("display"); | |
| 347 | - if(element.hasClass("visualizer-hidden-container-resized") || displayStyle === "none") { return ; } | |
| 348 | - element.find(".visualizer-front").resize(); | |
| 349 | - if(!everytime) { | |
| 350 | - element.addClass("visualizer-hidden-container-resized"); | |
| 351 | - } | |
| 352 | - } | |
| 353 | - }); | |
| 354 | - }); | |
| 355 | - | |
| 356 | - $('.visualizer-hidden-container').each(function(){ | |
| 357 | - mutateObserver.observe($(this)[0], {attributes: true}); | |
| 358 | - }); | |
| 359 | - } | |
| 360 | - | |
| 361 | -})(jQuery, visualizer); | |
| 362 | - | |
| 363 | -(function ($) { | |
| 364 | - $.fn.lock = function () { | |
| 365 | - $(this).each(function () { | |
| 366 | - var $this = $(this); | |
| 367 | - var position = $this.css('position'); | |
| 368 | - | |
| 369 | - if (!position) { | |
| 370 | - position = 'static'; | |
| 371 | - } | |
| 372 | - | |
| 373 | - switch (position) { | |
| 374 | - case 'absolute': | |
| 375 | - case 'relative': | |
| 376 | - break; | |
| 377 | - default: | |
| 378 | - $this.css('position', 'relative'); | |
| 379 | - break; | |
| 380 | - } | |
| 381 | - $this.data('position', position); | |
| 382 | - | |
| 383 | - var width = $this.width(), | |
| 384 | - height = $this.height(); | |
| 385 | - | |
| 386 | - var locker = $('<div class="locker"></div>'); | |
| 387 | - locker.width(width).height(height); | |
| 388 | - | |
| 389 | - var loader = $('<div class="locker-loader"></div>'); | |
| 390 | - loader.width(width).height(height); | |
| 391 | - | |
| 392 | - locker.append(loader); | |
| 393 | - $this.append(locker); | |
| 394 | - $(window).resize(function () { | |
| 395 | - $this.find('.locker,.locker-loader').width($this.width()).height($this.height()); | |
| 396 | - }); | |
| 397 | - }); | |
| 398 | - | |
| 399 | - return $(this); | |
| 400 | - }; | |
| 401 | - | |
| 402 | - $.fn.unlock = function () { | |
| 403 | - $(this).each(function () { | |
| 404 | - $(this).find('.locker').remove(); | |
| 405 | - $(this).css('position', $(this).data('position')); | |
| 406 | - }); | |
| 407 | - | |
| 408 | - return $(this); | |
| 409 | - }; | |
| 410 | -})(jQuery); | |
| 157 | + }); | |
| 158 | +})(jQuery, visualizer); | |