简体   繁体   English

Google Chart API未使用javascript / ajax显示图表

[英]Google chart API is not displaying the chart using javascript/ajax

What I am doing is to display a graph when a user clicks a button. 我正在做的是在用户单击按钮时显示图形。 The chart is being fetched from a PHP file using javascript. 该图表是使用javascript从PHP文件中提取的。 Here is the code what i am actually doing. 这是我实际上在做什么的代码。

This is the javascript function that fetches graph and attaches it to HTML DOM 这是获取图形并将其附加到HTML DOM的javascript函数

function draw_graph(num) {

   var xmlhttp;
   if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp=new XMLHttpRequest();
   } else { // code for IE6, IE5
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }

   xmlhttp.onreadystatechange = function() {
       if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        var output = xmlhttp.responseText;
        document.getElementById('script_content').innerHTML = output;
       }
   }
   xmlhttp.open("GET","includes/getgraphdata.php?type="+num+"&mode=day",true);
   xmlhttp.send();

}

This is the PHP code, I am trying to draw a simple graph with dummy data 这是PHP代码,我正在尝试绘制带有伪数据的简单图形

    <?php

echo "
    <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 = new google.visualization.DataTable();
                            data.addColumn('string', 'Date');
                            data.addColumn('number', 'Sell');
                            data.addColumn('number', 'Purchase');
                            data.addRows([
                              ['12-03-2012', 150, 300],
                              ['12-04-2012', 250, 500]
                            ]);

                            var options = {
                              title: 'Graph'
                            };
                            var chart = new google.visualization.LineChart.(document.getElementById('temp'));
                            chart.draw(data, options);
                        }
                    </script>
    ";

The above code attached with the HTML DOM correctly, but graph is not displaying. 上面的代码正确随附了HTML DOM,但未显示图形。

this line 这条线

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

Remove . 删除. after LineChart . LineChart之后。 Hence, it is LineChart(document.getElementById('temp')); 因此,它是LineChart(document.getElementById('temp'));

Also place an alert("statement reached"); 同时发出alert("statement reached"); between the above line and the next line to track the error. 在上一行和下一行之间跟踪错误。 If the error takes place, next line may not get executed, and thus use it as a debug advantage. 如果发生错误,则可能不会执行下一行,因此可以将其用作调试优势。

I've tried that before, you can't include text javascript code using ajax. 我之前曾尝试过,您不能使用Ajax包含文本javascript代码。

If you really want to do it that way you have to eval() the responseText string or have the javascript do something based on the string returned. 如果您确实想这样做,则必须eval() responseText字符串,或者让javascript根据返回的字符串执行某些操作。

eg. 例如。

if(responseText == '1'){
 function1();
}else{
 function2();
}

Is there any reason you can't just include the php file instead? 有什么原因不能只包含php文件吗?

<?php
require_once 'googleapi.php';
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM