0

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

网址: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);
  }

如您所见,变量 todays、uniquehitstoday 都在其中,尽管它们没有出现在图表上。我解析,因为我收到错误说我不能使用字符串。

jQuery帖子:

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

没有发生连接错误。

有任何想法吗?

谢谢。

4

1 回答 1

1

google.setOnLoadCallback(drawChart);在您的ajax调用之前完成。在您的文档中移动 drawChart。准备好:

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

         drawChart();
         }
于 2013-04-24T10:17:21.913 回答