afficher un graphique par defaut selon un picker

Eléphanteau du PHP | 21 Messages

02 août 2022, 12:42

Bonjour,
j'affiche des graphiques (googlechart) reposant sur une base mysql sur une page html.
j'utilise un picker date pour afficher le graphique selon un jour donné mais lorsque je sélectionne la page au départ, elle est vide tant que je n ai pas sélectionné une date.
j 'aimerai que , lorsque je selectionne la page de graphique temperature alors la courbe des dernieres 24 h s affiche.
merci a vous.
<script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(graphtemp);

      function graphtemp() {
        var data = google.visualization.arrayToDataTable([
          ['Date', 'Température '],


          <?php 
          mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
               if (isset($_POST['selectedatetemp'])) {
                    $timestamp = strtotime($_POST['selectedatetemp']);   
                    $date = date('Y-m-d ', $timestamp);
                    $month = (int) date('n', $timestamp);
                    $year = (int) date('Y', $timestamp);    
   
        
                    $chartQuery = "SELECT  date_format(Date,'%d/%m/%Y %H:%i ') AS 'Update', TmpExt AS TmpExt_Max  FROM ZiMeteo WHERE DATE(Date) = ('$date')  ORDER BY ID DESC LIMIT 144 ";
}                   
                else {
                    $chartQuery = "SELECT  date_format(Date,'%d/%m/%Y %H:%i ') AS 'Update', TmpExt AS TmpExt_Max  FROM ZiMeteo WHERE DATE(Date) = ('$date')  ORDER BY ID DESC LIMIT 144 ";

}

           $chartQueryRecords = mysqli_query($connect, $chartQuery);
                mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
                 

                    while ($row = mysqli_fetch_assoc($chartQueryRecords)) {
                         echo "['".$row['Update']."',".$row['TmpExt_Max']."],";
                    }
 
           ?>
           ]);

        var options = {
            
            
            explorer: {
                axis: 'horizontal',
                keepInBounds: true,
                maxZoomIn: 4.0
          },
            series: {
                0: { color: '#D80010' },
                1: { color: 'blue' },
                
        },
            backgroundColor: 'transparent',
            color: ['white'],
            lineWidth: 4,
            hAxis: { direction: -1,
                     format:'MMM d, y',

                     textStyle: {
                     color:'white'
                              },
         

                          },
            vAxis: { 
                     textStyle: {
                        color:'red',
                     },
            explorer: {
                    axis: 'horizontal',
                    keepInBounds: true,
                    maxZoomIn: 4.0,
                            },        


                 },

            chartArea: {left : 100,
                        width: 1130},
            legend: { position: 'bottom',
                      textStyle: {
                       color: 'white'},
                }
        };

        var chart = new google.visualization.AreaChart(document.getElementById('temp_chart'));

        chart.draw(data, options);
      }
</script>
<form action="temperature.php" method="POST" name="formulaire">
  <input type="date" name="selectedatetemp" defaultDate= "now">
  <input type="submit" value="Go">
</form>

Mammouth du PHP | 1967 Messages

02 août 2022, 15:18

il faut que tu définisse une date par défaut avant ton if(isset(...

tes variables $month et $year sont inutile, tu ne les utilise pas.
Spols
pour les fan de rubik's cube ou pour les curieux ==> le portail francophone du rubik's cube

Eléphanteau du PHP | 21 Messages

02 août 2022, 15:37

Bonjour. Comment je fait ca ? je n ai aucune idee de comment le realiser ?

Mammouth du PHP | 1967 Messages

02 août 2022, 16:00

a priori je modifierai le php comme ceci
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
               $date = date('Y-m-d ');
               if (isset($_POST['selectedatetemp'])) {
                    $timestamp = strtotime($_POST['selectedatetemp']);   
                    $date = date('Y-m-d ', $timestamp);
                    $month = (int) date('n', $timestamp);
                    $year = (int) date('Y', $timestamp);    
   
        
                    $chartQuery = "SELECT  date_format(Date,'%d/%m/%Y %H:%i ') AS 'Update', TmpExt AS TmpExt_Max  FROM ZiMeteo WHERE DATE(Date) = ('$date')  ORDER BY ID DESC LIMIT 144 ";
}                   
                else {
                    $chartQuery = "SELECT  date_format(Date,'%d/%m/%Y %H:%i ') AS 'Update', TmpExt AS TmpExt_Max  FROM ZiMeteo WHERE DATE(Date) = ('$date')  ORDER BY ID DESC LIMIT 144 ";

}

           $chartQueryRecords = mysqli_query($connect, $chartQuery);
                mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
                 

                    while ($row = mysqli_fetch_assoc($chartQueryRecords)) {
                         echo "['".$row['Update']."',".$row['TmpExt_Max']."],";
                    }
 
ainsi $date a la valeur du jour sauf si le formulaire en donne une autre
Spols
pour les fan de rubik's cube ou pour les curieux ==> le portail francophone du rubik's cube