Widget:Grafico: differenze tra le versioni

Da WikiLectio.
m test
m test
Riga 38: Riga 38:
</div>
</div>


<!-- Chart.js (CDN) -->
<!-- Chart.js locale -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="/resources/chart.umd.js"></script>
 


<script>
<script>
Riga 49: Riga 48:
   function numOrUndef(s){ const n = Number(s); return isNaN(n) ? undefined : n; }
   function numOrUndef(s){ const n = Number(s); return isNaN(n) ? undefined : n; }


   var id  = document.currentScript.previousElementSibling.previousElementSibling.id.replace('canvas_',''); // robusto
   var id  = "<!--{$id|escape:'html'}-->";
   var c  = document.getElementById("canvas_" + id);
   var c  = document.getElementById("canvas_" + id);
   var msg = document.getElementById("chartmsg_" + id);
   var msg = document.getElementById("chartmsg_" + id);


   if(!c){ if(msg) msg.textContent = "Canvas non trovato (id="+id+")"; return; }
   if(!c){ if(msg) msg.textContent = "Canvas non trovato (id="+id+")"; return; }
   if(typeof Chart === "undefined"){ if(msg) msg.textContent = "Chart.js non caricato (CDN bloccato?)"; return; }
   if(typeof Chart === "undefined"){ if(msg) msg.textContent = "Chart.js (locale) non caricato: controlla /resources/chart.umd.js"; return; }


   try {
   try {
Riga 60: Riga 59:
     var labels = csvToArray(ds.labels);
     var labels = csvToArray(ds.labels);


    // Leggi i data-attributes (camelCase)
     var seriesDefs = [
     var seriesDefs = [
       { t: ds.series1Title, d: ds.series1Data, ty: ds.series1Type, c: ds.series1Color },
       { t: ds.series1Title, d: ds.series1Data, ty: ds.series1Type, c: ds.series1Color },
Riga 83: Riga 83:
     });
     });


    // Punto di equilibrio opzionale
     var eqx = Number(ds.eqx), eqy = Number(ds.eqy);
     var eqx = Number(ds.eqx), eqy = Number(ds.eqy);
     if(!isNaN(eqx) && !isNaN(eqy)){
     if(!isNaN(eqx) && !isNaN(eqy)){
Riga 102: Riga 103:
       data: { labels: labels, datasets: datasets },
       data: { labels: labels, datasets: datasets },
       options: {
       options: {
         responsive: (ds.responsive||'true').toLowerCase() !== 'false',
         responsive: truthy(ds.responsive),
         maintainAspectRatio: false,
         maintainAspectRatio: false,
         plugins: {
         plugins: {
           title:  { display: !!ds.title, text: ds.title },
           title:  { display: !!ds.title, text: ds.title },
           legend:  { display: (ds.showLegend||'true').toLowerCase() !== 'false' },
           legend:  { display: truthy(ds.showLegend) },
           tooltip: { enabled: true }
           tooltip: { enabled: true }
         },
         },
Riga 114: Riga 115:
             type: 'linear',
             type: 'linear',
             title: { display: true, text: ds.xTitle || 'Quantità di appartamenti' },
             title: { display: true, text: ds.xTitle || 'Quantità di appartamenti' },
             min: Number.isNaN(Number(ds.xmin)) ? undefined : Number(ds.xmin),
             min: numOrUndef(ds.xmin), max: numOrUndef(ds.xmax)
            max: Number.isNaN(Number(ds.xmax)) ? undefined : Number(ds.xmax)
           } : {
           } : {
             type: 'category',
             type: 'category',
Riga 122: Riga 122:
           y: {
           y: {
             title: { display: true, text: ds.yTitle || "Prezzo dell'affitto" },
             title: { display: true, text: ds.yTitle || "Prezzo dell'affitto" },
             min: Number.isNaN(Number(ds.ymin)) ? undefined : Number(ds.ymin),
             min: numOrUndef(ds.ymin), max: numOrUndef(ds.ymax),
            max: Number.isNaN(Number(ds.ymax)) ? undefined : Number(ds.ymax),
             beginAtZero: false
             beginAtZero: false
           }
           }
Riga 137: Riga 136:
})();
})();
</script>
</script>
<!--
Se il tuo hosting blocca il CDN, scarica Chart.js (chart.umd.js) e servilo in locale:
  1) carica /resources/chart.umd.js
  2) sostituisci la riga del CDN con:
    <script src="/resources/chart.umd.js"></script>
-->
</includeonly>
</includeonly>

Versione delle 17:39, 19 set 2025