2013-04-24 48 views
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); 
    } 

正如你所看到的,變量今天,uniquehitstoday都在那裏,雖然他們沒有在圖表上顯示。我parsedint,因爲我有錯誤說我不能使用字符串。

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如果neccessary

<? 
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

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(); 
     } 
+0

謝謝!奇蹟般有效。 – 2013-04-24 10:20:16