File "traitement0015.php"

Full Path: /home/analogde/www/DOSSIER/Json/traitement0015.php
File size: 14.77 KB
MIME-type: text/html
Charset: utf-8

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Interrogation DB</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom@latest"></script> <!-- Inclure chartjs-plugin-zoom -->

    <style>
        .chart-title {
            text-align: center;
            margin-bottom: 20px;
            font-size: 1.5em;
            font-weight: bold;
        }
        #chart-container {
            position: relative;
            width: 100%;
            max-width: 600px;
            margin: 0 auto;
        }
        #myChart {
            width: 100%;
            max-width: 600px;
        }
    </style>
    <script>

/**********************************************************/

async function pipo() 
{
    try {
            const response = await fetch("load.php");
            const data = await response.json();

            //const receivedDataElement = $("#receivedData");
            //receivedDataElement.html("<pre>++++++++\n" + JSON.stringify(data, null, 2) + "\n++++++++</pre>");

            //const rowCountElement = $("#rowCount");
            //rowCountElement.text("Nombre de lignes : " + data.length);

            const nb_lignes = data.length;
            console.log("Nb lignes " + nb_lignes);
            
            data.reverse();
            data.forEach(item => 
            {
                if (Array.isArray(item) && item.length > 1) 
                {
                    item.splice(1, 1);
                }
            });

            //const displayedDataElement = $("#displayedData");
            //displayedDataElement.html("<pre>Tableau après modifications :\n" + JSON.stringify(data, null, 2) + "</pre>");

            let cumulativeData = [];
            let cumulativeSum = 0;

            let labels = [];
            let values = [];

            data.forEach(item => {
                if (Array.isArray(item) && item.length > 0) 
                {
                    cumulativeSum += parseInt(item[item.length - 1], 10);
                    labels.push(item[0]);
                    values.push(cumulativeSum);
                    cumulativeData.push([item[0], cumulativeSum]);
                }
            });

            console.log(" Data cumulative " + cumulativeData);

            console.log(" Somme cumulative " + cumulativeSum);

            let chartLabels = cumulativeData.map(item => item[1]);
            let chartValues = cumulativeData.map(item => item[0]);

            chartLabels.unshift(0);
            chartValues.push(0);

           // console.log("Trace chartLabels: " + JSON.stringify(chartLabels));
           // console.log("Trace chartValues: " + JSON.stringify(chartValues));

            //return chartLabels;
            return { chartLabels, chartValues };
    } 
    catch (error) 
    {
        console.error("Erreur :", error);
        showMessageModal("Une erreur est survenue lors du chargement des données.");
    }
}


/**********************************************************/
/*
function wwwwwwwpipo() {
            fetch("load.php")
                .then(response => response.json())
                .then(data => 
                {
                    const receivedDataElement = $("#receivedData");
                    receivedDataElement.html("<pre>++++++++\n" + JSON.stringify(data, null, 2) + "\n++++++++</pre>");

                    const rowCountElement = $("#rowCount");
                    rowCountElement.text("Nombre de lignes : " + data.length);

                    data.reverse();

                    data.forEach(item => 
                    {
                        if (Array.isArray(item) && item.length > 1) 
                        {
                            item.splice(1, 1);
                        }
                    });

                    const displayedDataElement = $("#displayedData");
                    displayedDataElement.html("<pre>Tableau après modifications :\n" + JSON.stringify(data, null, 2) + "</pre>");

                    let cumulativeData = [];
                    let cumulativeSum = 0;

                    let labels = [];
                    let values = [];

                    data.forEach(item => 
                    {
                        if (Array.isArray(item) && item.length > 0) 
                        {
                            cumulativeSum += parseInt(item[item.length - 1], 10);
                            labels.push(item[0]);
                            values.push(cumulativeSum);
                            cumulativeData.push([item[0], cumulativeSum]);
                        }
                    });

                    /*cumulativeData.forEach(item => {
                            console.log(item);
                            const listItem = $("<li></li>").text(JSON.stringify(item));
                            dataList.append(listItem);
                    });*/
