duanliao6077 2016-09-23 18:12
浏览 7
已采纳

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

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>
  • 写回答

2条回答 默认 最新

  • dtx63505 2016-09-23 18:16
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)