function generarGraficosEP15() {
    google.charts.setOnLoadCallback(dibujarGraficosEP15);
}

function dibujarGraficosEP15() {
    $.get("GetResultsJSONEP15", {
        _token: $('meta[name="csrf-token"]').attr("content"),
        id_reference: $("#id_analito_ep15").val(),
    })
        .done(function (json_elements) {
            var options = {
                legend: {
                    position: "right",
                    textStyle: {
                        color: "#555",
                        fontSize: 11,
                    },
                    alignment: "center",
                },
                series: {
                    9: {
                        pointSize: 12,
                        pointShape: "triangle",
                    },
                },
                hAxis: {
                    scaleType: "linear",
                    title: "Repeticiones en el tiempo",
                },
                vAxis: {
                    scaleType: "linear",
                    title: "Concentración",
                },
                showRowNumber: true,
                chartArea: {
                    width: "75%",
                    height: "80%",
                    left: "5%",
                    top: "5%",
                },

                // colors: ["#F56F6B", "#F5BD6B", "#97CFAD", "#7ABBCD", "#97CFAD", "#F5BD6B", "#F56F6B", "#000"],
                colors: [
                    "#1E1B7C",
                    "#F56F6B",
                    "#F5BD6B",
                    "#97CFAD",
                    "#7ABBCD",
                    "#00FF9E",
                    "#97CFAD",
                    "#F5BD6B",
                    "#F56F6B",
                    "#000",
                    "#1E1B7C",
                ],
                width: 912,
                height: 300,
            };

            for (
                nivel = 1;
                nivel <= Object.keys(json_elements).length;
                nivel++
            ) {
                let json_element_act = json_elements[nivel];

                let resultados_nivel = [];

                for (ic = 0; ic < json_element_act.length - 1; ic++) {
                    corridaAct = json_element_act[ic];
                    for (cont = 1; cont <= 5; cont++) {
                        if (
                            corridaAct["concentracion_" + cont] != "" &&
                            parseFloat(corridaAct["concentracion_" + cont]) != 0
                        ) {
                            resultados_nivel.push(
                                parseFloat(corridaAct["concentracion_" + cont])
                            );
                        }
                    }
                }

                let media = media_ce(resultados_nivel);
                let de = de_ce(resultados_nivel);
                let de_1_positivo = media + de * 1;
                let de_2_positivo = media + de * 2;
                let de_3_positivo = media + de * 3;
                let de_1_negativo = media - de * 1;
                let de_2_negativo = media - de * 2;
                let de_3_negativo = media - de * 3;

                let valorDiana = parseFloat(json_elements[nivel][5]);
                let errorTotal = parseFloat(
                    json_elements[nivel][0]["etmp"]
                );

                let limite_superior =
                    (errorTotal * valorDiana) / 100 + valorDiana;
                let limite_inferior = Math.abs(
                    (errorTotal * valorDiana) / 100 - valorDiana
                );

                console.log(
                    "ETMP: " +
                        json_elements[nivel][0]["etmp"] +
                        "\nvalor DIANA: " +
                        json_elements[nivel][5] +
                        "\nLimite superior: " +
                        limite_superior +
                        "\nLimite inferior: " +
                        limite_inferior
                );

                let obj_final_results = [];

                for (ix = 0; ix < resultados_nivel.length; ix++) {
                    if (
                        resultados_nivel[ix] != "" &&
                        parseFloat(resultados_nivel[ix]) != 0 &&
                        !isNaN(parseFloat(resultados_nivel[ix]))
                    ) {
                        obj_final_results.push([
                            ix + 1, // count
                            limite_superior,
                            de_3_positivo,
                            de_2_positivo,
                            de_1_positivo,
                            media,
                            valorDiana,
                            de_1_negativo,
                            de_2_negativo,
                            de_3_negativo,
                            resultados_nivel[ix],
                            limite_inferior,
                        ]);
                    }
                }

                var data = new google.visualization.DataTable();
                data.addColumn("number", "Repetición");
                data.addColumn("number", "Límite superior (" + obj_final_results[0][1].toFixed(3) + ")");
                data.addColumn("number", "+3 D.E (" + obj_final_results[0][2].toFixed(3) + ")");
                data.addColumn("number", "+2 D.E (" + obj_final_results[0][3].toFixed(3) + ")");
                data.addColumn("number", "+1 D.E (" + obj_final_results[0][4].toFixed(3) + ")");
                data.addColumn("number", "Media (" + obj_final_results[0][5].toFixed(3) + ")");
                data.addColumn("number", "DIAN (" + obj_final_results[0][6].toFixed(3) + ")");
                data.addColumn("number", "-1 D.E (" + obj_final_results[0][7].toFixed(3) + ")");
                data.addColumn("number", "-2 D.E (" + obj_final_results[0][8].toFixed(3) + ")");
                data.addColumn("number", "-3 D.E (" + obj_final_results[0][9].toFixed(3) + ")");
                data.addColumn("number", "Resultado", { role: "annotation" });
                data.addColumn("number", "Límite inferior (" + obj_final_results[0][11].toFixed(3) + ")");

                data.addRows(obj_final_results);

                const nodeChild = document.getElementById(
                    "grafica_ep15_" + nivel
                );
                const element = document.getElementById(
                    "funcion_prueba_load_dom"
                );

                const newNodeChild = document.createElement(`div`);
                newNodeChild.id = `grafica_ep15_${nivel}`;
                newNodeChild.style.width = "900px";
                newNodeChild.style.height = "300px";

                var chart = new google.visualization.LineChart(nodeChild);

                google.visualization.events.addListener(
                    chart,
                    "ready",
                    function () {
                        let formImgsTemp = $(".form-imgs-temp");
                        formImgsTemp.append(
                            '<textarea name="base64_grafica_ep15_' +
                                nivel +
                                '" id="base64_grafica_ep15_' +
                                nivel +
                                '"></textarea>'
                        );
                        $("#base64_grafica_ep15_" + nivel).val(
                            chart.getImageURI()
                        );
                    }
                );
                chart.draw(data, options);
            }
        })
        .fail(function () {
            notificacion(
                "normal",
                "error",
                "Error al cargar la información de la gráfica de control..."
            );
        })
        .always(function (sdsads) {
            notificacion(
                "normal",
                "info",
                "Información de la gráfica de control cargada..."
            );
            // console.log(sdsads);
        });
}
