2016-11-23 67 views
0

我想要表示存儲在變量$ dataBycountry中的以下數組數據。 這個變量包含如何使我的條形圖動態

(
    [0] => Array 
     (
      [country] => Russia 
      [sessions] => 132 
      [users] => 3 
      [pageviews] => 261 
     ) 

    [1] => Array 
     (
      [country] => India 
      [sessions] => 31 
      [users] => 20 
      [pageviews] => 67 
     ) 

    [2] => Array 
     (
      [country] => Austria 
      [sessions] => 18 
      [users] => 18 
      [pageviews] => 53 
     ) 
) 

,這是我的腳本

var randomScalingFactor = function(){ return Math.round(Math.random()*100)}; 

var barChartData = { 
    labels : ["i want lables also to be dynamic"], 
    datasets : [ 
     { 
      fillColor : "rgba(220,220,220,0.5)", 
      strokeColor : "rgba(220,220,220,0.8)", 
      highlightFill: "rgba(220,220,220,0.75)", 
      highlightStroke: "rgba(220,220,220,1)", 
      data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()] 
     }, 
     { 
      fillColor : "rgba(151,187,205,0.5)", 
      strokeColor : "rgba(151,187,205,0.8)", 
      highlightFill : "rgba(151,187,205,0.75)", 
      highlightStroke : "rgba(151,187,205,1)", 
      data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()] 
     } 
    ] 

} 
window.onload = function(){ 
    var ctx = document.getElementById("canvas").getContext("2d"); 
    window.myBar = new Chart(ctx).Bar(barChartData, { 
     responsive : true 
    }); 
} 

我想利用這些國家的標貼和顯示barcharts dynamically.please給我一個解決方案是如何使用該腳本$ dataByCountry變量。

+0

那是PHP的開始呢?問題沒有被標記爲PHP,但那絕對是無效的JavaScript。 – nbering

+0

是的,有PHP代碼。 – Abid

+0

[如何將變量和數據從PHP傳遞到JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) –

回答

相關問題