File "traitement0013.php"

Full Path: /home/analogde/www/userlogin/Json/traitement0013.php
File size: 9.07 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/chart.js"></script> <!-- Inclure Chart.js -->
    <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>
</head>
<body>
    <h1>Chart - dev</h1>
    <div id="chart-container">
        <div id="receivedData"></div>
        <p id="rowCount">Nombre de lignes  : </p>

        <div id="displayedData"></div>
        <div id="dbData"></div>
        <p id="rowCount">Nombre de lignes  : </p>
        <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>
        </div>
        <div class="chart-title">Graphique 2 courbes</div>
        <canvas id="myChart"></canvas>
    </div>
    <p id="result"></p>
    <p id="countValue"></p>
    <div id="tableContainer"></div>
    <div id="cellsContainer"></div>

    <script>
        function pipo() {
            fetch("load.php")
                .then(response => response.json())
                .then(data => {
                    
                    //console.log(" Lecture fichier JSON " + 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>");

                    //console.log(" Step 1 " + data);

                    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 data;
                })
                .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() {
            

            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>`;

                jsonData.push([rowIndex + 1, count]);
            });

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

            let countValues = jsonData.map(row => row[1]);
            countValues.reverse();
            jsonData.forEach((row, index) => {
                row[1] = countValues[index];
            });

            let firstCells = jsonData.map(row => row[0]);
            firstCells.reverse();
            firstCells.push(0);

            let secondCells = jsonData.map(row => row[1]);
            secondCells.unshift(0);

            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));

            const maxXValue = secondCells.reduce((sum, value) => sum + value, 0);

            console.log("Max " + maxXValue);

            let processedSecondCells = [...secondCells];
            for (let i = 2; i < processedSecondCells.length; i++) {
                processedSecondCells[i] += processedSecondCells[i - 1];
            }

            console.log(" Sum " + processedSecondCells);

            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);
        }

        function double(x1, x2, y) {
            const ctx = document.getElementById('myChart').getContext('2d');
            const myChart = new Chart(ctx, {
                type: 'line',
                data: {
                    datasets: [{
                        label: 'Courbe 1',
                        data: x1.map((value, index) => ({ x: value, y: y[index] })),
                        borderColor: 'rgba(75, 192, 192, 1)',
                        borderWidth: 1,
                        fill: false,
                        showLine: true
                    }, {
                        label: 'Courbe 2',
                        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
                        }
                    }
                }
            });
        }

        window.onload = displayResult;

        //pipo();

    </script>
</body>
</html>