在php mysql中使用Google Chart API显示条形图

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了在php mysql中使用Google Chart API显示条形图脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在搜索很多这个.我得到了解决方案.这里是.这是使用 AJAXPHP中完成的.
我有2页是GOOGLEapi.PHP和其他getData.PHP,由googleapi.PHP发送的AJAX请求使用.

googleapi.PHP

<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(object) {

      // Create our data table out of JSON data loaded From server.
      VAR data = new google.visualization.DataTable(object);
      var options = {
           tITle: 'test API',is3D: 'true',width: 200,height: 100
        };
      // Instantiate and draw our chart,passing in some options.
      // Do not forget to check your div ID
      var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
      chart.draw(data,options);
    }

    function show()
    {

        XMlhttp = new XMLHttPRequest();
        xmlhttp.open("GET","getdata.PHP?name=jaspreet",true);
        xmlhttp.send();
        xmlhttp.onreadystatechange = getResponse;
    }
    function getResponse()
    {
        if(xmlhttp.readyState==4){
            alert(xmlhttp.responseText);
            var obj = JSON.parse(xmlhttp.responseText);
            drawChart(obj);
        }
    }
    </script>
  </head>

  <body>
    <!--this is the div that will hold the pie chart-->
    <div id="chart_div"></div>
    <select id="name" onchange="show();">
        <option value="test">test</option>
        <option value="test1">test1</option>
        <option value="test2">test2</option>
    </select>
  </body>
</html>

访问getdata.PHP

<?PHP $con=MysqL_connect("localhost","root","") or die("Failed to connect with database!!!!");
MysqL_select_db("student",$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 marks where name='jas'");

/*
---------------------------
example data: Table (Chart)
--------------------------
marks     percentage
English           30
Maths             40
Science            44
*/

$rows = array();
//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' => 'subject','type' => 'string'),array('label' => 'marks','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['subject']); 

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

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

就是这样,你需要创建一个数据库.它的作用就像一个魅力.

知道你的帖子是一个“答案”,但我认为我会进来的.如果你想要动态地输入数据到Google图表中,你可以使用PHP回调到javascript中. PHP可用于从您的服务器MysqL表中获取信息.

这是一个例子:

<?PHP   $i = 0; $grab = MysqL_query("SELECT * FROM `productList` WHERE 1");
    $pricelist = MysqL_fetch_array($grab);
    $numberofproducts = MysqL_num_rows($grab);

                              ?>       
    <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([
      ['parts','Prices'],['Number of Products',<?PHP echo $numberofproducts;?>],]);

    var options = {
      title: 'Number of Products',hAxis: {title: '',titleTextStyle: {color: 'red'}},width: 980,height: 200
    };

    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(data,options);
  }

脚本宝典总结

以上是脚本宝典为你收集整理的在php mysql中使用Google Chart API显示条形图全部内容,希望文章能够帮你解决在php mysql中使用Google Chart API显示条形图所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。