| 1 |
/* Bubble Chart — frontend */ |
| 2 |
( function () { |
| 3 |
function parseHex(hex, alpha) { |
| 4 |
hex = (hex||'#6c3fb5').replace('#',''); |
| 5 |
if (hex.length===3) hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2]; |
| 6 |
var r=parseInt(hex.substring(0,2),16),g=parseInt(hex.substring(2,4),16),b=parseInt(hex.substring(4,6),16); |
| 7 |
return 'rgba('+r+','+g+','+b+','+(alpha/100).toFixed(2)+')'; |
| 8 |
} |
| 9 |
|
| 10 |
function parseBubbles(text) { |
| 11 |
return (text||'').split('\n').filter(Boolean).map(function(line) { |
| 12 |
var parts = line.split(',').map(function(s){return parseFloat(s)||0;}); |
| 13 |
return { x: parts[0]||0, y: parts[1]||0, r: parts[2]||8 }; |
| 14 |
}); |
| 15 |
} |
| 16 |
|
| 17 |
function renderAll() { |
| 18 |
document.querySelectorAll('.bkbbl-wrap[data-chart]').forEach(function(wrap) { |
| 19 |
var canvas = wrap.querySelector('.bkbbl-canvas'); |
| 20 |
if (!canvas || canvas._bkbblInit) return; |
| 21 |
canvas._bkbblInit = true; |
| 22 |
try { |
| 23 |
var d = JSON.parse(wrap.getAttribute('data-chart')); |
| 24 |
var alpha = d.fillAlpha||50; |
| 25 |
var datasets; |
| 26 |
try { datasets = JSON.parse(d.datasetsJson||'[]'); } catch(e){ datasets=[]; } |
| 27 |
var dsData = datasets.map(function(ds) { |
| 28 |
return { |
| 29 |
label: ds.label||'', |
| 30 |
data: parseBubbles(ds.bubbles), |
| 31 |
backgroundColor: parseHex(ds.color||'#6c3fb5', alpha), |
| 32 |
borderColor: ds.color||'#6c3fb5', |
| 33 |
borderWidth: 2 |
| 34 |
}; |
| 35 |
}); |
| 36 |
new window.Chart(canvas, { |
| 37 |
type: 'bubble', |
| 38 |
data: { datasets: dsData }, |
| 39 |
options: { |
| 40 |
responsive: true, maintainAspectRatio: false, |
| 41 |
plugins: { |
| 42 |
legend: { display: !!d.showLegend, position: d.legendPos||'bottom' }, |
| 43 |
title: { display: !!(d.showTitle&&d.chartTitle), text: d.chartTitle||'' } |
| 44 |
}, |
| 45 |
scales: { |
| 46 |
x: { grid: { display: d.showGrid!==false } }, |
| 47 |
y: { grid: { display: d.showGrid!==false } } |
| 48 |
} |
| 49 |
} |
| 50 |
}); |
| 51 |
} catch(e) { console.warn('bkbbl chart error', e); } |
| 52 |
}); |
| 53 |
} |
| 54 |
|
| 55 |
function init() { |
| 56 |
if (window.Chart) { renderAll(); return; } |
| 57 |
var s = document.createElement('script'); |
| 58 |
s.src = 'https://cdn.jsdelivr.net/npm/chart.js@4.4.3/dist/chart.umd.min.js'; |
| 59 |
s.onload = renderAll; |
| 60 |
document.head.appendChild(s); |
| 61 |
} |
| 62 |
|
| 63 |
if (document.readyState === 'loading') { |
| 64 |
document.addEventListener('DOMContentLoaded', init); |
| 65 |
} else { |
| 66 |
init(); |
| 67 |
} |
| 68 |
}() ); |
| 69 |
|