/* 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(); } }); })(); /* "Scale X axis by date/time" option: the PHP side emits {x: , y: value} points on a linear X scale whose tick callback and tooltip title callback are the two globals below. UTC getters are used on purpose so a date never shifts by a day in the viewer's time zone. */ (function () { function pad(n) { return (n < 10 ? '0' : '') + n; } function fmt(ms, withTime) { var d = new Date(ms); if (isNaN(d.getTime())) return ms; var s = d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate()); if (withTime) s += ' ' + pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes()); return s; } function hasTime(chart) { if (!chart || chart.__gvnHasTime !== undefined) return chart ? chart.__gvnHasTime : false; var found = false; (chart.data.datasets || []).forEach(function (ds) { (ds.data || []).forEach(function (p) { if (p && typeof p === 'object' && p.x % 86400000 !== 0) found = true; }); }); chart.__gvnHasTime = found; return found; } // Labels of dates that sit too close together on the axis are moved to a lower line (one below the // other) instead of being drawn over each other. Returns the line level (0, 1, 2) per tick index. function staggerLevels(scale, ticks, withTime) { var sig = ticks.length + ':' + scale.min + ':' + scale.max + ':' + scale.width; if (scale.__gvnStagger && scale.__gvnStagger.sig === sig) return scale.__gvnStagger.levels; var span = (scale.max - scale.min) || 1, width = scale.width || 0, levels = [], px = [], i, j, L, ok; var labelW = (withTime ? 16 : 10) * 7 + 12; // approx. pixel width of "YYYY-MM-DD[ HH:MM]" plus a gap for (i = 0; i < ticks.length; i++) { px[i] = (ticks[i].value - scale.min) / span * width; for (L = 0; L < 3; L++) { ok = true; for (j = 0; j < i; j++) if (levels[j] === L && Math.abs(px[i] - px[j]) < labelW) { ok = false; break; } if (ok) break; } levels[i] = ok ? L : -1; // -1: no free line, the label is hidden (the tooltip still shows the date) } scale.__gvnStagger = { sig: sig, levels: levels }; return levels; } // scales.x.ticks.callback – "this" is the scale window.gvnSqlChartsTimeTick = function (value, index, ticks) { var withTime = hasTime(this && this.chart); var label = fmt(value, withTime); if (!this || !ticks || !ticks.length || typeof index !== 'number') return label; var level = staggerLevels(this, ticks, withTime)[index] || 0; if (level < 0) return ''; if (!level) return label; var lines = []; for (var k = 0; k < level; k++) lines.push(''); lines.push(label); return lines; }; // scales.x.afterBuildTicks – label only the dates that exist in the data instead of evenly spaced values window.gvnSqlChartsTimeTicks = function (scale) { var seen = {}, xs = []; (scale.chart.data.datasets || []).forEach(function (ds, i) { if (scale.chart.getDatasetMeta(i).hidden) return; (ds.data || []).forEach(function (p) { if (p && typeof p === 'object' && typeof p.x === 'number' && !seen[p.x]) { seen[p.x] = true; xs.push(p.x); } }); }); if (!xs.length) return; xs.sort(function (a, b) { return a - b; }); scale.ticks = xs.map(function (v) { return { value: v }; }); }; // plugins.tooltip.callbacks.title window.gvnSqlChartsTimeTooltipTitle = function (items) { return items && items.length ? fmt(items[0].parsed.x, hasTime(items[0].chart)) : ''; }; })(); /* 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 () {};