/*
                    // Préparer les données pour le graphique à partir de cumulativeData
                    let chartLabels = cumulativeData.map(item => item[1]);
                    let chartValues = cumulativeData.map(item => item[0]);

                    // Insérer un 0 au début de chartLabels
                    chartLabels.unshift(0);

                    // Insérer un 0 à la fin de chartValues
                    chartValues.push(0);

                    console.log(" Trace  " + chartLabels);
                    console.log(" Trace " + chartValues);

                    return chartLabels;
                })

                .catch(error => 
                {
                    console.error("Erreur :", error);
                    showMessageModal("Une erreur est survenue lors du chargement des données.");
                });
        }
*/
/**********************************************************/

        function loadFromDB()
        {
            return fetch("load_mysql.php")
                .then(response => response.json())
                .then(data => {
                    console.log(data);
                    return data;
                })
                .catch(error => {
                    console.error("Erreur :", error);
                    showMessageModal("Une erreur est survenue lors du chargement des données.");
                });
        }

/**********************************************************/

        function countValueOneInCells(cells) 
        {
            let count = 0;
            for (let i = 0; i < cells.length; i++) 
            {
                if (cells[i].value === "1") 
                {
                    count++;
                }
            }
            return count;
        }

/**********************************************************/

        async function displayResult()
        {
			
			//const extraction = await pipo();

            const { chartLabels, chartValues } = await pipo();

            console.log(" Extraction chartLabels " + chartLabels );
            console.log(" Extraction chartValues " + chartValues );
			
            const extraction = chartLabels;

			console.log(" Extraction " + extraction );
			
            let data = await loadFromDB();

            //const cellsContainer = document.getElementById('cellsContainer');
            //let cellsHTML = '<table border="1"><tr><th>Ligne</th><th>Nombre de "value": "1"</th></tr>';
            let jsonData = [];

            data.forEach((row, rowIndex) => {
                const cells = row.cells;
                const count = countValueOneInCells(cells);
                //cellsHTML += `<tr><td>${rowIndex + 1}</td><td>${count}</td></tr>`;

                // Ajouter les informations au tableau JSON sous forme de tableau imbriqué
                jsonData.push([rowIndex + 1, count]);
            });

            //cellsHTML += '</table>';
            //cellsContainer.innerHTML = cellsHTML;

            // Inverser uniquement la colonne des valeurs de count
            let countValues = jsonData.map(row => row[1]);
            countValues.reverse();
            jsonData.forEach((row, index) => {
                row[1] = countValues[index];
            });

            // Créer un tableau contenant uniquement les premières cellules
            let firstCells = jsonData.map(row => row[0]);

            // Inverser l'ordre des premières cellules
            firstCells.reverse();

            // Insérer un 0 à la fin de firstCells
            firstCells.push(0);

            // Créer un tableau contenant uniquement les deuxièmes cellules
            let secondCells = jsonData.map(row => row[1]);

            // Insérer un 0 en première position de secondCells
            secondCells.unshift(0);

            // Afficher les tableaux JSON dans la console
            console.log("Premières cellules inversées avec 0 ajouté :", JSON.stringify(firstCells, null, 2));
            console.log("Deuxièmes cellules avec 0 en première position :", JSON.stringify(secondCells, null, 2));

            // Calculer la somme des éléments de secondCells
            const maxXValue = secondCells.reduce((sum, value) => sum + value, 0);

            console.log("Max " + maxXValue);

            // Copier secondCells et appliquer le traitement
            let processedSecondCells = [...secondCells];
            for (let i = 2; i < processedSecondCells.length; i++) {
                processedSecondCells[i] += processedSecondCells[i - 1];
            }

            console.log(" Sum " + processedSecondCells);

           // simple(processedSecondCells, firstCells, maxXValue);

           // const tab2 = [0, 22, 31, 4, 9, 14, 5];

           // let x_seconde_courbe = [...tab2];
           // for (let i = 2; i < x_seconde_courbe.length; i++) {
           //     x_seconde_courbe[i] += x_seconde_courbe[i - 1];
           // }

           // console.log(" ------ " + x_seconde_courbe);

           // console.log("------------>>>>>> Prévoir integration provenant de pipo() ....");

            //double(processedSecondCells ,tab2, firstCells, maxXValue);

            //double(processedSecondCells ,  x_seconde_courbe, firstCells);
        double(processedSecondCells ,  extraction, firstCells);
		
		console.log(" Axe x1 : " + processedSecondCells);
		console.log(" Axe x2 : " + extraction);

		console.log(" Y : " + firstCells);

		
        }

