我无法使用mysql表中的数据创建谷歌折线图

时间:2017-04-29 14:38:00

标签: php mysql json google-visualization

我正在尝试为SQL表生成一个Google图表,但无法完成它。我创建了一个由X和Y值组成的表,我应该得到有关X和Y值的折线图。有一个错误生成“table has no columns”并且没有生成图表。任何人都可以帮助我把它整理出来?。这是我的代码:

创建表:

<html>
<head>
<title>Creating MySQL Tables</title>
</head>
<body>
<?php

$conn = mysql_connect("localhost","root","","GOGRAPHS");
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("GO_GRAPHS") or die(mysql_error());

mysql_query("CREATE TABLE graphs(x INT NOT NULL,y INT NOT NULL)")
or die(mysql_error());
echo"table created successfully";

mysql_close($conn);
?>
</body>
</html>

插入数据:

<html>
<head>
<title>Creating MySQL Tables</title>
</head>
<body>
<?php

$conn = mysql_connect("localhost","root","");
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("GO_GRAPHS") or die(mysql_error());
mysql_query("INSERT INTO graphs(x, y)VALUES (10, 40),(20,60),(30,80)")
or die(mysql_error());  

mysql_close($conn);
?>

转换json中的数据(go_graphs_data.php):

<?php
$sql = mysql_connect("localhost","root","");
if(!$sql)
{
    echo "Connection Not Created";
}
$con = mysql_select_db("GO_GRAPHS");
if(!$sql)
{
    echo "Database Not Connected";
}
$data[] = array('A','B');
$sql = "select * from graphs";
$query = mysql_query($sql);

while($result = mysql_fetch_array($query))
{
$data[] = array((int)$result['x'],(int)$result['y']);
}      

echo json_encode($data);
?>

显示Google图表:

<html>

<head>

<!--Load the AJAX API-->

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script type="text/javascript">

     google.charts.load('current', {'packages':['corechart']});

    google.charts.setOnLoadCallback(line_chart);

     function line_chart() {

      var jsonData = $.ajax({

          url: "go_graphs_data.php",

          dataType: "json",

          async: false

          }).responseText;

          var data = new google.visualization.DataTable(jsonData);

      var chart = new google.visualization.ColumnChart(document.getElementById('linechart_div'));

      chart.draw(data, {width: 400, height: 240});

    }

 </script>

</head>

<body>

<!--Div that will hold the pie chart-->

<div id="linechart_div"></div>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

直接从json创建google-vis DataTable ...

var data = new google.visualization.DataTable(jsonData);

json必须采用特定格式found here

在这种情况下,您似乎正在构建一个简单的数组

因此,请改用static method arrayToDataTable

var data = google.visualization.arrayToDataTable(jsonData);

注意:由于该方法是静态的,因此不需要new关键字

编辑

如果数据不包含列标题,请务必将第二个参数设置为truefirstRowIsData)...

var data = google.visualization.arrayToDataTable(jsonData, true);

编辑2

使用所有数字查看以下工作代码段...

google.charts.load('current', {
  callback: drawBasic,
  packages:['corechart']
});

function drawBasic() {
  var data = new google.visualization.DataTable();
  data.addColumn('number', 'x');
  data.addColumn('number', 'y');

  data.addRows([
    [0, 0],   [1, 10],  [2, 23],  [3, 17],  [4, 18],  [5, 9],
    [6, 11],  [7, 27],  [8, 33],  [9, 40],  [10, 32], [11, 35],
    [12, 30], [13, 40], [14, 42], [15, 47], [16, 44], [17, 48],
    [18, 52], [19, 54], [20, 42], [21, 55], [22, 56], [23, 57],
    [24, 60], [25, 50], [26, 52], [27, 51], [28, 49], [29, 53],
    [30, 55], [31, 60], [32, 61], [33, 59], [34, 62], [35, 65],
    [36, 62], [37, 58], [38, 55], [39, 61], [40, 64], [41, 65],
    [42, 63], [43, 66], [44, 67], [45, 69], [46, 69], [47, 70],
    [48, 72], [49, 68], [50, 66],
  ]);

  var options = {
    chartArea: {
      top: 12,
      right: 12,
      bottom: 36,
      left: 24,
      height: '100%',
      width: '100%'
    },
    legend: {
      position: 'bottom'
    }
  };

  var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
  chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>