PluginProbe
SQL Chart Builder / 3.0.6
SQL Chart Builder v3.0.6
3.0.6 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.6, at asset/front.js

182 lines 8.1 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 // Labels of dates that sit too close together on the axis are moved to a lower line (one below the
80 // other) instead of being drawn over each other. Returns the line level (0, 1, 2) per tick index.
81 function staggerLevels(scale, ticks, withTime) {
82 var sig = ticks.length + ':' + scale.min + ':' + scale.max + ':' + scale.width;
83 if (scale.__gvnStagger && scale.__gvnStagger.sig === sig) return scale.__gvnStagger.levels;
84 var span = (scale.max - scale.min) || 1, width = scale.width || 0, levels = [], px = [], i, j, L, ok;
85 var labelW = (withTime ? 16 : 10) * 7 + 12; // approx. pixel width of "YYYY-MM-DD[ HH:MM]" plus a gap
86 for (i = 0; i < ticks.length; i++) {
87 px[i] = (ticks[i].value - scale.min) / span * width;
88 for (L = 0; L < 3; L++) {
89 ok = true;
90 for (j = 0; j < i; j++) if (levels[j] === L && Math.abs(px[i] - px[j]) < labelW) { ok = false; break; }
91 if (ok) break;
92 }
93 levels[i] = ok ? L : -1; // -1: no free line, the label is hidden (the tooltip still shows the date)
94 }
95 scale.__gvnStagger = { sig: sig, levels: levels };
96 return levels;
97 }
98 // scales.x.ticks.callback – "this" is the scale
99 window.gvnSqlChartsTimeTick = function (value, index, ticks) {
100 var withTime = hasTime(this && this.chart);
101 var label = fmt(value, withTime);
102 if (!this || !ticks || !ticks.length || typeof index !== 'number') return label;
103 var level = staggerLevels(this, ticks, withTime)[index] || 0;
104 if (level < 0) return '';
105 if (!level) return label;
106 var lines = [];
107 for (var k = 0; k < level; k++) lines.push('');
108 lines.push(label);
109 return lines;
110 };
111 // scales.x.afterBuildTicks – label only the dates that exist in the data instead of evenly spaced values
112 window.gvnSqlChartsTimeTicks = function (scale) {
113 var seen = {}, xs = [];
114 (scale.chart.data.datasets || []).forEach(function (ds, i) {
115 if (scale.chart.getDatasetMeta(i).hidden) return;
116 (ds.data || []).forEach(function (p) {
117 if (p && typeof p === 'object' && typeof p.x === 'number' && !seen[p.x]) { seen[p.x] = true; xs.push(p.x); }
118 });
119 });
120 if (!xs.length) return;
121 xs.sort(function (a, b) { return a - b; });
122 scale.ticks = xs.map(function (v) { return { value: v }; });
123 };
124 // plugins.tooltip.callbacks.title
125 window.gvnSqlChartsTimeTooltipTitle = function (items) {
126 return items && items.length ? fmt(items[0].parsed.x, hasTime(items[0].chart)) : '';
127 };
128 })();
129
130 /* Datepicker for [type: date] dynamic filters */
131 jQuery(function ($) {
132 if ($.fn.datepicker) {
133 $('[data-toggle="datepicker"]').datepicker({ format: 'yyyy-mm-dd', autoHide: true });
134 }
135 });
136
137 /* Export helpers (kept for backward compatibility with custom setups) */
138 function saveaspng(id) {
139 var canvas = jQuery('#' + id + ' canvas, canvas#' + id).get(0);
140 if (canvas && canvas.toDataURL) {
141 window.open(canvas.toDataURL('image/png'));
142 return;
143 }
144 window.open(jQuery('#' + id + ' img').attr('src'));
145 }
146
147 function exportcsv() {
148 var csvFile = csv_title + csv_data;
149 csvFile = csvFile.split('<br>').join('\n');
150 var filename = 'mycsv.csv';
151 var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' });
152 var link = document.createElement('a');
153 if (link.download !== undefined) {
154 var url = URL.createObjectURL(blob);
155 link.setAttribute('href', url);
156 link.setAttribute('download', filename);
157 link.style.visibility = 'hidden';
158 document.body.appendChild(link);
159 link.click();
160 document.body.removeChild(link);
161 }
162 }
163
164 /* ajax on dismissed admin notice */
165 jQuery(function ($) {
166 $(document).on('click', '.guaven-sqlcharts-notice .notice-dismiss', function () {
167 var type = $(this).closest('.guaven-sqlcharts-notice').data('notice');
168 if (typeof guaven_sqlcharts_notice_dismissed === 'undefined') return;
169 $.ajax(ajaxurl, {
170 type: 'POST',
171 data: {
172 action: guaven_sqlcharts_notice_dismissed.action,
173 type: type,
174 nonce: guaven_sqlcharts_notice_dismissed.nonce
175 }
176 });
177 });
178 });
179
180 /* Legacy no-op: v2.x cached output may call this before our plugin exists */
181 window.guaven_sqlcharts_show_pie_labels = window.guaven_sqlcharts_show_pie_labels || function () {};
182