2016-11-23 10 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 
     ) 
) 

を含み、これは私がlablesと表示棒グラフは私のスクリプトにその$ dataByCountry変数を使用するにはどのように解決策を与えるdynamically.pleaseとしてそれらの国を使用したい

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 
    }); 
} 

私のスクリプトです。

+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) –

答えて

関連する問題