0

i want to represent the following array data which is stored in the variable $dataBycountry. this variable contain

(
    [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
        )
)

and this is my script

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

i want to use those countries as lables and display barcharts dynamically.please give me a solution how to use that $dataByCountry variable in script.

WhiteHat
  • 59,912
  • 7
  • 51
  • 133
Abid
  • 344
  • 1
  • 4
  • 21
  • Is that PHP at the beginning there? Question isn't tagged PHP, but that is definitely not valid JavaScript. – nbering Nov 23 '16 at 15:09
  • yes there is php code. – Abid Nov 23 '16 at 15:26
  • Possible duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – M. Laing Nov 23 '16 at 16:38

1 Answers1

2

This has been covered before... The following link should help. It outlines several different approaches along with their pros and cons.

How to pass variables and data from PHP to JavaScript?

Several other duplicates:

Access PHP variable in JavaScript

How to access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?>

Community
  • 1
  • 1
M. Laing
  • 1,607
  • 11
  • 25