| 1 |
/* Polar Area 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 renderAll() { |
| 11 |
document.querySelectorAll('.bkpca-wrap[data-chart]').forEach(function(wrap) { |
| 12 |
var canvas = wrap.querySelector('.bkpca-canvas'); |
| 13 |
if (!canvas || canvas._bkpcaInit) return; |
| 14 |
canvas._bkpcaInit = true; |
| 15 |
try { |
| 16 |
var d = JSON.parse(wrap.getAttribute('data-chart')); |
| 17 |
var labels = (d.labels||'').split(',').map(function(s){return s.trim();}); |
| 18 |
var vals = (d.values||'').split(',').map(function(s){return parseFloat(s.trim())||0;}); |
| 19 |
var cols = (d.colors||'').split(',').map(function(s){return s.trim();}); |
| 20 |
var alpha = d.fillAlpha||75; |
| 21 |
var bgColors = cols.map(function(c){return parseHex(c, alpha);}); |
| 22 |
new window.Chart(canvas, { |
| 23 |
type: 'polarArea', |
| 24 |
data: { labels: labels, datasets: [{ data: vals, backgroundColor: bgColors, borderColor: cols, borderWidth: 2 }] }, |
| 25 |
options: { |
| 26 |
responsive: true, |
| 27 |
plugins: { |
| 28 |
legend: { display: !!d.showLegend, position: d.legendPos||'bottom' }, |
| 29 |
title: { display: !!(d.showTitle && d.chartTitle), text: d.chartTitle||'' } |
| 30 |
} |
| 31 |
} |
| 32 |
}); |
| 33 |
} catch(e) { console.warn('bkpca chart error', e); } |
| 34 |
}); |
| 35 |
} |
| 36 |
|
| 37 |
function init() { |
| 38 |
if (window.Chart) { renderAll(); return; } |
| 39 |
var s = document.createElement('script'); |
| 40 |
s.src = 'https://cdn.jsdelivr.net/npm/chart.js@4.4.3/dist/chart.umd.min.js'; |
| 41 |
s.onload = renderAll; |
| 42 |
document.head.appendChild(s); |
| 43 |
} |
| 44 |
|
| 45 |
if (document.readyState === 'loading') { |
| 46 |
document.addEventListener('DOMContentLoaded', init); |
| 47 |
} else { |
| 48 |
init(); |
| 49 |
} |
| 50 |
}() ); |
| 51 |
|