JSON powered by while

2

I have a graphical counter that is fed by JSON what I wanted to err generate dynamically houses ie having a number of elements stipulated by a variable.

Example of what I tried to do:

<script type="text/javascript">
// Get the context of the canvas element we want to select
var ctx = document.getElementById("piechart").getContext("2d");
var data = [
while(i=2)
{
{
    value: <?print $valor;?>,
    color:"#F7464A",
    highlight: "#FF5A5E",
    label: "Red"
},
i++;
}


];

var options = {
  animateScale: true
};

var myNewChart = new Chart(ctx).Pie(data,options);

    
asked by anonymous 07.01.2016 / 14:28

2 answers

1

In this code, while this is within the variable date, the variable must be set before as an array and within the while you add the values.

An example next to yours that should work:

<script type="text/javascript">
// Get the context of the canvas element we want to select
var ctx = document.getElementById("piechart").getContext("2d");
var data = [];
var i = 0;
while(i <= 2)
{
    data.push({
        value: <?print $valor;?>,
        color:"#F7464A",
        highlight: "#FF5A5E",
        label: "Red"
    });
    i++;
}

var options = {
  animateScale: true
};

var myNewChart = new Chart(ctx).Pie(data,options);
</script>
    
07.01.2016 / 14:46
0

It worked. But I'm not able to pass value from an array of PHP.

 <script type="text/javascript">

// Get the context of the canvas element we want to select var ctx = document.getElementById ("piechart"). getContext ("2d"); var data = []; var i = 0; while (i

07.01.2016 / 19:46