PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.7
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.7
2.0.13 2.0.12 2.0.11 2.0.10 2.0.9 trunk 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8
blockenberg / blocks / line-chart / frontend.js

frontend.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.7, at blocks/line-chart/frontend.js

74 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* Line Chart — frontend.js (Chart.js via CDN) */
2 ( function () {
3 var CHARTJS_CDN = 'https://cdn.jsdelivr.net/npm/chart.js@4.4.3/dist/chart.umd.min.js';
4
5 function loadChartJs( cb ) {
6 if ( window.Chart ) { cb(); return; }
7 var s = document.createElement('script');
8 s.src = CHARTJS_CDN;
9 s.onload = cb;
10 document.head.appendChild(s);
11 }
12
13 function initChart( wrap ) {
14 var canvas = wrap.querySelector('canvas');
15 if (!canvas) return;
16
17 var labels = (wrap.dataset.labels || '').split(',').map(s=>s.trim());
18 var datasets = JSON.parse(wrap.dataset.datasets || '[]');
19 var height = parseInt(wrap.dataset.height) || 340;
20 var showGrid = wrap.dataset.grid !== '0';
21 var showLeg = wrap.dataset.legend !== '0';
22 var legPos = wrap.dataset.legendPos || 'top';
23 var ptStyle = wrap.dataset.pointStyle || 'circle';
24
25 canvas.height = height;
26
27 var chartDatasets = datasets.map(function(ds) {
28 var rgb = hexToRgb(ds.color || '#6c3fb5');
29 return {
30 label: ds.label,
31 data: (ds.data||'').split(',').map(Number),
32 borderColor: ds.color,
33 backgroundColor: ds.fill ? ('rgba('+rgb+',0.15)') : 'transparent',
34 fill: !!ds.fill,
35 tension: ds.tension !== undefined ? ds.tension : 0.4,
36 pointStyle: ptStyle === 'false' ? false : ptStyle,
37 pointRadius: ptStyle === 'false' ? 0 : 5,
38 borderWidth: 2,
39 };
40 });
41
42 new window.Chart(canvas, {
43 type: 'line',
44 data: { labels: labels, datasets: chartDatasets },
45 options: {
46 responsive: true,
47 plugins: {
48 legend: { display: showLeg, position: legPos },
49 },
50 scales: {
51 x: { grid: { display: showGrid } },
52 y: { grid: { display: showGrid } },
53 },
54 }
55 });
56 }
57
58 function hexToRgb(hex) {
59 var r = parseInt(hex.slice(1,3),16);
60 var g = parseInt(hex.slice(3,5),16);
61 var b = parseInt(hex.slice(5,7),16);
62 return r+','+g+','+b;
63 }
64
65 function init() {
66 var wraps = document.querySelectorAll('.bklc-wrap[data-type]');
67 if (!wraps.length) return;
68 loadChartJs(function() { wraps.forEach(initChart); });
69 }
70
71 if (document.readyState==='loading') document.addEventListener('DOMContentLoaded',init);
72 else init();
73 }() );
74