MySQL查询谷歌图表中的日期格式

时间:2019-06-04 10:18:42

标签: php mysql date charts google-visualization

给日期格式一个mysql数据查询,以创建带有haxis日期的google图表。

  

错误:类型不匹配。值2019-05-25与列索引0中的类型日期不匹配。

查询的数据是2019-05-25和一个数字,问题是Google图表不接受日期的格式。

有什么主意吗?

<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: 'Importe en el Último Mes',
              width: 900,
              height: 500,
              hAxis: {
                format: 'yyyy-M-d'
              },
              vAxis: {
                gridlines: {color: 'none'},
                minValue: 0
              }
        };

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

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

0 个答案:

没有答案