/* SQL Chart Builder 3.0 front-end helpers (Chart.js v4) */ /* Legacy compat: cached shortcode output generated by v2.x may still reference the 'horizontalBar' chart type that Chart.js v4 removed. Alias it to a bar chart with a horizontal index axis so old inline scripts keep rendering. */ (function () { if (typeof Chart === 'undefined' || !Chart.registry || !Chart.BarController) return; try { Chart.registry.getController('horizontalBar'); } catch (e) { var HorizontalBarController = function () { return Reflect.construct(Chart.BarController, arguments, HorizontalBarController); }; HorizontalBarController.prototype = Object.create(Chart.BarController.prototype); Object.setPrototypeOf(HorizontalBarController, Chart.BarController); HorizontalBarController.id = 'horizontalBar'; HorizontalBarController.defaults = Object.assign({}, Chart.BarController.defaults, { indexAxis: 'y' }); try { Chart.register(HorizontalBarController); } catch (err) { /* noop */ } } })(); /* "Force tooltips" feature: draws every value directly on the chart (v4 replacement of the old v2 beforeRender/Tooltip hack). Activated per chart via options.showAllTooltips = true. */ (function () { if (typeof Chart === 'undefined') return; Chart.register({ id: 'gvnShowAllValues', afterDatasetsDraw: function (chart) { if (!chart.config.options || !chart.config.options.showAllTooltips) return; var ctx = chart.ctx; ctx.save(); ctx.font = 'bold 12px sans-serif'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; chart.data.datasets.forEach(function (dataset, i) { var meta = chart.getDatasetMeta(i); if (meta.hidden) return; meta.data.forEach(function (element, index) { var value = dataset.data[index]; if (value === null || typeof value === 'undefined') return; if (typeof value === 'object') value = value.y; var pos = element.tooltipPosition ? element.tooltipPosition() : element; ctx.fillStyle = '#fff'; ctx.strokeStyle = 'rgba(0,0,0,0.6)'; ctx.lineWidth = 3; ctx.strokeText(String(value), pos.x, pos.y); ctx.fillText(String(value), pos.x, pos.y); }); }); ctx.restore(); } }); })(); /* Datepicker for [type: date] dynamic filters */ jQuery(function ($) { if ($.fn.datepicker) { $('[data-toggle="datepicker"]').datepicker({ format: 'yyyy-mm-dd', autoHide: true }); } }); /* Export helpers (kept for backward compatibility with custom setups) */ function saveaspng(id) { var canvas = jQuery('#' + id + ' canvas, canvas#' + id).get(0); if (canvas && canvas.toDataURL) { window.open(canvas.toDataURL('image/png')); return; } window.open(jQuery('#' + id + ' img').attr('src')); } function exportcsv() { var csvFile = csv_title + csv_data; csvFile = csvFile.split('
').join('\n'); var filename = 'mycsv.csv'; var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' }); var link = document.createElement('a'); if (link.download !== undefined) { var url = URL.createObjectURL(blob); link.setAttribute('href', url); link.setAttribute('download', filename); link.style.visibility = 'hidden'; document.body.appendChild(link); link.click(); document.body.removeChild(link); } } /* ajax on dismissed admin notice */ jQuery(function ($) { $(document).on('click', '.guaven-sqlcharts-notice .notice-dismiss', function () { var type = $(this).closest('.guaven-sqlcharts-notice').data('notice'); if (typeof guaven_sqlcharts_notice_dismissed === 'undefined') return; $.ajax(ajaxurl, { type: 'POST', data: { action: guaven_sqlcharts_notice_dismissed.action, type: type, nonce: guaven_sqlcharts_notice_dismissed.nonce } }); }); }); /* Legacy no-op: v2.x cached output may call this before our plugin exists */ window.guaven_sqlcharts_show_pie_labels = window.guaven_sqlcharts_show_pie_labels || function () {};