JSON powered by while

Asked

Viewed 112 times

2

I have a Graph Builder that is powered by JSON which I wanted to generate homes dynamically ie have 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);

  • You want to have that while in Javascript or PHP?

2 answers

1

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

An example close 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>
  • The body while will it ever run? In the previous line you define i = 0 and in the condition of while the i has to be 2 to be executed.

  • 1

    Well noted @Otavio, corrected the reply :)

0

It worked.However I’m not able to pass value from a PHP array.

 <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 <= ) { data.push({ value: , color:"#F7464A", Highlight: "#FF5A5E", label: }); i++; }

var options = { animateScale: true };

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

  • must have had a problem at the time of placing the code, it would be possible to update your question with the code you are currently using?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.