首页 > 解决方案 > 谷歌图表查询mysql

问题描述

我一直在尝试使用 json 在 php 和 javascript 之间传输信息,但不起作用。查询没问题,我检查一下名为“dia”的查询结果是一个日期,“importe”是一个数字(浮点数)

任何想法 ?谢谢您的帮助

<html>
 <head>
  <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi" > </script>
  
 </head>  
 <body>
   <div id="line_chart" style="width: 100%; height: 500px"></div>
  
     <?php
                            $consulta = "SELECT fecha as dia,SUM(importe) as importe FROM tfg.reservas WHERE fecha  < '2019-05-31' AND fecha > '2019-05-20' GROUP BY DAY(fecha) asc;";

    $json = array();   
$resultado = mysqli_query($conexion,$consulta);
 if ($resultado->num_rows > 0) {
      while($fila = mysqli_fetch_array($resultado)) {
          $dataRow = array(
              $fila['dia'],
              $fila['importe'],
              
          );
          array_push($json, $dataRow);
      }
  }

  $jsonstring = json_encode($json);
?>
     
     <script type="text/javascript">

   google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawChart);
   function drawChart()
   {
    var data = google.visualization.arrayToDataTable([
        [{type: 'date', label: 'dia'}, {type: 'number', label: 'importe'}]
      ]);

      data.addRows(<?= $jsonstring ?>);

    var options = {
     title:'Sensors Data',
     legend:{position:'bottom'},
     chartArea:{width:'95%', height:'65%'}
    };

    var chart = new google.visualization.LineChart(document.getElementById('line_chart'));

    chart.draw(data, options);
   }
  </script>
 </body>
</html>

标签: phphtmlmysqlcharts

解决方案


推荐阅读