使用PHP和MYSQL的Google API图表

时间:2015-02-17 16:21:54

标签: php mysql

您好我正在尝试使用Google Live图表API来显示单个折线图,该折线图仅显示Y轴中的数值和X轴中的日期值。

  

MYSQL:   Estado:数字   霍拉:约会

这是我的代码,我不太喜欢php,所以我尝试了一个我发现的例子,但我不能让它工作。

<?php

 $public =  "admin"; //This is the user i search in the LIKE





class conect

{

private $host;

private $root;

private $pass;

private $db;



public function dbconect($host,$root,$pass,$db)

{

$this->host = $host;

$this->root = $root;

$this->pass = $pass;

$this->db   = $db;

$this->conexion = mysql_connect($this->host,$this->root,$this->pass);

mysql_query("SET NAMES 'utf8'");

mysql_select_db( $this->db, $this->conexion );

}



//se cierra la conexión

public function dbcerrar()

{

mysql_close($this->conexion);

}

//

}



$conex=new conect();



 $conex->dbconect("localhost","root","","database");

//extraccion array de datos

$res = mysql_query("SELECT Estado AS total,Hora FROM Datos WHERE Usuario LIKE '$public'");

while($arr = mysql_fetch_array($res))

{

$arrayhombres[$arr['Hora']] = array(

'Hora' => $arr['Hora'],

'total' => $arr['total'],

);

}





/*

echo "<pre>";

print_r($arrayhombres);

echo "</pre>";

*/

?>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>

<script type="text/javascript">

google.load("visualization", "1", {packages:["corechart"]});  

    google.setOnLoadCallback(drawChart);



 function drawChart() {

        var data = google.visualization.arrayToDataTable([

          ['Fecha', 'Bateria', 'Mujeres','Sin datos'],



          ['<?php echo date("Y-m-d", $i);?>',<?php echo $totalhombres;?>,<?php echo $totalsindatos;?>],

 <?php 

 }

 ?>

        ]);



        var options = {

          title: 'Estadistica Registros',

 pointShape: 'circle',

 pointSize: 3,//tamaño de los puntos

 series: {//colores de las lineas

            0: { color: '#29ab1e' },

            1: { color: '#2f7ce4' },

          },

 titleTextStyle: {color: '#db6c00',fontSize: 14},//estilos

 tooltip: { textStyle: { color: '#3d3d3d', fontSize: 10 }},//estilo toltip

 legend: {textStyle: {color: '#535353', fontSize: 12 }},//estilos leyenda

 vAxis: { textStyle: {color: '#3d3d3d', fontSize: 12 }},//estilos horizontal

 hAxis: { textStyle: {color: '#3d3d3d', fontSize: 12 }}//estilos vertical

        };



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

        chart.draw(data, options);

      }

</script>

<div id="chart_div" style="width: 400px; height: 300px;"></div>

2 个答案:

答案 0 :(得分:0)

这是因为您只是将日期加上2组数据放入图表中,但是您要告诉它预期3。

['Fecha', 'Bateria', 'Mujeres','Sin datos']

['<?php echo date("Y-m-d", $i);?>',<?php echo $totalhombres;?>,<?php echo $totalsindatos;?>, EXTRA DATA NEEDED HERE!]

此外还有一个紧密的支架,我认为不需要在那里。

 <?php 

 }

 ?>

我希望这会有所帮助

答案 1 :(得分:0)

如果这是您的完整代码,有几个问题。

首先,您可以通过查询数据库中某个用户的所有行(例如,&#39; admin&#39;)。然后你取出这些值并将它们放入一个数组$arrayhombres,键入你不能再次使用的小时/ horas。相反,您可以查询数据库并直接将行添加到图表中。

...
$res = mysql_query("SELECT Estado AS total,Hora FROM Datos WHERE Usuario LIKE '$public'");
?>
<script type="text/javascript" src="https://www.google.com/jsapi">    </script>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});  
  google.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Fecha/Date', 'Total Hombres'],
      <?php
        while($arr = mysql_fetch_array($res)){
          echo '[Date('.$arr['Hora'].'),'.$arr['total'].'],';
        }
      ?>
    ]
 ...

重要的是要了解您的&#39; Fecha / Hora&#39;列是。您可能需要将存储在SQL数据库中的日期转换为javascript DATE对象。

只是为了好玩,这里有一个小提琴,其中包括一个可用于参考的单线图。 http://jsfiddle.net/ghwh8b0m/