Widget:Grafico: differenze tra le versioni
Da WikiLectio.
m test |
m test |
||
| Riga 1: | Riga 1: | ||
< | <includeonly> | ||
<div id="chartwrap_<!--{$id|escape:'html'}-->" | <div id="chartwrap_<!--{$id|escape:'html'}-->" | ||
style="width:<!--{$width|default:600|escape:'html'}-->px; | style="width:<!--{$width|default:600|escape:'html'}-->px;height:<!--{$height|default:380|escape:'html'}-->px;"> | ||
<canvas id="canvas_<!--{$id|escape:'html'}-->" | <canvas id="canvas_<!--{$id|escape:'html'}-->" | ||
width="<!--{$width|default:600|escape:'html'}-->" | width="<!--{$width|default:600|escape:'html'}-->" | ||
| Riga 38: | Riga 37: | ||
</div> | </div> | ||
<!-- Chart.js | <!-- Chart.js dal CDN --> | ||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script> | <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> | ||
<script> | <script> | ||
(function(){ | (function(){ | ||
| Riga 48: | Riga 46: | ||
function truthy(s){ return String(s||'').toLowerCase() !== 'false'; } | function truthy(s){ return String(s||'').toLowerCase() !== 'false'; } | ||
function numOrUndef(s){ const n = Number(s); return isNaN(n) ? undefined : n; } | function numOrUndef(s){ const n = Number(s); return isNaN(n) ? undefined : n; } | ||
document.querySelectorAll("canvas[id^='canvas_']").forEach(function(c){ | document.querySelectorAll("canvas[id^='canvas_']").forEach(function(c){ | ||
| Riga 73: | Riga 57: | ||
['series4Title','series4Data','series4Type','series4Color'] | ['series4Title','series4Data','series4Type','series4Color'] | ||
].forEach(function(keys){ | ].forEach(function(keys){ | ||
var | var title = ds[keys[0].toLowerCase()], data = ds[keys[1].toLowerCase()], | ||
if( | type = ds[keys[2].toLowerCase()], color = ds[keys[3].toLowerCase()]; | ||
if(title && data){ | |||
datasets.push({ | |||
label: title, data: csvToNum(data), type: (type||'line'), | |||
fill: false, borderColor: color||undefined, backgroundColor: color||undefined, | |||
tension: 0, pointRadius: 2 | |||
}); | |||
} | |||
}); | }); | ||
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)){ | ||
datasets.push({ | datasets.push({ | ||
label: 'Equilibrio', | label: 'Equilibrio', type: 'scatter', | ||
data: [{x:eqx, y:eqy}], showLine: false, pointRadius: 5, pointHoverRadius: 6, | |||
data: [{x:eqx, y:eqy}], | borderColor: '#000', backgroundColor: '#000' | ||
borderColor: | |||
}); | }); | ||
} | } | ||
var numericLabels = labels.every(l => !isNaN(Number(l))); | var numericLabels = labels.every(l => !isNaN(Number(l))); | ||
new Chart(c.getContext('2d'), { | |||
type: 'line', | type: 'line', | ||
data: { labels: labels, datasets: datasets }, | data: { labels: labels, datasets: datasets }, | ||
options: { | options: { | ||
responsive: truthy(ds.responsive), | responsive: truthy(ds.responsive), maintainAspectRatio: false, | ||
plugins: { | plugins: { | ||
title: { display: !!ds.title, text: ds.title }, | title: { display: !!ds.title, text: ds.title }, | ||
legend: { display: truthy(ds.showLegend) }, | legend: { display: truthy(ds.showLegend) }, tooltip: { enabled: true } | ||
}, | }, | ||
elements: { line: { borderWidth: 2 } }, | elements: { line: { borderWidth: 2 } }, | ||
scales: { | scales: { | ||
x: numericLabels ? { | x: numericLabels ? { | ||
type: 'linear', | type: 'linear', title: { display: true, text: ds.xTitle || 'Quantità di appartamenti' }, | ||
min: numOrUndef(ds.xmin), max: numOrUndef(ds.xmax) | min: numOrUndef(ds.xmin), max: numOrUndef(ds.xmax) | ||
} : { | } : { | ||
type: 'category', | type: 'category', title: { display: true, text: ds.xTitle || 'Quantità di appartamenti' } | ||
}, | }, | ||
y: { | y: { | ||
title: { display: true, text: ds.yTitle || "Prezzo dell'affitto" }, | title: { display: true, text: ds.yTitle || "Prezzo dell'affitto" }, | ||
min: numOrUndef(ds.ymin), max: numOrUndef(ds.ymax), | min: numOrUndef(ds.ymin), max: numOrUndef(ds.ymax), beginAtZero: false | ||
} | } | ||
} | } | ||
} | } | ||
} | }); | ||
}); | }); | ||
})(); | })(); | ||
</script> | </script> | ||
</includeonly> | |||