开发者社区> 问答> 正文

PHP MySQL Google Chart JSON-完整示例

我进行了很多搜索,找到了一个很好的示例,该示例使用MySQL表数据作为数据源来生成Google Chart。经过几天的搜索,我意识到几乎没有可用PHP和MySQL组合生成Google图表(饼图,条形图,列,表)的示例。我终于设法得到一个例子。

我以前从StackOverflow那里获得了很多帮助,所以这次我将返回一些帮助。

我有两个例子;一个使用Ajax,另一个不使用。今天,我将仅展示非Ajax示例。

用法:

要求:PHP,Apache和MySQL

安装:

  ---通过使用phpMyAdmin创建数据库并将其命名为“图表”
  ---通过使用phpMyAdmin创建一个表并将其命名为“ googlechart”,并进行 
      确保表只有两列,因为我已经使用了两列。然而,
      您可以根据需要使用2列以上的列,但必须更改 
      为此编码
  ---指定列名称,如下所示:“ weekly_task”和“ percentage”
  ---在表中插入一些数据
  ---对于百分比列,仅使用数字

      ---------------------------------
      范例资料:表格(googlechart)
      ---------------------------------

      周任务百分比
      ----------- ----------
      睡觉30
      看电影10
      工作40
      练习20    

PHP-MySQL-JSON-Google图表示例:

<?php

$con=mysql_connect("localhost","Username","Password") or die("Failed to connect with database!!!!"); mysql_select_db("Database Name", $con); // The Chart table contains two fields: weekly_task and percentage // This example will display a pie chart. If you need other charts such as a Bar chart, you will need to modify the code a little to make it work with bar chart and other charts $sth = mysql_query("SELECT * FROM chart");

/*

example data: Table (Chart)

weekly_task percentage Sleep 30 Watching Movie 40 work 44 */

//flag is not needed $flag = true; $table = array(); $table['cols'] = array(

// Labels for your chart, these represent the column titles
// Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
array('label' => 'Weekly Task', 'type' => 'string'),
array('label' => 'Percentage', 'type' => 'number')

);

$rows = array(); while($r = mysql_fetch_assoc($sth)) { $temp = array(); // the following line will be used to slice the Pie chart $temp[] = array('v' => (string) $r['Weekly_task']);

// Values of each slice
$temp[] = array('v' => (int) $r['percentage']); 
$rows[] = array('c' => $temp);

}

$table['rows'] = $rows; $jsonTable = json_encode($table); //echo $jsonTable; ?>

PHP-PDO-JSON-MySQL-Google图表示例:

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); /* select all the weekly tasks from the table googlechart */ $result = $conn->query('SELECT * FROM googlechart'); /* --------------------------- example data: Table (googlechart) -------------------------- weekly_task percentage Sleep 30 Watching Movie 10 job 40 Exercise 20 */ $rows = array(); $table = array(); $table['cols'] = array( // Labels for your chart, these represent the column titles. /* note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for Slice title */ array('label' => 'Weekly Task', 'type' => 'string'), array('label' => 'Percentage', 'type' => 'number') ); /* Extract the information from $result */ foreach($result as $r) { $temp = array(); // the following line will be used to slice the Pie chart $temp[] = array('v' => (string) $r['weekly_task']); // Values of each slice $temp[] = array('v' => (int) $r['percentage']); $rows[] = array('c' => $temp); } $table['rows'] = $rows; // convert data into JSON format $jsonTable = json_encode($table); //echo $jsonTable; } catch(PDOException $e) { echo 'ERROR: ' . $e->getMessage(); } ?>
<html>
  <head>
    <!--Load the Ajax API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript">

    // Load the Visualization API and the piechart package.
    google.load('visualization', '1', {'packages':['corechart']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);

    function drawChart() {

      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(<?=$jsonTable?>);
      var options = {
           title: 'My Weekly Plan',
          is3D: 'true',
          width: 800,
          height: 600
        };
      // Instantiate and draw our chart, passing in some options.
      // Do not forget to check your div ID
      var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
      chart.draw(data, options);
    }
    </script>
  </head>

  <body>
    <!--this is the div that will hold the pie chart-->
    <div id="chart_div"></div>
  </body>
</html>

PHP-MySQLi-JSON-Google图表示例

展开
收起
保持可爱mmm 2020-05-11 10:58:55 573 0
1 条回答
写回答
取消 提交回答
  • 有些人可能在本地或在服务器上遇到此错误:

    syntax error var data = new google.visualization.DataTable( ); 这意味着他们的环境不支持短标签,解决方案是改用以下标签:

    一切都应该正常工作!来源:stack overflow
    2020-05-11 10:59:09
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载

相关镜像