23.02.2021, 10:20
It's possible that chart instance can be created multiple times. Use this to create chart only once. I've removed some parts of the code to make it more readable.
Code:
$(function(){
// load chart JS
$.getScript('https://www.gstatic.com/charts/loader.js', function() {
var chart4, chart5;
google.charts.load('current', {'packages':['gauge', 'corechart'],language: 'es'});
$('body').on('showplan', function(event, id) {
if (id == 21) {
google.charts.setOnLoadCallback(drawGraphBC);
}
else if (id == 24) {
google.charts.setOnLoadCallback(drawSolcast);
}
}).trigger('showplan', [ currentPlanId ]);
//------------------------------------------------------------------------------------------------
//-- plan21, graph consumo BC
//---
function drawGraphBC() {
if (!chart4) {
// create empty element and add it to plan with ID=21
var graphEl = $('<div></div>').css({
position: 'absolute',
width: 1000, height: 350,
top: 20, left: 20
}).appendTo('#plan-21');
chart4 = new google.visualization.ComboChart(graphEl.get(0));
}
$.getJSON('/user/data.lp', function(jsonData) {
var data4 = new google.visualization.arrayToDataTable(jsonData);
var opt4 = { ... };
chart4.draw(data4, opt4);
}); //getJSON
}; //drawWCons
//------------------------------------------------------------------------------------------------
//-- plan24, grafica weather
//---
function drawSolcast() {
if (!chart5) {
// create empty element and add it to plan with ID=24
var graphSol = $('<div></div>').css({
position: 'absolute',
width: 1000, height: 350,
top: 400, left: 20,
border: 'solid'
}).appendTo('#plan-24');
chart5 = new google.visualization.ComboChart(graphSol.get(0));
}
$.getJSON('/user/data2.lp', function(jsonObj) {
var data5 = new google.visualization.DataTable();
var opt5 = { ... };
var date_formatter = new google.visualization.DateFormat({ pattern: 'dd MMM, HH:mm' });
date_formatter.format(data5, 0);
chart5.draw(data5, opt5);
}); //getJSON
}; //drawSolcast
}); // getscript
});