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

标签 php mysql json google-visualization

我正在尝试为 SQL 表生成一个 google 图表,但无法完成。我已经创建了一个由 X 和 Y 值组成的表,我应该得到有关 X 和 Y 值的折线图。生成错误“表没有列”并且未生成图表。任何人都可以帮助我解决这个问题?这是我的代码:

创建表格:

<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);
?>

谷歌图表的显示:

<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>

最佳答案

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

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

json 必须是特定格式,found here

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

因此,使用 static method arrayToDataTable相反

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

注意:因为该方法是静态的,所以不需要 new 关键字

编辑

如果数据不包含列标题,请确保将第二个参数设置为 true (firstRowIsData)...

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>

关于php - 我无法使用 mysql 表中的数据创建谷歌折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43696905/

相关文章:

php - 使用 Dompdf 保存 PDF 文件

php - 获取不同变量中相同 id 的多行

php - 将我的标准化表关联在一起

php - 从表 2's column to Table 1' 列中复制跟踪 ID

mysql - 如何求和上个月的值?

javascript - 在 react 中显示数据json到表

php - 将 PHP 页面显示为图像

mysql - Tomcat 服务器与 Mysql 5.5 连接

.net - 南希FX : Deserialize JSON

java - 发送 POST 请求或尝试更新时出现 org.springframework.http.converter.HttpMessageNotReadableException