收到的值不会进入图表

时间:2013-04-24 00:05:54

标签: php jquery graph google-visualization

我似乎无法让我的图表正确地绘制数据,我添加了值并且它们在控制台中没有任何错误地正确接收,尽管图表没有显示这些数据。这是谷歌图表。

网站:http://oli.pw/stats.php?id=12131

  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Data', 'Visitors'],
      ['Todays Hits',  parseInt(todays)],
      ['Unique Hits Today', parseInt(uniquehitstoday)],
      ['Total Hits', parseInt(total)],
      ['Total Unique Hits', parseInt(uniquehits)]
    ]);

    var options = {
      title: 'Company Performance',
      hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
    };

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

正如你所看到的,今天的变量,今天唯一的变量,虽然它们没有出现在图表上。我解析因为我得到错误,说我不能使用字符串。

jQuery Post:

 $(document).ready(function () {
         var url = $('#getid').val();
         $.post("assets/stats/getStatsData.php", {
             url: url
         },
         function (result) {
             var response = jQuery.parseJSON(result);
             if (response.available === true) {
             total = response.totalhits;
             todays = response.todays;
             uniquehits = response.uhits;
             uniquehitstoday = response.uhitstoday;
             }
             else
             {
                 alert("An error has occured");
             }
         });

     });

这在应用程序加载开始时运行,允许在构建图形之前设置变量。

PHP如果需要

<?  
require("../config/config.php"); 
$id = $_POST['id'];
    $data = new stdClass();
    $data->available= true;
    $date = date("M d, Y"); 
    $uniquea = $dbh->query("SELECT DISTINCT shorturl FROM stats WHERE shorturl = '$id'"); 
    $uniqueb = count($uniquea->fetchColumn()); 
    $tdayua = $dbh->query("SELECT DISTINCT shorturl FROM stats WHERE date = '$date' AND shorturl = '$id'"); 
    $tdayub = count($tdayua->fetchColumn()); 
    $hitsa = $dbh->query("SELECT * from stats WHERE shorturl = '$id'"); 
    $hitsb = count($hitsa->fetchColumn()); 
    $tdayhitsa = $dbh->query("SELECT * from stats where date = '$date'"); 
    $tdayhitsb = count($tdayhitsa->fetchColumn()); 
    $data->totalhits= $hitsb;
    $data->todays= $tdayhitsb;
    $data->uhits = $uniqueb;
    $data->uhitstoday = $tdayub;
    echo json_encode($data);
?>

没有发生连接错误。

有什么想法吗?

感谢。

1 个答案:

答案 0 :(得分:1)

在你的ajax通话之前

google.setOnLoadCallback(drawChart);完成了。在文档中移动drawChart。准备:

         if (response.available === true) {
             alert(response.totalhits);
         total = response.totalhits;
         todays = response.todays;
         uniquehits = response.uhits;
         uniquehitstoday = response.uhitstoday;

         drawChart();
         }