| @@ -4,8 +4,9 @@ | ||
| 4 | 4 | |
| 5 | 5 | // this will store the images for each chart rendered. |
| 6 | 6 | var __visualizer_chart_images = []; |
| 7 | 7 | var chartWrapperError = []; |
| 8 | +var isResizeRequest = false; | |
| 8 | 9 | |
| 9 | 10 | (function($) { |
| 10 | 11 | var gv; |
| 11 | 12 | var all_charts, objects; |
| @@ -12,22 +13,40 @@ | ||
| 12 | 13 | // so that we know which charts belong to our library. |
| 13 | 14 | var rendered_charts = []; |
| 14 | 15 | |
| 15 | 16 | function renderChart(id) { |
| 17 | + | |
| 18 | + if ( ! all_charts || 0 === Object.keys( all_charts ).length ) { | |
| 19 | + return; | |
| 20 | + } | |
| 21 | + | |
| 16 | 22 | var chart = all_charts[id]; |
| 17 | 23 | var hasAnnotation = false; |
| 18 | 24 | |
| 25 | + if ( ! chart ) { | |
| 26 | + return; | |
| 27 | + } | |
| 28 | + | |
| 29 | + if ( chart.library && chart.library !== 'google' && chart.library !== 'GoogleCharts' ) { | |
| 30 | + return; | |
| 31 | + } | |
| 32 | + | |
| 19 | 33 | // re-render the chart only if it doesn't have annotations and it is on the front-end |
| 20 | 34 | // this is to prevent the chart from showing "All series on a given axis must be of the same data type" during resize. |
| 21 | 35 | // remember, some charts do not support annotations so they should not be included in this. |
| 22 | 36 | var no_annotation_charts = ['tabular', 'timeline', 'gauge', 'geo', 'bubble', 'candlestick']; |
| 23 | 37 | if ( undefined !== chart.settings && undefined !== chart.settings.series && undefined === chart.settings.series.length ) { |
| 24 | - var chartSeries = []; | |
| 25 | - var chartSeriesValue = Object.values( chart.settings.series ); | |
| 26 | - $.each( Object.keys( chart.settings.series ), function( index, element ) { | |
| 27 | - chartSeries[element] = chartSeriesValue[index]; | |
| 28 | - } ); | |
| 29 | - chart.settings.series = chartSeries; | |
| 38 | + var seriesKeys = Object.keys( chart.settings.series ); | |
| 39 | + // Only convert when keys are numeric indices (PHP JSON-encoded array). | |
| 40 | + // String keys (e.g. named series from manual config) must be left as-is. | |
| 41 | + if ( seriesKeys.every( function( k ) { return ! isNaN( k ); } ) ) { | |
| 42 | + var chartSeries = []; | |
| 43 | + var chartSeriesValue = Object.values( chart.settings.series ); | |
| 44 | + $.each( seriesKeys, function( index, element ) { | |
| 45 | + chartSeries[ element ] = chartSeriesValue[ index ]; | |
| 46 | + } ); | |
| 47 | + chart.settings.series = chartSeries; | |
| 48 | + } | |
| 30 | 49 | } |
| 31 | 50 | if(id !== 'canvas' && typeof chart.series !== 'undefined' && typeof chart.settings.series !== 'undefined' && ! no_annotation_charts.includes(chart.type) ) { |
| 32 | 51 | hasAnnotation = chart.series.length - chart.settings.series.length > 1; |
| 33 | 52 | } |
| @@ -39,13 +58,14 @@ | ||
| 39 | 58 | |
| 40 | 59 | function renderSpecificChart(id, chart) { |
| 41 | 60 | var render, container, series, data, table, settings, i, j, row, date, axis, property, format, formatter; |
| 42 | 61 | |
| 43 | - if ( $('#' + id).hasClass('visualizer-chart-loaded') || ( 'canvas' !== id && $('#' + id).children( ':not(.loader)' ).length > 0 ) ) { | |
| 62 | + if( chart.library !== 'google' ) { | |
| 44 | 63 | return; |
| 45 | 64 | } |
| 46 | - | |
| 47 | - if(chart.library !== 'google'){ | |
| 65 | + | |
| 66 | + // Bail if the chart is already rendered or is being rendered. | |
| 67 | + if ( ! window.isResizeRequest && ( $('#' + id).hasClass('visualizer-chart-loaded') || ( 'canvas' !== id && $('#' + id).children( ':not(.loader, style)' ).length > 0 ) ) ) { | |
| 48 | 68 | return; |
| 49 | 69 | } |
| 50 | 70 | rendered_charts[id] = 'yes'; |
| 51 | 71 | |
| @@ -116,8 +136,13 @@ | ||
| 116 | 136 | { |
| 117 | 137 | settings['animation']['startup'] = true; |
| 118 | 138 | settings['animation']['duration'] = parseInt(settings['animation']['duration']); |
| 119 | 139 | } |
| 140 | + if ( settings['controls'] ) { | |
| 141 | + settings['controls']['ui']['allowMultiple'] = 'true' === settings['controls']['allowMultiple'] ? true : false; | |
| 142 | + settings['controls']['ui']['allowTyping'] = 'true' === settings['controls']['allowTyping'] ? true : false; | |
| 143 | + settings['controls']['ui']['showRangeValues'] = 'true' === settings['controls']['showRangeValues'] ? true : false; | |
| 144 | + } | |
| 120 | 145 | |
| 121 | 146 | // mark roles for series that have specified a role |
| 122 | 147 | // and then remove them from future processing |
| 123 | 148 | // and also adjust the indices of the series array so that |
| @@ -335,21 +360,16 @@ | ||
| 335 | 360 | format_data(id, table, series[i + 1].type, settings.series[i].format, i + 1); |
| 336 | 361 | } |
| 337 | 362 | break; |
| 338 | 363 | default: |
| 339 | - for (i = 0; i < settings.series.length; i++) { | |
| 340 | - if (!series[i + 1] || typeof settings.series[i] === 'undefined') { | |
| 341 | - continue; | |
| 342 | - } | |
| 343 | - var seriesIndexToUse = i + 1; | |
| 344 | - | |
| 345 | - // if an annotation "swallowed" a series, use the following one. | |
| 346 | - if(series_annotations.includes(i)){ | |
| 347 | - seriesIndexToUse++; | |
| 348 | - } | |
| 349 | - if ( series[seriesIndexToUse] ) { | |
| 350 | - format_data(id, table, series[seriesIndexToUse].type, settings.series[i].format, seriesIndexToUse); | |
| 351 | - } | |
| 364 | + // Single-pass: walk columns, skip annotation/helper roles, apply formats in order. | |
| 365 | + var k = 0; // index into settings.series (visible series) | |
| 366 | + for (var c = 1; c < series.length && k < settings.series.length; c++) { // skip label at 0 | |
| 367 | + if (table.getColumnProperty(c, 'role')) continue; // helper/annotation column | |
| 368 | + var s = settings.series[k++]; | |
| 369 | + if (!s || !s.format) continue; | |
| 370 | + if (!series[c]) continue; | |
| 371 | + format_data(id, table, series[c].type, s.format, c); | |
| 352 | 372 | } |
| 353 | 373 | break; |
| 354 | 374 | } |
| 355 | 375 | } else if (chart.type === 'pie' && settings.format && settings.format !== '') { |
| @@ -432,21 +452,32 @@ | ||
| 432 | 452 | return; |
| 433 | 453 | } |
| 434 | 454 | |
| 435 | 455 | var formatter = null; |
| 436 | - switch (type) { | |
| 437 | - case 'number': | |
| 438 | - formatter = new gv.NumberFormat({pattern: format}); | |
| 439 | - break; | |
| 440 | - case 'date': | |
| 441 | - case 'datetime': | |
| 442 | - case 'timeofday': | |
| 443 | - formatter = new gv.DateFormat({pattern: format}); | |
| 444 | - break; | |
| 445 | - } | |
| 456 | + var $formatInput = $('input.control-text[name*="[format]"]').filter(function() { | |
| 457 | + return $(this).val() === format; | |
| 458 | + }); | |
| 459 | + try { | |
| 460 | + switch (type) { | |
| 461 | + case 'number': | |
| 462 | + formatter = new gv.NumberFormat({pattern: format}); | |
| 463 | + break; | |
| 464 | + case 'date': | |
| 465 | + case 'datetime': | |
| 466 | + case 'timeofday': | |
| 467 | + formatter = new gv.DateFormat({pattern: format}); | |
| 468 | + break; | |
| 469 | + } | |
| 446 | 470 | |
| 447 | - if (formatter) { | |
| 448 | - formatter.format(table, index); | |
| 471 | + if (formatter) { | |
| 472 | + formatter.format(table, index); | |
| 473 | + $formatInput.nextAll('.visualizer-format-error').remove(); | |
| 474 | + } | |
| 475 | + } catch (e) { | |
| 476 | + if ($formatInput.length) { | |
| 477 | + $formatInput.nextAll('.visualizer-format-error').remove(); | |
| 478 | + $('<p class="visualizer-format-error" style="color:#cc0000;margin:4px 0 0"></p>').text(visualizer.l10n.invalid_format).insertAfter($formatInput); | |
| 479 | + } | |
| 449 | 480 | } |
| 450 | 481 | |
| 451 | 482 | var arr = id.split('-'); |
| 452 | 483 | $('body').trigger('visualizer:format:chart', {id: parseInt(arr[1]), data: table, column: index}); |
| @@ -476,10 +507,11 @@ | ||
| 476 | 507 | |
| 477 | 508 | var resizeTimeout; |
| 478 | 509 | |
| 479 | 510 | $(document).ready(function() { |
| 480 | - $(window).resize(function() { | |
| 511 | + $(window).resize(function(e) { | |
| 481 | 512 | clearTimeout(resizeTimeout); |
| 513 | + window.isResizeRequest = 'undefined' !== typeof e.originalEvent ? true : false; | |
| 482 | 514 | resizeTimeout = setTimeout(render, 100); |
| 483 | 515 | }); |
| 484 | 516 | |
| 485 | 517 | resizeHiddenContainers(true); |
| @@ -531,8 +563,12 @@ | ||
| 531 | 563 | if(v.is_front == true){ // jshint ignore:line |
| 532 | 564 | // check what all chart types to load. |
| 533 | 565 | $chart_types = []; |
| 534 | 566 | $.each(v.charts, function(i, c){ |
| 567 | + // Only consider charts rendered by Google Charts. | |
| 568 | + if ( c.library && c.library !== 'google' && c.library !== 'GoogleCharts' ) { | |
| 569 | + return; | |
| 570 | + } | |
| 535 | 571 | var $type = c.type; |
| 536 | 572 | switch($type){ |
| 537 | 573 | case 'bar': |
| 538 | 574 | case 'column': |
| @@ -561,9 +597,9 @@ | ||
| 561 | 597 | case 'radar': |
| 562 | 598 | $type = null; |
| 563 | 599 | break; |
| 564 | 600 | } |
| 565 | - if($type != null){ | |
| 601 | + if($type != null && $type !== ''){ | |
| 566 | 602 | $chart_types.push($type); |
| 567 | 603 | } |
| 568 | 604 | }); |
| 569 | 605 | } |