| 1 |
/* SQL Chart Builder 3.0 front-end helpers (Chart.js v4) */ |
| 2 |
|
| 3 |
/* Legacy compat: cached shortcode output generated by v2.x may still reference |
| 4 |
the 'horizontalBar' chart type that Chart.js v4 removed. Alias it to a bar |
| 5 |
chart with a horizontal index axis so old inline scripts keep rendering. */ |
| 6 |
(function () { |
| 7 |
if (typeof Chart === 'undefined' || !Chart.registry || !Chart.BarController) return; |
| 8 |
try { |
| 9 |
Chart.registry.getController('horizontalBar'); |
| 10 |
} catch (e) { |
| 11 |
var HorizontalBarController = function () { |
| 12 |
return Reflect.construct(Chart.BarController, arguments, HorizontalBarController); |
| 13 |
}; |
| 14 |
HorizontalBarController.prototype = Object.create(Chart.BarController.prototype); |
| 15 |
Object.setPrototypeOf(HorizontalBarController, Chart.BarController); |
| 16 |
HorizontalBarController.id = 'horizontalBar'; |
| 17 |
HorizontalBarController.defaults = Object.assign({}, Chart.BarController.defaults, { indexAxis: 'y' }); |
| 18 |
try { Chart.register(HorizontalBarController); } catch (err) { /* noop */ } |
| 19 |
} |
| 20 |
})(); |
| 21 |
|
| 22 |
/* "Force tooltips" feature: draws every value directly on the chart |
| 23 |
(v4 replacement of the old v2 beforeRender/Tooltip hack). |
| 24 |
Activated per chart via options.showAllTooltips = true. */ |
| 25 |
(function () { |
| 26 |
if (typeof Chart === 'undefined') return; |
| 27 |
Chart.register({ |
| 28 |
id: 'gvnShowAllValues', |
| 29 |
afterDatasetsDraw: function (chart) { |
| 30 |
if (!chart.config.options || !chart.config.options.showAllTooltips) return; |
| 31 |
var ctx = chart.ctx; |
| 32 |
ctx.save(); |
| 33 |
ctx.font = 'bold 12px sans-serif'; |
| 34 |
ctx.textAlign = 'center'; |
| 35 |
ctx.textBaseline = 'middle'; |
| 36 |
chart.data.datasets.forEach(function (dataset, i) { |
| 37 |
var meta = chart.getDatasetMeta(i); |
| 38 |
if (meta.hidden) return; |
| 39 |
meta.data.forEach(function (element, index) { |
| 40 |
var value = dataset.data[index]; |
| 41 |
if (value === null || typeof value === 'undefined') return; |
| 42 |
if (typeof value === 'object') value = value.y; |
| 43 |
var pos = element.tooltipPosition ? element.tooltipPosition() : element; |
| 44 |
ctx.fillStyle = '#fff'; |
| 45 |
ctx.strokeStyle = 'rgba(0,0,0,0.6)'; |
| 46 |
ctx.lineWidth = 3; |
| 47 |
ctx.strokeText(String(value), pos.x, pos.y); |
| 48 |
ctx.fillText(String(value), pos.x, pos.y); |
| 49 |
}); |
| 50 |
}); |
| 51 |
ctx.restore(); |
| 52 |
} |
| 53 |
}); |
| 54 |
})(); |
| 55 |
|
| 56 |
/* Datepicker for [type: date] dynamic filters */ |
| 57 |
jQuery(function ($) { |
| 58 |
if ($.fn.datepicker) { |
| 59 |
$('[data-toggle="datepicker"]').datepicker({ format: 'yyyy-mm-dd', autoHide: true }); |
| 60 |
} |
| 61 |
}); |
| 62 |
|
| 63 |
/* Export helpers (kept for backward compatibility with custom setups) */ |
| 64 |
function saveaspng(id) { |
| 65 |
var canvas = jQuery('#' + id + ' canvas, canvas#' + id).get(0); |
| 66 |
if (canvas && canvas.toDataURL) { |
| 67 |
window.open(canvas.toDataURL('image/png')); |
| 68 |
return; |
| 69 |
} |
| 70 |
window.open(jQuery('#' + id + ' img').attr('src')); |
| 71 |
} |
| 72 |
|
| 73 |
function exportcsv() { |
| 74 |
var csvFile = csv_title + csv_data; |
| 75 |
csvFile = csvFile.split('<br>').join('\n'); |
| 76 |
var filename = 'mycsv.csv'; |
| 77 |
var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' }); |
| 78 |
var link = document.createElement('a'); |
| 79 |
if (link.download !== undefined) { |
| 80 |
var url = URL.createObjectURL(blob); |
| 81 |
link.setAttribute('href', url); |
| 82 |
link.setAttribute('download', filename); |
| 83 |
link.style.visibility = 'hidden'; |
| 84 |
document.body.appendChild(link); |
| 85 |
link.click(); |
| 86 |
document.body.removeChild(link); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/* ajax on dismissed admin notice */ |
| 91 |
jQuery(function ($) { |
| 92 |
$(document).on('click', '.guaven-sqlcharts-notice .notice-dismiss', function () { |
| 93 |
var type = $(this).closest('.guaven-sqlcharts-notice').data('notice'); |
| 94 |
if (typeof guaven_sqlcharts_notice_dismissed === 'undefined') return; |
| 95 |
$.ajax(ajaxurl, { |
| 96 |
type: 'POST', |
| 97 |
data: { |
| 98 |
action: guaven_sqlcharts_notice_dismissed.action, |
| 99 |
type: type, |
| 100 |
nonce: guaven_sqlcharts_notice_dismissed.nonce |
| 101 |
} |
| 102 |
}); |
| 103 |
}); |
| 104 |
}); |
| 105 |
|
| 106 |
/* Legacy no-op: v2.x cached output may call this before our plugin exists */ |
| 107 |
window.guaven_sqlcharts_show_pie_labels = window.guaven_sqlcharts_show_pie_labels || function () {}; |
| 108 |
|