测试变量是否是正确的数字并显示条形图数据

i'm trying to make my bar chart a little more dynamic so that the user would be able to change the number of values entered. I'm using a php variable which is converted into a JQuery variable. Now the chart works fine but when I try and add an if statement for if the no of bars is more than or equal to the number of values the chart disapears. Anyone know where I'm going wrong here?

<script>
    window.onload = function (){
        var pValue1 = <?php echo $pValue1;?>;
        var pValue2 = <?php echo $pValue2;?>;
        var pValue3 = <?php echo $pValue3;?>;
        var pValue4 = <?php echo $pValue4;?>;
        var pValue5 = <?php echo $pValue5;?>;

        var NoOfBars = <?php echo $NoOfBars;?>;

        var bar = new RGraph.Bar({
            id:'cvs1',
            data: 
            if (NoOfBars >= 1){
                [pValue1],
            }
            if (NoOfBars >= 2){
                [pValue1, pValue2],
            }
            if (NoOfBars >= 3){
                [pValue1, pValue2, pValue3],
            }
            if (NoOfBars >= 4){
                [pValue1, pValue2, pValue3, pValue4],
            }
            if (NoOfBars >= 5){
                [pValue1, pValue2, pValue3, pValue4, pValue5],
            }
            options: {
              backgroundGridDashed: true,
              labels: ['Mal', 'Barry', 'Gary', 'Neil', 'Kim', 'Pete', 'Lou', 'Fred', 'Jobe'],
              title: 'Title of Chart',
              strokestyle: 'rgba(0,0,0,0)',
              textAccessible: true
            }
        }).draw();
    };
</script>

So what you are trying to do is assign to JSON, but what you need to do is to assign it to a variable since you have a lot of conditions.

I created a variable for you, which is then assigned. (I defaulted it to the empty array based on your trend. Then i would assign that in the JSON for RGraph.Bar

<script>
    window.onload = function (){
        var pValue1 = <?php echo $pValue1;?>;
        var pValue2 = <?php echo $pValue2;?>;
        var pValue3 = <?php echo $pValue3;?>;
        var pValue4 = <?php echo $pValue4;?>;
        var pValue5 = <?php echo $pValue5;?>;

        var NoOfBars = <?php echo $NoOfBars;?>;
        var barValue = [];  // <-- new variable.

        if (NoOfBars >= 1){
            barValue = [pValue1];
        }
        if (NoOfBars >= 2){
            barValue = [pValue1, pValue2];
        }
        if (NoOfBars >= 3){
            barValue = [pValue1, pValue2, pValue3];
        }
        if (NoOfBars >= 4){
            barValue = [pValue1, pValue2, pValue3, pValue4];
        }
        if (NoOfBars >= 5){
            barValue = [pValue1, pValue2, pValue3, pValue4, pValue5];
        }

        var bar = new RGraph.Bar({
            id:'cvs1',
            data: barValue,     //  <-- assignment
            options: {
              backgroundGridDashed: true,
              labels: ['Mal', 'Barry', 'Gary', 'Neil', 'Kim', 'Pete', 'Lou', 'Fred', 'Jobe'],
              title: 'Title of Chart',
              strokestyle: 'rgba(0,0,0,0)',
              textAccessible: true
            }
        }).draw();
    };
</script>

That's NOT how you build an array conditionally. if is not a function, it has no return value, you can NOT assign an if to a variable.

You code should be:

var foo = []; // empty array
if ( ... something ...) {
   foo.push(new value);
}
if (... something else ) {
   foo.push (new value);
}
doSomething(foo);