/**********************************************************/

        function double(x1, x2, y)
        {

                // Création du graphique
        const ctx = document.getElementById('myChart').getContext('2d');
        const myChart = new Chart(ctx, {
            type: 'line',
            data: {
                datasets: [{
                    label: 'Estimation temps',
                    data: x1.map((value, index) => ({ x: value, y: y[index] })),
                    borderColor: 'rgba(75, 192, 192, 1)',
                    borderWidth: 1,
                    fill: false,
                    showLine: true
                }, {
                    label: 'Temps passé',
                    data: x2.map((value, index) => ({ x: value, y: y[index] })),
                    borderColor: 'rgba(153, 102, 255, 1)',
                    borderWidth: 1,
                    fill: false,
                    showLine: true
                }]
            },
            options: {
                scales: {
                    x: {
                        type: 'linear',
                        position: 'bottom'
                    },
                    y: {
                        beginAtZero: true
                    }
                }
            }
        });

        }
       
/**********************************************************/
        async function blabla()
        {
            const response = await fetch("load.php");
            if (!response.ok) {
                throw new Error("Erreur réseau : " + response.status);
            }
            const data = await response.json();
            console.log("Données reçues :", data);

            // Inverser le tableau JSON
            data.reverse();

            // Supprimer l'élément d'indice 1 dans chaque sous-tableau
            data.forEach(item => {
                if (Array.isArray(item) && item.length > 1) {
                    item.splice(1, 1);
                }
            });

            // Initialiser un nouveau tableau pour les résultats cumulés
            let cumulativeData = [];
            let cumulativeSum = 0;

            // Préparer les données pour le graphique
            let labels = [];
            let values = [];

            // Parcourir le tableau JSON et calculer les sommes cumulées
            data.forEach(item => {
                if (Array.isArray(item) && item.length > 0) {
                    // Convertir la valeur en entier et ajouter à la somme cumulée
                    cumulativeSum += parseInt(item[item.length - 1], 10);
                    // Ajouter les données pour le graphique
                    labels.push(item[0]); // Supposons que le premier élément est le label
                    values.push(cumulativeSum);
                    // Ajouter le sous-tableau modifié au nouveau tableau
                    cumulativeData.push([item[0], cumulativeSum]);
                }
            });

            // Préparer les données pour le graphique à partir de cumulativeData
            let chartLabels = cumulativeData.map(item => item[1]);
            let chartValues = cumulativeData.map(item => item[0]);

            // Insérer un 0 au début de chartLabels
            chartLabels.unshift(0);

            // Insérer un 0 à la fin de chartValues
            chartValues.push(0);

            return { chartLabels, chartValues };
        }

        window.onload = displayResult;

        //pipo();

/**********************************************************/

    </script>
</head>
<body>
    
    <div id="chart-container">
        <div class="chart-title">Graphique 2 courbes</div>
<!--    <div id="receivedData"></div>   -->
<!--    <p id="rowCount">Nombre de lignes  : </p>   -->

        <div id="displayedData"></div>
   
       
        <ul id="dataList"></ul>
        <p id="sumResult">Somme des jours : </p>
        <div id="chartLabelsContainer">
            <div id="chartLabels"></div>
        </div>
        <div id="chartValuesContainer">
            <div id="chartValues"></div>

        <canvas id="myChart"></canvas>
    </div>

    

    <p id="result"></p>
    <p id="countValue"></p>
    <div id="tableContainer"></div>
    <div id="cellsContainer"></div>
</body>
</html>