highcharts

How to dynamically load libraries (more than one)?


Currently, due to the needs of my project, I am loading the libraries dynamically and the first time I load them I get errors and not all the graphics work.

I don't know how to correct the load so that it works correctly and I don't know the cause of why it fails.

Here is an image of the errors:

enter image description here

The libraries are loaded correctly, as far as I understand:

enter image description here

The js code I use to load the libraries is

for ( var i = 0; i < librerias.length; i++ ) {
    var rutaLibreria = librerias[ i ];
    //console.log( "Ruta de la librería:", rutaLibreria );

    var fechaActual = new Date();
    let timestamp = fechaActual.getTime();

    let fechaFormateada = new Date( timestamp ).toISOString().replace( /[^0-9]/g, '' );

    if (loadedCount === librerias.length) { 
        if ( !isScriptLoaded( rutaLibreria ) ) {
            console.log("Carganddo la ULTIMA libreria...");
            script = document.createElement( 'script' );
            script.type = 'text/javascript';
            script.src = `${rutaLibreria}?${fechaFormateada}`;
            script.onload = function () {
                Highcharts.chart( container, config );
            };
            document.head.appendChild( script );
        }
    }else{
        loadedCount++;
        console.log("Carganddo SOLO la libreria...");
        if ( !isScriptLoaded( rutaLibreria ) ) {
            script = document.createElement( 'script' );
            script.type = 'text/javascript';
            script.src = `${rutaLibreria}?${fechaFormateada}`;
            document.head.appendChild( script );
        }

    } 
}

If someone could help me, I would greatly appreciate it.

What I'm trying to do is to do a dynamic library loading along with the graph configuration, so I can draw any graph as needed.

Added Detail


Indeed, I think it would not be possible to replicate. I will try to leave more details. The js code that I left is the one that loads the js libraries (librerias) in the header. the libraries arrive like this:

["https://code.highcharts.com/highcharts.js","https://code.highcharts.com/modules/series-label.js","https://code.highcharts.com/modules/exporting.js","https://code.highcharts.com/modules/export-data.js","https://code.highcharts.com/modules/accessibility.js"]

and the chart configuration (config) like this:

{"chart":{"type":"line"},"title":{"text":"Solar Employment Growth by Sector, 2010-2016"},"subtitle":{"text":"Source= thesolarfoundation.com"},"yAxis":{"title":{"text":"Number of Employees"}},"xAxis":{"accessibility":{"rangeDescription":"Range= 2010 to 2017"}},"legend":{"layout":"vertical","align":"right","verticalAlign":"middle"},"plotOptions":{"series":{"label":{"connectorAllowed":false},"pointStart":2010}},"series":[{"name":"Installation","data":[43934,52503,57177,69658,97031,119931,137133,154175]},{"name":"Manufacturing","data":[24916,24064,29742,29851,32490,30282,38121,40434]},{"name":"Sales & Distribution","data":[11744,17722,16005,19771,20185,24377,32147,39387]},{"name":"Project Development","data":[0,0,7988,12169,15112,22452,34400,34227]},{"name":"Other","data":[12908,5948,8105,11248,8989,11816,18274,18111]}],"responsive":{"rules":[{"condition":{"maxWidth":500},"chartOptions":{"legend":{"layout":"horizontal","align":"center","verticalAlign":"bottom"}}}]}}

and when I run the graph like this:

Highcharts.chart(container, config );

Solution

  • I got everything working fine with the following code. The libraries have a list of dependencies in json format.

        function loadScripts( url ) {
        return new Promise( ( resolve, reject ) => {
            const script = document.createElement( 'script' );
            script.src = url;
            script.onload = resolve;
            script.onerror = reject;
            document.head.appendChild( script );
        } );
    
    }
    
    async function loadHighcharts( lib ) {
        try {
            for ( const url of lib ) {
                if ( !isScriptLoaded( url ) ) {
                    await loadScripts( url );
                }
            }
            Cargar( $CONFIG );
        } catch ( error ) {
            console.error( "Error al cargar las librerías de Highcharts:", error 
    );
        }
    }
    
    function isScriptLoaded( url ) {
        var headContent = document.head.innerHTML;
        return headContent.includes( url );
    }
    
    function Cargar( config ) {
        let json = gx.json.evalJSON( config );
        Highcharts.chart( $ID, json );
    }
    
    window.onload = loadHighcharts( librerias );