33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
// Setup del chart desiderato con id univoco
|
|
window.setup = (id, config) => {
|
|
|
|
const el = document.getElementById(id);
|
|
if (!el) {
|
|
//console.error("Elemento canvas non trovato:", id);
|
|
return;
|
|
}
|
|
// TECNICA INFALLIBILE:
|
|
// Chiediamo a Chart.js di trovare l'istanza associata a questo elemento DOM
|
|
// Indipendentemente da quale ID avesse prima.
|
|
const existingChart = Chart.getChart(el);
|
|
if (existingChart) {
|
|
existingChart.destroy();
|
|
}
|
|
|
|
const ctx = el.getContext('2d');
|
|
|
|
//var ctx = document.getElementById(id).getContext('2d');
|
|
//let currentDate = new Date();
|
|
//console.log(currentDate + " - Calling setup...");
|
|
//console.log(id);
|
|
if (window['chart-' + id] instanceof Chart) {
|
|
//window.myChart.destroy();
|
|
window['chart-' + id].destroy();
|
|
//console.log("Chart " + id + " destroyed!");
|
|
}
|
|
|
|
window['chart-' + id] = new Chart(ctx, config);
|
|
//console.log("Chart " + id + " created!");
|
|
//console.log(window['chart-' + id]);
|
|
}
|