| 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 |
/* "Scale X axis by date/time" option: the PHP side emits {x: <utc ms>, y: value} points on a linear |
| 57 |
X scale whose tick callback and tooltip title callback are the two globals below. UTC getters are |
| 58 |
used on purpose so a date never shifts by a day in the viewer's time zone. */ |
| 59 |
(function () { |
| 60 |
function pad(n) { return (n < 10 ? '0' : '') + n; } |
| 61 |
function fmt(ms, withTime) { |
| 62 |
var d = new Date(ms); |
| 63 |
if (isNaN(d.getTime())) return ms; |
| 64 |
var s = d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate()); |
| 65 |
if (withTime) s += ' ' + pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes()); |
| 66 |
return s; |
| 67 |
} |
| 68 |
function hasTime(chart) { |
| 69 |
if (!chart || chart.__gvnHasTime !== undefined) return chart ? chart.__gvnHasTime : false; |
| 70 |
var found = false; |
| 71 |
(chart.data.datasets || []).forEach(function (ds) { |
| 72 |
(ds.data || []).forEach(function (p) { |
| 73 |
if (p && typeof p === 'object' && p.x % 86400000 !== 0) found = true; |
| 74 |
}); |
| 75 |
}); |
| 76 |
chart.__gvnHasTime = found; |
| 77 |
return found; |
| 78 |
} |
| 79 |
// scales.x.ticks.callback – "this" is the scale |
| 80 |
window.gvnSqlChartsTimeTick = function (value) { |
| 81 |
return fmt(value, hasTime(this && this.chart)); |
| 82 |
}; |
| 83 |
// plugins.tooltip.callbacks.title |
| 84 |
window.gvnSqlChartsTimeTooltipTitle = function (items) { |
| 85 |
return items && items.length ? fmt(items[0].parsed.x, hasTime(items[0].chart)) : ''; |
| 86 |
}; |
| 87 |
})(); |
| 88 |
|
| 89 |
/* Datepicker for [type: date] dynamic filters */ |
| 90 |
jQuery(function ($) { |
| 91 |
if ($.fn.datepicker) { |
| 92 |
$('[data-toggle="datepicker"]').datepicker({ format: 'yyyy-mm-dd', autoHide: true }); |
| 93 |
} |
| 94 |
}); |
| 95 |
|
| 96 |
/* Export helpers (kept for backward compatibility with custom setups) */ |
| 97 |
function saveaspng(id) { |
| 98 |
var canvas = jQuery('#' + id + ' canvas, canvas#' + id).get(0); |
| 99 |
if (canvas && canvas.toDataURL) { |
| 100 |
window.open(canvas.toDataURL('image/png')); |
| 101 |
return; |
| 102 |
} |
| 103 |
window.open(jQuery('#' + id + ' img').attr('src')); |
| 104 |
} |
| 105 |
|
| 106 |
function exportcsv() { |
| 107 |
var csvFile = csv_title + csv_data; |
| 108 |
csvFile = csvFile.split('<br>').join('\n'); |
| 109 |
var filename = 'mycsv.csv'; |
| 110 |
var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' }); |
| 111 |
var link = document.createElement('a'); |
| 112 |
if (link.download !== undefined) { |
| 113 |
var url = URL.createObjectURL(blob); |
| 114 |
link.setAttribute('href', url); |
| 115 |
link.setAttribute('download', filename); |
| 116 |
link.style.visibility = 'hidden'; |
| 117 |
document.body.appendChild(link); |
| 118 |
link.click(); |
| 119 |
document.body.removeChild(link); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/* ajax on dismissed admin notice */ |
| 124 |
jQuery(function ($) { |
| 125 |
$(document).on('click', '.guaven-sqlcharts-notice .notice-dismiss', function () { |
| 126 |
var type = $(this).closest('.guaven-sqlcharts-notice').data('notice'); |
| 127 |
if (typeof guaven_sqlcharts_notice_dismissed === 'undefined') return; |
| 128 |
$.ajax(ajaxurl, { |
| 129 |
type: 'POST', |
| 130 |
data: { |
| 131 |
action: guaven_sqlcharts_notice_dismissed.action, |
| 132 |
type: type, |
| 133 |
nonce: guaven_sqlcharts_notice_dismissed.nonce |
| 134 |
} |
| 135 |
}); |
| 136 |
}); |
| 137 |
}); |
| 138 |
|
| 139 |
/* Legacy no-op: v2.x cached output may call this before our plugin exists */ |
| 140 |
window.guaven_sqlcharts_show_pie_labels = window.guaven_sqlcharts_show_pie_labels || function () {}; |
| 141 |
|