PluginProbe
SQL Chart Builder / 3.0.5
SQL Chart Builder v3.0.5
3.0.5 3.0.4 3.0.3 3.0.2 3.0.1 trunk 1.0.2 1.0.3 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.7.1 2.3.7.2 2.3.8 3.0.0
sql-chart-builder / asset / front.js

front.js in SQL Chart Builder 3.0.5, at asset/front.js

154 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // scales.x.afterBuildTicks – label only the dates that exist in the data instead of evenly spaced values
84 window.gvnSqlChartsTimeTicks = function (scale) {
85 var seen = {}, xs = [];
86 (scale.chart.data.datasets || []).forEach(function (ds, i) {
87 if (scale.chart.getDatasetMeta(i).hidden) return;
88 (ds.data || []).forEach(function (p) {
89 if (p && typeof p === 'object' && typeof p.x === 'number' && !seen[p.x]) { seen[p.x] = true; xs.push(p.x); }
90 });
91 });
92 if (!xs.length) return;
93 xs.sort(function (a, b) { return a - b; });
94 scale.ticks = xs.map(function (v) { return { value: v }; });
95 };
96 // plugins.tooltip.callbacks.title
97 window.gvnSqlChartsTimeTooltipTitle = function (items) {
98 return items && items.length ? fmt(items[0].parsed.x, hasTime(items[0].chart)) : '';
99 };
100 })();
101
102 /* Datepicker for [type: date] dynamic filters */
103 jQuery(function ($) {
104 if ($.fn.datepicker) {
105 $('[data-toggle="datepicker"]').datepicker({ format: 'yyyy-mm-dd', autoHide: true });
106 }
107 });
108
109 /* Export helpers (kept for backward compatibility with custom setups) */
110 function saveaspng(id) {
111 var canvas = jQuery('#' + id + ' canvas, canvas#' + id).get(0);
112 if (canvas && canvas.toDataURL) {
113 window.open(canvas.toDataURL('image/png'));
114 return;
115 }
116 window.open(jQuery('#' + id + ' img').attr('src'));
117 }
118
119 function exportcsv() {
120 var csvFile = csv_title + csv_data;
121 csvFile = csvFile.split('<br>').join('\n');
122 var filename = 'mycsv.csv';
123 var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' });
124 var link = document.createElement('a');
125 if (link.download !== undefined) {
126 var url = URL.createObjectURL(blob);
127 link.setAttribute('href', url);
128 link.setAttribute('download', filename);
129 link.style.visibility = 'hidden';
130 document.body.appendChild(link);
131 link.click();
132 document.body.removeChild(link);
133 }
134 }
135
136 /* ajax on dismissed admin notice */
137 jQuery(function ($) {
138 $(document).on('click', '.guaven-sqlcharts-notice .notice-dismiss', function () {
139 var type = $(this).closest('.guaven-sqlcharts-notice').data('notice');
140 if (typeof guaven_sqlcharts_notice_dismissed === 'undefined') return;
141 $.ajax(ajaxurl, {
142 type: 'POST',
143 data: {
144 action: guaven_sqlcharts_notice_dismissed.action,
145 type: type,
146 nonce: guaven_sqlcharts_notice_dismissed.nonce
147 }
148 });
149 });
150 });
151
152 /* Legacy no-op: v2.x cached output may call this before our plugin exists */
153 window.guaven_sqlcharts_show_pie_labels = window.guaven_sqlcharts_show_pie_labels || function () {};
154